Skip to content

Commit 0cb26d8

Browse files
committed
Add 560. Subarray Sum Equals K
1 parent acb6410 commit 0cb26d8

3 files changed

Lines changed: 59 additions & 30 deletions

File tree

README.md

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,34 @@ Computer Science problems in Java.
1313

1414
1. LLD of chess
1515

16+
### Patterns
17+
18+
1. Two Pointers
19+
2. Sliding Window
20+
3. Prefix Sums
21+
4. Merge Intervals
22+
5. Binary Search (and Variants)
23+
6. Sorting-Based Patterns
24+
7. Fast and Slow Pointers
25+
8. Backtracking & Recursive Search
26+
9. Divide and Conquer
27+
10. Linked List Techniques (Dummy Node, In-place Reversal)
28+
11. Stacks and Queues
29+
12. Monotonic Stack / Queue
30+
13. Expression Evaluation (Two Stacks)
31+
14. String Manipulation & Regular Expressions
32+
15. Hashmaps & Frequency Counting
33+
16. Binary Trees & BSTs (Traversal, Construction, Properties)
34+
17. Path Sum & Root-to-Leaf Techniques
35+
18. Kth Largest/Smallest Elements (Heaps / QuickSelect)
36+
19. Top K Frequent Elements
37+
20. Merge K Sorted Lists
38+
21. Dynamic Programming (Including Knapsack, Range DP, etc.)
39+
22. Greedy & Interval Partitioning
40+
23. Graph Traversals (BFS, DFS)
41+
24. Graph Algorithms (DAGs, MSTs, Shortest Paths, etc.)
42+
25. Design Problems (LRU Cache, Twitter, etc.)
43+
1644
## Question Bank
1745

1846
**𝗔𝗿𝗿𝗮𝘆𝘀 𝗮𝗻𝗱 𝗦𝘁𝗿𝗶𝗻𝗴𝘀:**
@@ -267,34 +295,6 @@ Computer Science problems in Java.
267295
- Efficient for problems with optimal substructure
268296
- Covers tasks like activity selection, minimum coins
269297

270-
### More patterns
271-
272-
1. Two Pointers
273-
2. Sliding Window
274-
3. Prefix Sums
275-
4. Merge Intervals
276-
5. Binary Search (and Variants)
277-
6. Sorting-Based Patterns
278-
7. Fast and Slow Pointers
279-
8. Backtracking & Recursive Search
280-
9. Divide and Conquer
281-
10. Linked List Techniques (Dummy Node, In-place Reversal)
282-
11. Stacks and Queues
283-
12. Monotonic Stack / Queue
284-
13. Expression Evaluation (Two Stacks)
285-
14. String Manipulation & Regular Expressions
286-
15. Hashmaps & Frequency Counting
287-
16. Binary Trees & BSTs (Traversal, Construction, Properties)
288-
17. Path Sum & Root-to-Leaf Techniques
289-
18. Kth Largest/Smallest Elements (Heaps / QuickSelect)
290-
19. Top K Frequent Elements
291-
20. Merge K Sorted Lists
292-
21. Dynamic Programming (Including Knapsack, Range DP, etc.)
293-
22. Greedy & Interval Partitioning
294-
23. Graph Traversals (BFS, DFS)
295-
24. Graph Algorithms (DAGs, MSTs, Shortest Paths, etc.)
296-
25. Design Problems (LRU Cache, Twitter, etc.)
297-
298298
## Roadmap:
299299

300300
✅Build a strong foundation (WEEKS 1-4)

src/main/java/org/dojo/leetcode/ArrayProblems.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -627,8 +627,8 @@ public List<Integer> spiralOrder(int[][] matrix) {
627627

628628
int top = 0, bottom = m - 1, left = 0, right = n - 1;
629629

630-
while(list.size() != m * n) {
631-
for (int c = left; c < right; c++) list.add(matrix[top][c]);
630+
while (list.size() != m * n) {
631+
for (int c = left; c < right; c++) list.add(matrix[top][c]);
632632
for (int r = top; r <= bottom; r++) list.add(matrix[r][right]);
633633
if (top == bottom) break;
634634
for (int c = right - 1; c >= left; c--) list.add(matrix[bottom][c]);
@@ -643,4 +643,19 @@ public List<Integer> spiralOrder(int[][] matrix) {
643643

644644
return list;
645645
}
646+
647+
public int subarraySum(int[] nums, int k) {
648+
int prefixSum = 0;
649+
int count = 0;
650+
651+
Map<Integer, Integer> sums = new HashMap<>();
652+
sums.put(prefixSum, 1);
653+
for (int num : nums) {
654+
prefixSum += num;
655+
int prevSum = prefixSum - k;
656+
if (sums.containsKey(prevSum)) count += sums.get(prevSum);
657+
sums.merge(prefixSum, 1, Integer::sum);
658+
}
659+
return count;
660+
}
646661
}

src/test/java/org/dojo/leetcode/ArrayProblemsTests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,4 +418,18 @@ void spiralOrder(int[][] matrix, List<Integer> expected) {
418418
System.out.printf("Expected: %s, Actual: %s%n", expected, actual);
419419
assertEquals(expected, actual);
420420
}
421+
422+
static Stream<Arguments> subarraySumData() {
423+
return Stream.of(
424+
Arguments.of(new int[] {1, 1, 1}, 2, 2),
425+
Arguments.of(new int[] {1, 2, 3}, 3, 2)
426+
);
427+
}
428+
@ParameterizedTest
429+
@MethodSource("subarraySumData")
430+
void subarraySum(int[] nums, int k, int expected) {
431+
int actual = sut.subarraySum(nums, k);
432+
System.out.printf("Expected: %d, Actual: %d%n", expected, actual);
433+
assertEquals(expected, actual);
434+
}
421435
}

0 commit comments

Comments
 (0)