Skip to content

Commit

Permalink
solutions: 0525 - Contiguous Array (Medium)
Browse files Browse the repository at this point in the history
  • Loading branch information
wingkwong committed Mar 16, 2024
1 parent b038047 commit 7b3b288
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions solutions/0500-0599/0525-contiguous-array-medium.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
description: 'Author: @wingkwong | https://leetcode.com/problems/contiguous-array/'
tags: [Array, Hash Table, Prefix Sum]
---

# 0525 - Contiguous Array (Medium)
Expand Down Expand Up @@ -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.

<Tabs>
<TabItem value="cpp" label="C++">
<SolutionAuthor name="@wingkwong"/>

```cpp
Expand All @@ -60,8 +63,15 @@ public:
};
```
</TabItem>
</Tabs>
Or you can initialise $$m[pre] = -1$$ for $$pre = 0$$.
<Tabs>
<TabItem value="cpp" label="C++">
<SolutionAuthor name="@wingkwong"/>
```cpp
class Solution {
public:
Expand All @@ -78,3 +88,6 @@ public:
}
};
```

</TabItem>
</Tabs>

0 comments on commit 7b3b288

Please sign in to comment.