Skip to content

Commit e9c6d07

Browse files
committed
add 523 java
1 parent c10e7bf commit e9c6d07

File tree

6 files changed

+208
-8
lines changed

6 files changed

+208
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@
367367
463| [Island Perimeter](https://leetcode.com/problems/island-perimeter/)|[Python ](./leetcode_python/Hash_table/island_perimeter.py) | _O(n)_ | _O(k)_ | Medium |`basic`,`hash table`, `google`, `fb`, amazon| AGAIN** (2)
368368
470| [Implement Rand10() Using Rand7()](https://leetcode.com/problems/implement-rand10-using-rand7/) | [Python ](./leetcode_python/Hash_table/implement-rand10-using-rand7.py) | _O(1)_ | _O(1)_ | Medium |`trick`, `google`| AGAIN** (3)
369369
473| [Matchsticks to Square](https://leetcode.com/problems/matchsticks-to-square/) | [Python ](./leetcode_python/Hash_table/matchsticks-to-square.py) | _O(n * s * 2^n)_ | _O(n * (2^n + s))_ | Medium |`complex`,`hard`| AGAIN (not start)
370-
523| [Continuous Subarray Sum](https://leetcode.com/problems/continuous-subarray-sum/) | [Python ](./leetcode_python/Hash_table/continuous-subarray-sum.py) | _O(n)_ | _O(k)_ | Medium |sub array sum, check `# 560 Subarray Sum Equals K`,`good trick`,`substring`, `hash table`,`AGAIN`,`M$`,`fb`, `apple`| AGAIN*************** (10) (MUST)
370+
523| [Continuous Subarray Sum](https://leetcode.com/problems/continuous-subarray-sum/) | [Python ](./leetcode_python/Hash_table/continuous-subarray-sum.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/HashTable/ContinuousSubarraySum.java) | _O(n)_ | _O(k)_ | Medium |sub array sum, check `# 560 Subarray Sum Equals K`,`good trick`,`substring`, `hash table`,`AGAIN`,`M$`,`fb`, `apple`| AGAIN*************** (10) (MUST)
371371
525| [Contiguous Array](https://leetcode.com/problems/contiguous-array/) | [Python](./leetcode_python/Hash_table/contiguous-array.py) | _O(n)_ | _O(n)_ | Medium | sub-array sum, `good basic`, `array`, `hashmap`, `cache`, `AGAIN`, `fb`, amazon| AGAIN*************** (11) (MUST)
372372
532| [K-diff Pairs in an Array](https://leetcode.com/problems/k-diff-pairs-in-an-array/) | [Python ](./leetcode_python/Hash_table/k-diff-pairs-in-an-array.py) | _O(n)_ | _O(n)_ | Medium |hash table, `basic`, `collections.Counter()`,`a-b =k -> a = k + b `, `amazon`| AGAIN********** (6)
373373
554| [Brick Wall](https://leetcode.com/problems/brick-wall/) | [Python ](./leetcode_python/Hash_table/brick-wall.py) | _O(n)_ | _O(m)_ | Medium |`trick`,`hash map`, `bloomberg`, `fb`, grab | AGAIN******** (5)

data/progress.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
20241208: 304,853,325,560(todo)
1+
20241214: 560(todo),523(todo)
2+
20241208: 304,853,325
23
20241202: 370(todo),1109(todo)
34
20241130: 34,767
45
20241126: 722,380

data/to_review.txt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,35 @@
1+
2025-02-07 -> ['560(todo),523(todo)']
12
2025-02-01 -> ['304,853,325']
23
2025-01-26 -> ['370(todo),1109(todo)']
34
2025-01-24 -> ['34,767']
45
2025-01-20 -> ['722,380']
56
2025-01-19 -> ['33,81']
6-
2025-01-17 -> ['253']
7+
2025-01-17 -> ['560(todo),523(todo)', '253']
78
2025-01-16 -> ['776,31']
89
2025-01-15 -> ['004(todo),34(todo),162(todo),275(todo)']
910
2025-01-14 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
1011
2025-01-11 -> ['304,853,325', '394']
1112
2025-01-10 -> ['833,950']
1213
2025-01-05 -> ['370(todo),1109(todo)']
13-
2025-01-04 -> ['53,210,207']
14+
2025-01-04 -> ['560(todo),523(todo)', '53,210,207']
1415
2025-01-03 -> ['34,767', '444']
1516
2025-01-02 -> ['1188,130,855(again)']
1617
2024-12-30 -> ['722,380']
1718
2024-12-29 -> ['304,853,325', '33,81']
1819
2024-12-28 -> ['900']
19-
2024-12-27 -> ['253', '26,27', '802,1197,26']
20+
2024-12-27 -> ['560(todo),523(todo)', '253', '26,27', '802,1197,26']
2021
2024-12-26 -> ['776,31']
2122
2024-12-25 -> ['004(todo),34(todo),162(todo),275(todo)']
2223
2024-12-24 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2324
2024-12-23 -> ['370(todo),1109(todo)']
25+
2024-12-22 -> ['560(todo),523(todo)']
2426
2024-12-21 -> ['304,853,325', '34,767', '394', '855,846']
2527
2024-12-20 -> ['833,950', '932']
28+
2024-12-19 -> ['560(todo),523(todo)']
2629
2024-12-18 -> ['951,792']
27-
2024-12-17 -> ['722,380']
28-
2024-12-16 -> ['304,853,325', '33,81']
29-
2024-12-15 -> ['370(todo),1109(todo)']
30+
2024-12-17 -> ['560(todo),523(todo)', '722,380']
31+
2024-12-16 -> ['560(todo),523(todo)', '304,853,325', '33,81']
32+
2024-12-15 -> ['560(todo),523(todo)', '370(todo),1109(todo)']
3033
2024-12-14 -> ['253', '53,210,207', '163,1048']
3134
2024-12-13 -> ['304,853,325', '34,767', '776,31', '444', '298,729']
3235
2024-12-12 -> ['004(todo),34(todo),162(todo),275(todo)', '1188,130,855(again)', '1146']

doc/pic/presum_mod.png

33.3 KB
Loading
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
package LeetCodeJava.HashTable;
2+
3+
// https://leetcode.com/problems/continuous-subarray-sum/description/
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
/**
9+
* 523. Continuous Subarray Sum
10+
* Solved
11+
* Medium
12+
* Topics
13+
* Companies
14+
* Given an integer array nums and an integer k, return true if nums has a good subarray or false otherwise.
15+
*
16+
* A good subarray is a subarray where:
17+
*
18+
* its length is at least two, and
19+
* the sum of the elements of the subarray is a multiple of k.
20+
* Note that:
21+
*
22+
* A subarray is a contiguous part of the array.
23+
* An integer x is a multiple of k if there exists an integer n such that x = n * k. 0 is always a multiple of k.
24+
*
25+
*
26+
* Example 1:
27+
*
28+
* Input: nums = [23,2,4,6,7], k = 6
29+
* Output: true
30+
* Explanation: [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
31+
* Example 2:
32+
*
33+
* Input: nums = [23,2,6,4,7], k = 6
34+
* Output: true
35+
* Explanation: [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
36+
* 42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
37+
* Example 3:
38+
*
39+
* Input: nums = [23,2,6,4,7], k = 13
40+
* Output: false
41+
*
42+
*
43+
* Constraints:
44+
*
45+
* 1 <= nums.length <= 105
46+
* 0 <= nums[i] <= 109
47+
* 0 <= sum(nums[i]) <= 231 - 1
48+
* 1 <= k <= 231 - 1
49+
* Seen this question in a real interview before?
50+
* 1/5
51+
* Yes
52+
* No
53+
* Accepted
54+
* 625K
55+
* Submissions
56+
* 2M
57+
* Acceptance Rate
58+
* 30.5%
59+
*/
60+
public class ContinuousSubarraySum {
61+
62+
// V0
63+
// IDEA : HASH MAP (fixed by gpt)
64+
public boolean checkSubarraySum(int[] nums, int k) {
65+
if (nums.length < 2) {
66+
return false;
67+
}
68+
69+
// Map to store the remainder and its index
70+
Map<Integer, Integer> map = new HashMap<>();
71+
// Initialize with remainder 0 at index -1 to handle edge cases
72+
map.put(0, -1);
73+
74+
int presum = 0;
75+
for (int i = 0; i < nums.length; i++) {
76+
presum += nums[i];
77+
78+
// Calculate remainder
79+
int remainder = (k != 0) ? presum % k : presum;
80+
81+
// If the remainder already exists in the map
82+
if (map.containsKey(remainder)) {
83+
// Check if the subarray length is at least 2
84+
if (i - map.get(remainder) > 1) {
85+
return true;
86+
}
87+
} else {
88+
// Store the first occurrence of this remainder
89+
map.put(remainder, i);
90+
}
91+
}
92+
93+
return false;
94+
}
95+
96+
// V1
97+
// IDEA : HASHMAP
98+
// https://leetcode.com/problems/continuous-subarray-sum/editorial/
99+
public boolean checkSubarraySum_1(int[] nums, int k) {
100+
int prefixMod = 0;
101+
HashMap<Integer, Integer> modSeen = new HashMap<>();
102+
modSeen.put(0, -1);
103+
104+
for (int i = 0; i < nums.length; i++) {
105+
/**
106+
* NOTE !!! we get `mod of prefixSum`, instead of get prefixSum
107+
*/
108+
prefixMod = (prefixMod + nums[i]) % k;
109+
110+
if (modSeen.containsKey(prefixMod)) {
111+
// ensures that the size of subarray is at least 2
112+
if (i - modSeen.get(prefixMod) > 1) {
113+
return true;
114+
}
115+
} else {
116+
// mark the value of prefixMod with the current index.
117+
modSeen.put(prefixMod, i);
118+
}
119+
}
120+
121+
return false;
122+
}
123+
124+
125+
// V2
126+
// IDEA : HASHMAP
127+
// https://leetcode.com/problems/continuous-subarray-sum/solutions/1405425/a-java-implementation-of-prefix-sum-that-si0m/
128+
public boolean checkSubarraySum_2(int[] nums, int k) {
129+
// maintain a hash map to store <sum % k, index>
130+
Map<Integer, Integer> map = new HashMap<>();
131+
int sum = 0;
132+
for (int i = 0; i < nums.length; i++) {
133+
sum += nums[i];
134+
sum %= k;
135+
// case 1
136+
if (sum == 0 && i > 0) {
137+
return true;
138+
}
139+
// case 2
140+
if (map.containsKey(sum) && i - map.get(sum) > 1) {
141+
return true;
142+
}
143+
if (!map.containsKey(sum)) {
144+
map.put(sum, i);
145+
}
146+
147+
}
148+
return false;
149+
}
150+
}

leetcode_java/src/main/java/dev/workspace5.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4403,6 +4403,52 @@ public int maxSubArrayLen(int[] nums, int k) {
44034403
return maxSize;
44044404
}
44054405

4406+
// LC 523
4407+
// https://leetcode.com/problems/continuous-subarray-sum/
4408+
// 9.24 am - 9.40 am
4409+
/**
4410+
*
4411+
* Given an integer array nums and an integer k,
4412+
* return true if nums has a good subarray or false otherwise.
4413+
*
4414+
* A good subarray is a subarray where:
4415+
*
4416+
* its length is at least two, and
4417+
* the sum of the elements of the subarray is a multiple of k.
4418+
*
4419+
*/
4420+
public boolean checkSubarraySum(int[] nums, int k) {
4421+
if (nums.length < 2){
4422+
return false;
4423+
}
4424+
// hash map
4425+
// record presum and idx
4426+
// map(presum: idx)
4427+
Map<Integer, Integer> map = new HashMap<>();
4428+
int presum = 0;
4429+
for (int i = 0; i < nums.length; i++){
4430+
int cur = nums[i];
4431+
// if there is any element equals 0, return true directly
4432+
// if (cur == 0){
4433+
// return true;
4434+
// }
4435+
presum += cur;
4436+
map.putIfAbsent(presum, i);
4437+
/**
4438+
* sum(i,j) = presum(j+1) - presum(i)
4439+
*
4440+
* k = presum(j+1) - presum(i)
4441+
* -> presum(i) = presum(j+1) - k
4442+
*/
4443+
if (map.containsKey(presum - k) || presum % k == 0){
4444+
return true;
4445+
}
4446+
4447+
}
4448+
4449+
return false;
4450+
}
4451+
44064452

44074453

44084454
}

0 commit comments

Comments
 (0)