Skip to content

Commit

Permalink
add 560 java
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Dec 14, 2024
1 parent bb175ab commit b26215c
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@
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)
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)
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)
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)
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)
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
575| [Distribute Candies](https://leetcode.com/problems/distribute-candies/) | [Python ](./leetcode_python/Hash_table/distribute-candies.py) | _O(n)_ | _O(n)_ | Easy | | OK
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)
Expand Down
2 changes: 1 addition & 1 deletion data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
20241214: 560(todo),523(todo)
20241214: 560,523
20241208: 304,853,325
20241202: 370(todo),1109(todo)
20241130: 34,767
Expand Down
18 changes: 9 additions & 9 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
2025-02-07 -> ['560(todo),523(todo)']
2025-02-07 -> ['560,523']
2025-02-01 -> ['304,853,325']
2025-01-26 -> ['370(todo),1109(todo)']
2025-01-24 -> ['34,767']
2025-01-20 -> ['722,380']
2025-01-19 -> ['33,81']
2025-01-17 -> ['560(todo),523(todo)', '253']
2025-01-17 -> ['560,523', '253']
2025-01-16 -> ['776,31']
2025-01-15 -> ['004(todo),34(todo),162(todo),275(todo)']
2025-01-14 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2025-01-11 -> ['304,853,325', '394']
2025-01-10 -> ['833,950']
2025-01-05 -> ['370(todo),1109(todo)']
2025-01-04 -> ['560(todo),523(todo)', '53,210,207']
2025-01-04 -> ['560,523', '53,210,207']
2025-01-03 -> ['34,767', '444']
2025-01-02 -> ['1188,130,855(again)']
2024-12-30 -> ['722,380']
2024-12-29 -> ['304,853,325', '33,81']
2024-12-28 -> ['900']
2024-12-27 -> ['560(todo),523(todo)', '253', '26,27', '802,1197,26']
2024-12-27 -> ['560,523', '253', '26,27', '802,1197,26']
2024-12-26 -> ['776,31']
2024-12-25 -> ['004(todo),34(todo),162(todo),275(todo)']
2024-12-24 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2024-12-23 -> ['370(todo),1109(todo)']
2024-12-22 -> ['560(todo),523(todo)']
2024-12-22 -> ['560,523']
2024-12-21 -> ['304,853,325', '34,767', '394', '855,846']
2024-12-20 -> ['833,950', '932']
2024-12-19 -> ['560(todo),523(todo)']
2024-12-19 -> ['560,523']
2024-12-18 -> ['951,792']
2024-12-17 -> ['560(todo),523(todo)', '722,380']
2024-12-16 -> ['560(todo),523(todo)', '304,853,325', '33,81']
2024-12-15 -> ['560(todo),523(todo)', '370(todo),1109(todo)']
2024-12-17 -> ['560,523', '722,380']
2024-12-16 -> ['560,523', '304,853,325', '33,81']
2024-12-15 -> ['560,523', '370(todo),1109(todo)']
2024-12-14 -> ['253', '53,210,207', '163,1048']
2024-12-13 -> ['304,853,325', '34,767', '776,31', '444', '298,729']
2024-12-12 -> ['004(todo),34(todo),162(todo),275(todo)', '1188,130,855(again)', '1146']
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package LeetCodeJava.HashTable;

// https://leetcode.com/problems/subarray-sum-equals-k/description/

import java.util.HashMap;
import java.util.Map;

