Skip to content

Commit

Permalink
add 523 java
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Dec 14, 2024
1 parent c10e7bf commit e9c6d07
Show file tree
Hide file tree
Showing 6 changed files with 208 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@
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)
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)
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)
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)
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)
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)
Expand Down
3 changes: 2 additions & 1 deletion data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
20241208: 304,853,325,560(todo)
20241214: 560(todo),523(todo)
20241208: 304,853,325
20241202: 370(todo),1109(todo)
20241130: 34,767
20241126: 722,380
Expand Down
15 changes: 9 additions & 6 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
2025-02-07 -> ['560(todo),523(todo)']
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 -> ['253']
2025-01-17 -> ['560(todo),523(todo)', '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 -> ['53,210,207']
2025-01-04 -> ['560(todo),523(todo)', '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 -> ['253', '26,27', '802,1197,26']
2024-12-27 -> ['560(todo),523(todo)', '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-21 -> ['304,853,325', '34,767', '394', '855,846']
2024-12-20 -> ['833,950', '932']
2024-12-19 -> ['560(todo),523(todo)']
2024-12-18 -> ['951,792']
2024-12-17 -> ['722,380']
2024-12-16 -> ['304,853,325', '33,81']
2024-12-15 -> ['370(todo),1109(todo)']
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-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
Binary file added doc/pic/presum_mod.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package LeetCodeJava.HashTable;

// https://leetcode.com/problems/continuous-subarray-sum/description/

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

/**
* 523. Continuous Subarray Sum
* Solved
* Medium
* Topics
* Companies
* Given an integer array nums and an integer k, return true if nums has a good subarray or false otherwise.
*
* A good subarray is a subarray where:
*
* its length is at least two, and
* the sum of the elements of the subarray is a multiple of k.
* Note that:
*
* A subarray is a contiguous part of the array.
* 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.
*
*
* Example 1:
*
* Input: nums = [23,2,4,6,7], k = 6
* Output: true
* Explanation: [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
* Example 2:
*
* Input: nums = [23,2,6,4,7], k = 6
* Output: true
* Explanation: [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
* 42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
* Example 3:
*
* Input: nums = [23,2,6,4,7], k = 13
* Output: false
*
*
* Constraints:
*
* 1 <= nums.length <= 105
* 0 <= nums[i] <= 109
* 0 <= sum(nums[i]) <= 231 - 1
* 1 <= k <= 231 - 1
* Seen this question in a real interview before?
* 1/5
* Yes
* No
* Accepted
* 625K
* Submissions
* 2M
* Acceptance Rate
* 30.5%
*/
public class ContinuousSubarraySum {

// V0
// IDEA : HASH MAP (fixed by gpt)
public boolean checkSubarraySum(int[] nums, int k) {
if (nums.length < 2) {
return false;
}

// Map to store the remainder and its index
Map<Integer, Integer> map = new HashMap<>();
// Initialize with remainder 0 at index -1 to handle edge cases
map.put(0, -1);

int presum = 0;
for (int i = 0; i < nums.length; i++) {
presum += nums[i];

// Calculate remainder
int remainder = (k != 0) ? presum % k : presum;

// If the remainder already exists in the map
if (map.containsKey(remainder)) {
// Check if the subarray length is at least 2
if (i - map.get(remainder) > 1) {
return true;
}
} else {
// Store the first occurrence of this remainder
map.put(remainder, i);
}
}

return false;
}

// V1
// IDEA : HASHMAP
// https://leetcode.com/problems/continuous-subarray-sum/editorial/
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;
}


// V2
// IDEA : HASHMAP
// https://leetcode.com/problems/continuous-subarray-sum/solutions/1405425/a-java-implementation-of-prefix-sum-that-si0m/
public boolean checkSubarraySum_2(int[] nums, int k) {
// maintain a hash map to store <sum % k, index>
Map<Integer, Integer> map = new HashMap<>();
int sum = 0;
for (int i = 0; i < nums.length; i++) {
sum += nums[i];
sum %= k;
// case 1
if (sum == 0 && i > 0) {
return true;
}
// case 2
if (map.containsKey(sum) && i - map.get(sum) > 1) {
return true;
}
if (!map.containsKey(sum)) {
map.put(sum, i);
}

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

// LC 523
// https://leetcode.com/problems/continuous-subarray-sum/
// 9.24 am - 9.40 am
/**
*
* Given an integer array nums and an integer k,
* return true if nums has a good subarray or false otherwise.
*
* A good subarray is a subarray where:
*
* its length is at least two, and
* the sum of the elements of the subarray is a multiple of k.
*
*/
public boolean checkSubarraySum(int[] nums, int k) {
if (nums.length < 2){
return false;
}
// hash map
// record presum and idx
// map(presum: idx)
Map<Integer, Integer> map = new HashMap<>();
int presum = 0;
for (int i = 0; i < nums.length; i++){
int cur = nums[i];
// if there is any element equals 0, return true directly
// if (cur == 0){
// return true;
// }
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) || presum % k == 0){
return true;
}

}

return false;
}



}
Expand Down

0 comments on commit e9c6d07

Please sign in to comment.