Skip to content

Commit

Permalink
update cheatsheet, 523 jaav
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Dec 14, 2024
1 parent e9c6d07 commit bb175ab
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
38 changes: 37 additions & 1 deletion doc/cheatsheet/prefix_sum.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ We can use prefix sums. Say P[i+1] = A[0] + A[1] + ... + A[i], where A[i] = 1 if
- LC 325
- Count Number of Nice Subarrays
- LC 1248
- Continuous Subarray Sum
- Continuous Subarray Sum (preSum with mod)
- https://github.com/yennanliu/CS_basics/blob/master/doc/pic/presum_mod.png
- LC 523
- Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold
- LC 1343
Expand Down Expand Up @@ -222,4 +223,39 @@ class Solution(object):
if acc - k in dic:
result = max(result, i - dic[acc-k])
return result
```

### 2-5) Continuous Subarray Sum

```java
// java
// LC 523
// V1
// IDEA : HASHMAP
// https://leetcode.com/problems/continuous-subarray-sum/editorial/
// https://github.com/yennanliu/CS_basics/blob/master/doc/pic/presum_mod.png
public boolean checkSubarraySum_1(int[] nums, int k) {
int prefixMod = 0;
HashMap<Integer, Integer> modSeen = new HashMap<>();
modSeen.put(0, -1);

for (int i = 0; i < nums.length; i++) {
/**
* NOTE !!! we get `mod of prefixSum`, instead of get prefixSum
*/
prefixMod = (prefixMod + nums[i]) % k;

if (modSeen.containsKey(prefixMod)) {
// ensures that the size of subarray is at least 2
if (i - modSeen.get(prefixMod) > 1) {
return true;
}
} else {
// mark the value of prefixMod with the current index.
modSeen.put(prefixMod, i);
}
}

return false;
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public boolean checkSubarraySum(int[] nums, int k) {
// V1
// IDEA : HASHMAP
// https://leetcode.com/problems/continuous-subarray-sum/editorial/
// https://github.com/yennanliu/CS_basics/blob/master/doc/pic/presum_mod.png
public boolean checkSubarraySum_1(int[] nums, int k) {
int prefixMod = 0;
HashMap<Integer, Integer> modSeen = new HashMap<>();
Expand Down

0 comments on commit bb175ab

Please sign in to comment.