/**
* 560. Subarray Sum Equals K
* Solved
* Medium
* Topics
* Companies
* Hint
* Given an array of integers nums and an integer k, return the total number of subarrays whose sum equals to k.
*
* A subarray is a contiguous non-empty sequence of elements within an array.
*
*
*
* Example 1:
*
* Input: nums = [1,1,1], k = 2
* Output: 2
* Example 2:
*
* Input: nums = [1,2,3], k = 3
* Output: 2
*
*
* Constraints:
*
* 1 <= nums.length <= 2 * 104
* -1000 <= nums[i] <= 1000
* -107 <= k <= 107
* Seen this question in a real interview before?
* 1/5
* Yes
* No
* Accepted
* 1.5M
* Submissions
* 3.4M
* Acceptance Rate
* 44.5%
* Topics
*/
public class SubarraySumEqualsK {

// V0
// IDEA : HASH MAP (fixed by gpt)
/**
*
* Explanation of Fixes:
*
* 1. Using presum - k Logic Properly:
* • 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.
*
* 2. Map Initialization:
* • Initializing the map with map.put(0, 1) allows the logic to handle cases where a subarray starting from index 0 equals k .
*
* 3. Updating the Map:
* • 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.
*
* 4. Simplifying Index-Based Logic:
* • Removed unnecessary index-based conditions (map.get(presum - k) == i + 1) which were incorrect and not required for this problem.
*/
public int subarraySum(int[] nums, int k) {
// Map to store prefix sum and its count
Map<Integer, Integer> map = new HashMap<>();
int presum = 0;
int count = 0;

// Initialize the map with prefix sum 0 (to handle subarrays starting at index 0)
map.put(0, 1);

for (int num : nums) {
presum += num;

// Check if there's a prefix sum such that presum - k exists
if (map.containsKey(presum - k)) {
count += map.get(presum - k);
}

// Update the map with the current prefix sum
map.put(presum, map.getOrDefault(presum, 0) + 1);
}

return count;
}

// V1
// IDEA : HASH MAP
// https://leetcode.com/problems/subarray-sum-equals-k/solutions/6143642/java-beats-9983-by-mohamedhazem3-9yj6/
public static int subarraySum_1(int[] nums, int k) {
int[] prefix = new int[nums.length + 1];
int out = 0;
HashMap<Integer, Integer> hm = new HashMap<>();

for (int i = 0; i < nums.length; i++)
prefix[i + 1] = prefix[i] + nums[i]; // Compute prefix sums

for (int i = 0; i < prefix.length; i++) {
if (hm.containsKey(prefix[i] - k)) // Check if required difference exists
out += hm.get(prefix[i] - k); // Add the frequency to the result

// Update HashMap with the current prefix sum
hm.put(prefix[i], hm.getOrDefault(prefix[i], 0) + 1);
}
return out;
}

// V2
// IDEA : HASH MAP
// https://leetcode.com/problems/subarray-sum-equals-k/solutions/3167609/explained-beats-964-using-hashmap-in-jav-0b6o/
public int subarraySum_2(int[] nums, int k) {
int sum = 0;
int ans = 0;
HashMap<Integer,Integer> map = new HashMap<>();
map.put(0,1);
for(int j=0;j<nums.length;j++){
sum += nums[j];
if(map.containsKey(sum -k)){
ans += map.get(sum-k);
}
map.put(sum,map.getOrDefault(sum,0)+1);
}
return ans;
}

// V3
// IDEA : HASH MAP
// https://leetcode.com/problems/subarray-sum-equals-k/solutions/803317/java-solution-with-detailed-explanation-yu2hg/
public int subarraySum_3(int[] nums, int k) {
int count = 0;

int[] sum = new int[nums.length + 1];
sum[0] = 0;
for (int i = 1; i <= nums.length; i++)
sum[i] = sum[i - 1] + nums[i - 1];

for (int start = 0; start < sum.length; start++) {
for (int end = start + 1; end < sum.length; end++) {
if (sum[end] - sum[start] == k)
count++;
}
}
return count;
}

}
40 changes: 40 additions & 0 deletions leetcode_java/src/main/java/dev/workspace5.java
Original file line number Diff line number Diff line change
Expand Up @@ -4449,6 +4449,46 @@ public boolean checkSubarraySum(int[] nums, int k) {
return false;
}

// LC 560
// https://leetcode.com/problems/subarray-sum-equals-k/
// 11.18 AM - 11.40 AM
/**
* Given an array of integers nums and an integer k,
* return the total number of subarrays whose sum equals to k.
*
* A subarray is a contiguous non-empty sequence of elements within an array.
*
*/
public int subarraySum(int[] nums, int k) {

if(nums.length==1){
return nums[0] == k ? 1 : 0;
}

// map : {presum : idx}
Map<Integer, Integer> map = new HashMap<>();
int presum = 0;
int cnt = 0;
map.put(0, -1); // TODO : check if necessary
for (int i = 0; i < nums.length; i++){
int cur = nums[i];
presum += cur;
map.putIfAbsent(presum, i); // ???
/**
* sum(i,j) = presum(j+1) - presum(i)
*
* k = presum(j+1) - presum(i)
* -> presum(i) = presum(j+1) - k
*/
if (map.containsKey(presum - k) && map.get(presum - k) == i+1){
cnt += 1;
}
//map.putIfAbsent(presum, i);
}

return cnt;
}



}
Expand Down

0 comments on commit b26215c

Please sign in to comment.