From 7b3b28836a3537061e2430023b57b647ca3662da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D5=A1=C9=A8=D5=BC=C9=A2=D3=84=D5=A1=D6=85=D5=BC=C9=A2?= Date: Sat, 16 Mar 2024 12:08:16 +0800 Subject: [PATCH] solutions: 0525 - Contiguous Array (Medium) --- solutions/0500-0599/0525-contiguous-array-medium.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/solutions/0500-0599/0525-contiguous-array-medium.md b/solutions/0500-0599/0525-contiguous-array-medium.md index cc6468afc1b..c3261743446 100644 --- a/solutions/0500-0599/0525-contiguous-array-medium.md +++ b/solutions/0500-0599/0525-contiguous-array-medium.md @@ -1,5 +1,6 @@ --- description: 'Author: @wingkwong | https://leetcode.com/problems/contiguous-array/' +tags: [Array, Hash Table, Prefix Sum] --- # 0525 - Contiguous Array (Medium) @@ -41,6 +42,8 @@ For example, given the input $$[0, 0, 0, 1, 1, 1]$$, $$pre$$ would be 0 -> -1 -> Therefore, the approach is to calculate the prefix sum and put it into a hash map. If the prefix sum can be found, then the we can compare the length with the current maximum answer to see if we update it or not. This solution gives both $$O(n)$$time complexity and space complexity. + + ```cpp @@ -60,8 +63,15 @@ public: }; ``` + + + Or you can initialise $$m[pre] = -1$$ for $$pre = 0$$. + + + + ```cpp class Solution { public: @@ -78,3 +88,6 @@ public: } }; ``` + + +