Skip to content

Commit c8e1297

Browse files
update 560
1 parent 9d1d91f commit c8e1297

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

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

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,47 +14,51 @@ public static class Solution1 {
1414
* To achieve this, we just need to go through the array,
1515
* calculate the current sum and save number of all seen PreSum to a HashMap.
1616
* <p>
17-
* Time complexity O(n), Space complexity O(n).
17+
* Time complexity: O(n);
18+
* Space complexity: O(n).
1819
*/
1920
public int subarraySum(int[] nums, int k) {
20-
Map<Integer, Integer> preSum = new HashMap();
21+
Map<Integer, Integer> preSumFrequencyMap = new HashMap();
2122
int sum = 0;
22-
int result = 0;
23-
preSum.put(0, 1);
23+
int count = 0;
24+
preSumFrequencyMap.put(0, 1);
2425
for (int i = 0; i < nums.length; i++) {
2526
sum += nums[i];
26-
if (preSum.containsKey(sum - k)) {
27-
result += preSum.get(sum - k);
27+
if (preSumFrequencyMap.containsKey(sum - k)) {
28+
count += preSumFrequencyMap.get(sum - k);
2829
}
29-
preSum.put(sum, preSum.getOrDefault(sum, 0) + 1);
30+
preSumFrequencyMap.put(sum, preSumFrequencyMap.getOrDefault(sum, 0) + 1);
3031
}
31-
return result;
32+
return count;
3233
}
3334
}
3435

3536
public static class Solution2 {
3637
/**
3738
* My completely original solution on 10/14/2021.
3839
* Again, using a pen and paper to visualize your thought process just clears out all ambiguities.
40+
* <p>
41+
* Time: O(n^2)
42+
* Space: O(n)
3943
*/
4044
public int subarraySum(int[] nums, int k) {
41-
int ans = 0;
45+
int count = 0;
4246
int[] prefixSum = new int[nums.length];
4347
prefixSum[0] = nums[0];
4448
for (int i = 1; i < nums.length; i++) {
4549
prefixSum[i] = prefixSum[i - 1] + nums[i];
4650
}
4751
for (int i = 0; i < nums.length; i++) {
4852
if (prefixSum[i] == k) {
49-
ans++;
53+
count++;
5054
}
5155
for (int j = 0; j < i; j++) {
5256
if (prefixSum[i] - prefixSum[j] == k) {
53-
ans++;
57+
count++;
5458
}
5559
}
5660
}
57-
return ans;
61+
return count;
5862
}
5963
}
6064

src/test/java/com/fishercoder/_560Test.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package com.fishercoder;
22

33
import com.fishercoder.solutions._560;
4-
import org.junit.BeforeClass;
5-
import org.junit.Test;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
66

7-
import static org.junit.Assert.assertEquals;
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
88

99
public class _560Test {
1010
private static _560.Solution1 solution1;
@@ -14,8 +14,8 @@ public class _560Test {
1414
private static int[] nums;
1515
private static int k;
1616

17-
@BeforeClass
18-
public static void setup() {
17+
@BeforeEach
18+
public void setup() {
1919
solution1 = new _560.Solution1();
2020
solution2 = new _560.Solution2();
2121
}

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