Skip to content

Commit 4edafe4

Browse files
committed
560_Subarray_Sum_Equals_K
1 parent 84d9638 commit 4edafe4

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ Also, there are open source implementations for basic data structs and algorithm
152152
| 538 | [Convert BST to Greater Tree](https://leetcode.com/problems/convert-bst-to-greater-tree/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/538_Convert_BST_to_Greater_Tree.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/538_Convert_BST_to_Greater_Tree.java) | Right first DFS with a variable recording sum of node.val and right.val. 1. Recursive.<br>2. Stack 3. Reverse Morris In-order Traversal |
153153
| 543 | [Diameter of Binary Tree](https://leetcode.com/problems/diameter-of-binary-tree/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/543_Diameter_of_Binary_Tree.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/543_Diameter_of_Binary_Tree.java) | DFS with O(1) for max answer |
154154
| 557 | [Reverse Words in a String III](https://leetcode.com/problems/reverse-words-in-a-string-iii/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/557_Reverse_Words_in_a_String_III.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/557_Reverse_Words_in_a_String_III.java) | String handle: Split with space than reverse word, O(n) and O(n). Better solution is that reverse can be O(1) space in array. |
155+
| 560 | [Subarray Sum Equals K](https://leetcode.com/problems/subarray-sum-equals-k/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/560_Subarray_Sum_Equals_K.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/560_Subarray_Sum_Equals_K.java) | Note that there are n^2 possible pairs, so the key point is accelerate computation for sum and reduce unnecessary pair. 1. Cummulative sum, O(n^2) and O(1)/O(n)<br>2. Add sum into hash, check if sum - k is in hash, O(n) and O(n) |
155156
| 572 | [Subtree of Another Tree](https://leetcode.com/problems/subtree-of-another-tree/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/572_Subtree_of_Another_Tree.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/572_Subtree_of_Another_Tree.java) | 1. Tree traverse and compare<br>2. Tree to string and compare |
156157
| 581 | [Shortest Unsorted Continuous Subarray](https://leetcode.com/problems/subtree-of-another-tree/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/581_Shortest_Unsorted_Continuous_Subarray.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/581_Shortest_Unsorted_Continuous_Subarray.java) | 1. Sort and find the difference (min and max), O(nlgn)<br>2. Using stack to find boundaries (push when correct order, pop when not correct), O(n) and O(n)<br>3. Find min and max of unordered array, O(n) and O(1)|
157158
| 605 | [Can Place Flowers](https://leetcode.com/problems/can-place-flowers/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/605_Can_Place_Flowers.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/605_Can_Place_Flowers.java) | One time scan, check [i-1] [i] and [i+1], O(n) and O(1) |

java/560_Subarray_Sum_Equals_K.java

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
public class Solution {
2+
/*public int subarraySum(int[] nums, int k) {
3+
int count = 0;
4+
for (int start = 0; start < nums.length; start++) {
5+
int sum = 0;
6+
for (int end = start; end < nums.length; end++) {
7+
sum += nums[end];
8+
if (sum == k)
9+
count++;
10+
}
11+
}
12+
return count;
13+
}*/
14+
public int subarraySum(int[] nums, int k) {
15+
int count = 0, sum = 0;
16+
HashMap < Integer, Integer > map = new HashMap < > ();
17+
map.put(0, 1);
18+
for (int i = 0; i < nums.length; i++) {
19+
sum += nums[i];
20+
// check if sum - k in hash
21+
if (map.containsKey(sum - k))
22+
count += map.get(sum - k);
23+
// push sum into hash
24+
map.put(sum, map.getOrDefault(sum, 0) + 1);
25+
}
26+
return count;
27+
}
28+
}

python/560_Subarray_Sum_Equals_K.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution(object):
2+
def subarraySum(self, nums, k):
3+
"""
4+
:type nums: List[int]
5+
:type k: int
6+
:rtype: int
7+
"""
8+
sum_map = {}
9+
sum_map[0] = 1
10+
count = curr_sum = 0
11+
for num in nums:
12+
curr_sum += num
13+
# Check if sum - k in hash
14+
count += sum_map.get(curr_sum - k, 0)
15+
# add curr_sum to hash
16+
sum_map[curr_sum] = sum_map.get(curr_sum, 0) + 1
17+
return count

0 commit comments

Comments
 (0)