Skip to content

Commit 74feddd

Browse files
edit 523
1 parent 1edd764 commit 74feddd

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ Your ideas/fixes/algorithms are more than welcome!
110110
|526|[Beautiful Arrangement](https://leetcode.com/problems/beautiful-arrangement/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_526.java) | O(n) |O(h) | Medium | Backtracking
111111
|525|[Contiguous Array](https://leetcode.com/problems/contiguous-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_525.java) | O(n) |O(n) | Medium | HashMap
112112
|524|[Longest Word in Dictionary through Deleting](https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_524.java) | O(n) |O(n) | Medium | Sort
113-
|523|[Continuous Subarray Sum](https://leetcode.com/problems/continuous-subarray-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_523.java) | O(n^2) |O(n) | Medium|
113+
|523|[Continuous Subarray Sum](https://leetcode.com/problems/continuous-subarray-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_523.java) | O(n) |O(1) | Medium| DP
114114
|522|[Longest Uncommon Subsequence II](https://leetcode.com/problems/longest-uncommon-subsequence-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_522.java) | O(x*n^2) (x is average length of strings)|O(1) | Medium|
115115
|521|[Longest Uncommon Subsequence I](https://leetcode.com/problems/longest-uncommon-subsequence-i/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_521.java) | O(max(x,y)) (x and y are length of strings) |O(1) | Easy|
116116
|520|[Detect Capital](https://leetcode.com/problems/detect-capital/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_520.java) | O(n) |O(1) | Easy|

src/main/java/com/fishercoder/solutions/_523.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package com.fishercoder.solutions;
22

3+
import java.util.HashMap;
4+
import java.util.Map;
5+
36
/**
7+
* 523. Continuous Subarray Sum
8+
*
49
* Given a list of non-negative numbers and a target integer k,
510
* write a function to check if the array has a continuous subarray of size at least 2
611
* that sums up to the multiple of k, that is, sums up to n*k where n is also an integer.
@@ -21,7 +26,29 @@
2126
2227
*/
2328
public class _523 {
24-
/**TODO: could be optimized to O(n) time and O(k) space, reference: https://discuss.leetcode.com/topic/80793/java-o-n-time-o-k-space*/
29+
30+
public boolean checkSubarraySumOnTimeO1Space(int[] nums, int k) {
31+
Map<Integer, Integer> map = new HashMap<>();
32+
map.put(0, -1);
33+
int sum = 0;
34+
for (int i = 0; i < nums.length; i++) {
35+
sum += nums[i];
36+
if (k != 0) {
37+
/**Because if k == 0, sum %= k will throw ArithmeticException.*/
38+
sum %= k;
39+
}
40+
Integer prev = map.get(sum);
41+
if (prev != null) {
42+
if (i - prev > 1) {
43+
/**This makes sure that it has length at least 2*/
44+
return true;
45+
}
46+
} else {
47+
map.put(sum, i);
48+
}
49+
}
50+
return false;
51+
}
2552

2653
public boolean checkSubarraySum(int[] nums, int k) {
2754
if (nums == null || nums.length == 0) return false;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy