Skip to content

Commit b26215c

Browse files
committed
add 560 java
1 parent bb175ab commit b26215c

File tree

5 files changed

+202
-11
lines changed

5 files changed

+202
-11
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@
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)
374-
560| [Subarray Sum Equals K](https://leetcode.com/problems/subarray-sum-equals-k/) | [Python ](./leetcode_python/Hash_table/subarray-sum-equals-k.py) | _O(n)_ | _O(n)_ | Medium | `must check`,check `# 523 Continuous Subarray Sum`, LC 1268,`basic`, `substring`,`good trick`,`google`,`fb`| AGAIN************** (6) (MUST)
374+
560| [Subarray Sum Equals K](https://leetcode.com/problems/subarray-sum-equals-k/) | [Python ](./leetcode_python/Hash_table/subarray-sum-equals-k.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/HashTable/SubarraySumEqualsK.java) | _O(n)_ | _O(n)_ | Medium | `must check`,check `# 523 Continuous Subarray Sum`, LC 1268,`basic`, `substring`,`good trick`,`google`,`fb`| AGAIN************** (6) (MUST)
375375
561| [Array Partition I](https://leetcode.com/problems/array-partition-i/) | [Python ](./leetcode_python/Hash_table/array-partition-i.py) | _O(r)_ | _O(r)_ | Easy | | OK
376376
575| [Distribute Candies](https://leetcode.com/problems/distribute-candies/) | [Python ](./leetcode_python/Hash_table/distribute-candies.py) | _O(n)_ | _O(n)_ | Easy | | OK
377377
594| [Longest Harmonious Subsequence](https://leetcode.com/problems/longest-harmonious-subsequence/) | [Python ](./leetcode_python/Hash_table/longest-harmonious-subsequence.py) | _O(n)_ | _O(n)_ | Easy | `basic`, `good trick`| OK* (3)

data/progress.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
20241214: 560(todo),523(todo)
1+
20241214: 560,523
22
20241208: 304,853,325
33
20241202: 370(todo),1109(todo)
44
20241130: 34,767

data/to_review.txt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
1-
2025-02-07 -> ['560(todo),523(todo)']
1+
2025-02-07 -> ['560,523']
22
2025-02-01 -> ['304,853,325']
33
2025-01-26 -> ['370(todo),1109(todo)']
44
2025-01-24 -> ['34,767']
55
2025-01-20 -> ['722,380']
66
2025-01-19 -> ['33,81']
7-
2025-01-17 -> ['560(todo),523(todo)', '253']
7+
2025-01-17 -> ['560,523', '253']
88
2025-01-16 -> ['776,31']
99
2025-01-15 -> ['004(todo),34(todo),162(todo),275(todo)']
1010
2025-01-14 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
1111
2025-01-11 -> ['304,853,325', '394']
1212
2025-01-10 -> ['833,950']
1313
2025-01-05 -> ['370(todo),1109(todo)']
14-
2025-01-04 -> ['560(todo),523(todo)', '53,210,207']
14+
2025-01-04 -> ['560,523', '53,210,207']
1515
2025-01-03 -> ['34,767', '444']
1616
2025-01-02 -> ['1188,130,855(again)']
1717
2024-12-30 -> ['722,380']
1818
2024-12-29 -> ['304,853,325', '33,81']
1919
2024-12-28 -> ['900']
20-
2024-12-27 -> ['560(todo),523(todo)', '253', '26,27', '802,1197,26']
20+
2024-12-27 -> ['560,523', '253', '26,27', '802,1197,26']
2121
2024-12-26 -> ['776,31']
2222
2024-12-25 -> ['004(todo),34(todo),162(todo),275(todo)']
2323
2024-12-24 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2424
2024-12-23 -> ['370(todo),1109(todo)']
25-
2024-12-22 -> ['560(todo),523(todo)']
25+
2024-12-22 -> ['560,523']
2626
2024-12-21 -> ['304,853,325', '34,767', '394', '855,846']
2727
2024-12-20 -> ['833,950', '932']
28-
2024-12-19 -> ['560(todo),523(todo)']
28+
2024-12-19 -> ['560,523']
2929
2024-12-18 -> ['951,792']
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)']
30+
2024-12-17 -> ['560,523', '722,380']
31+
2024-12-16 -> ['560,523', '304,853,325', '33,81']
32+
2024-12-15 -> ['560,523', '370(todo),1109(todo)']
3333
2024-12-14 -> ['253', '53,210,207', '163,1048']
3434
2024-12-13 -> ['304,853,325', '34,767', '776,31', '444', '298,729']
3535
2024-12-12 -> ['004(todo),34(todo),162(todo),275(todo)', '1188,130,855(again)', '1146']
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
package LeetCodeJava.HashTable;
2+
3+
// https://leetcode.com/problems/subarray-sum-equals-k/description/
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
/**
9+
* 560. Subarray Sum Equals K
10+
* Solved
11+
* Medium
12+
* Topics
13+
* Companies
14+
* Hint
15+
* Given an array of integers nums and an integer k, return the total number of subarrays whose sum equals to k.
16+
*
17+
* A subarray is a contiguous non-empty sequence of elements within an array.
18+
*
19+
*
20+
*
21+
* Example 1:
22+
*
23+
* Input: nums = [1,1,1], k = 2
24+
* Output: 2
25+
* Example 2:
26+
*
27+
* Input: nums = [1,2,3], k = 3
28+
* Output: 2
29+
*
30+
*
31+
* Constraints:
32+
*
33+
* 1 <= nums.length <= 2 * 104
34+
* -1000 <= nums[i] <= 1000
35+
* -107 <= k <= 107
36+
* Seen this question in a real interview before?
37+
* 1/5
38+
* Yes
39+
* No
40+
* Accepted
41+
* 1.5M
42+
* Submissions
43+
* 3.4M
44+
* Acceptance Rate
45+
* 44.5%
46+
* Topics
47+
*/
48+
public class SubarraySumEqualsK {
49+
50+
// V0
51+
// IDEA : HASH MAP (fixed by gpt)
52+
/**
53+
*
54+
* Explanation of Fixes:
55+
*
56+
* 1. Using presum - k Logic Properly:
57+
* • If presum - k exists in the map, it means there is a subarray ending at the current index whose sum is k . Add the count of such prefix sums to the total.
58+
*
59+
* 2. Map Initialization:
60+
* • Initializing the map with map.put(0, 1) allows the logic to handle cases where a subarray starting from index 0 equals k .
61+
*
62+
* 3. Updating the Map:
63+
* • Instead of tracking indices, the map stores the count of occurrences of each prefix sum. This allows us to find how many subarrays can be formed using a specific prefix sum.
64+
*
65+
* 4. Simplifying Index-Based Logic:
66+
* • Removed unnecessary index-based conditions (map.get(presum - k) == i + 1) which were incorrect and not required for this problem.
67+
*/
68+
public int subarraySum(int[] nums, int k) {
69+
// Map to store prefix sum and its count
70+
Map<Integer, Integer> map = new HashMap<>();
71+
int presum = 0;
72+
int count = 0;
73+
74+
// Initialize the map with prefix sum 0 (to handle subarrays starting at index 0)
75+
map.put(0, 1);
76+
77+
for (int num : nums) {
78+
presum += num;
79+
80+
// Check if there's a prefix sum such that presum - k exists
81+
if (map.containsKey(presum - k)) {
82+
count += map.get(presum - k);
83+
}
84+
85+
// Update the map with the current prefix sum
86+
map.put(presum, map.getOrDefault(presum, 0) + 1);
87+
}
88+
89+
return count;
90+
}
91+
92+
// V1
93+
// IDEA : HASH MAP
94+
// https://leetcode.com/problems/subarray-sum-equals-k/solutions/6143642/java-beats-9983-by-mohamedhazem3-9yj6/
95+
public static int subarraySum_1(int[] nums, int k) {
96+
int[] prefix = new int[nums.length + 1];
97+
int out = 0;
98+
HashMap<Integer, Integer> hm = new HashMap<>();
99+
100+
for (int i = 0; i < nums.length; i++)
101+
prefix[i + 1] = prefix[i] + nums[i]; // Compute prefix sums
102+
103+
for (int i = 0; i < prefix.length; i++) {
104+
if (hm.containsKey(prefix[i] - k)) // Check if required difference exists
105+
out += hm.get(prefix[i] - k); // Add the frequency to the result
106+
107+
// Update HashMap with the current prefix sum
108+
hm.put(prefix[i], hm.getOrDefault(prefix[i], 0) + 1);
109+
}
110+
return out;
111+
}
112+
113+
// V2
114+
// IDEA : HASH MAP
115+
// https://leetcode.com/problems/subarray-sum-equals-k/solutions/3167609/explained-beats-964-using-hashmap-in-jav-0b6o/
116+
public int subarraySum_2(int[] nums, int k) {
117+
int sum = 0;
118+
int ans = 0;
119+
HashMap<Integer,Integer> map = new HashMap<>();
120+
map.put(0,1);
121+
for(int j=0;j<nums.length;j++){
122+
sum += nums[j];
123+
if(map.containsKey(sum -k)){
124+
ans += map.get(sum-k);
125+
}
126+
map.put(sum,map.getOrDefault(sum,0)+1);
127+
}
128+
return ans;
129+
}
130+
131+
// V3
132+
// IDEA : HASH MAP
133+
// https://leetcode.com/problems/subarray-sum-equals-k/solutions/803317/java-solution-with-detailed-explanation-yu2hg/
134+
public int subarraySum_3(int[] nums, int k) {
135+
int count = 0;
136+
137+
int[] sum = new int[nums.length + 1];
138+
sum[0] = 0;
139+
for (int i = 1; i <= nums.length; i++)
140+
sum[i] = sum[i - 1] + nums[i - 1];
141+
142+
for (int start = 0; start < sum.length; start++) {
143+
for (int end = start + 1; end < sum.length; end++) {
144+
if (sum[end] - sum[start] == k)
145+
count++;
146+
}
147+
}
148+
return count;
149+
}
150+
151+
}

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4449,6 +4449,46 @@ public boolean checkSubarraySum(int[] nums, int k) {
44494449
return false;
44504450
}
44514451

4452+
// LC 560
4453+
// https://leetcode.com/problems/subarray-sum-equals-k/
4454+
// 11.18 AM - 11.40 AM
4455+
/**
4456+
* Given an array of integers nums and an integer k,
4457+
* return the total number of subarrays whose sum equals to k.
4458+
*
4459+
* A subarray is a contiguous non-empty sequence of elements within an array.
4460+
*
4461+
*/
4462+
public int subarraySum(int[] nums, int k) {
4463+
4464+
if(nums.length==1){
4465+
return nums[0] == k ? 1 : 0;
4466+
}
4467+
4468+
// map : {presum : idx}
4469+
Map<Integer, Integer> map = new HashMap<>();
4470+
int presum = 0;
4471+
int cnt = 0;
4472+
map.put(0, -1); // TODO : check if necessary
4473+
for (int i = 0; i < nums.length; i++){
4474+
int cur = nums[i];
4475+
presum += cur;
4476+
map.putIfAbsent(presum, i); // ???
4477+
/**
4478+
* sum(i,j) = presum(j+1) - presum(i)
4479+
*
4480+
* k = presum(j+1) - presum(i)
4481+
* -> presum(i) = presum(j+1) - k
4482+
*/
4483+
if (map.containsKey(presum - k) && map.get(presum - k) == i+1){
4484+
cnt += 1;
4485+
}
4486+
//map.putIfAbsent(presum, i);
4487+
}
4488+
4489+
return cnt;
4490+
}
4491+
44524492

44534493

44544494
}

0 commit comments

Comments
 (0)