Skip to content

Commit 66beb60

Browse files
add 1524
1 parent b5156d7 commit 66beb60

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1524|[Number of Sub-arrays With Odd Sum](https://leetcode.com/problems/number-of-sub-arrays-with-odd-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1524.java) | |Medium|Array, Math|
1112
|1523|[Count Odd Numbers in an Interval Range](https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1523.java) | |Easy|Math|
1213
|1518|[Water Bottles](https://leetcode.com/problems/water-bottles/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1518.java) | |Easy|Greedy|
1314
|1514|[Path with Maximum Probability](https://leetcode.com/problems/path-with-maximum-probability/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1514.java) | |Medium|Graph|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _1524 {
4+
public static class Solution1 {
5+
/**
6+
* This brute force solution will throw exceed time limit exceeded exception on LeetCode.
7+
*/
8+
public int numOfSubarrays(int[] arr) {
9+
long oddCount = 0;
10+
for (int i = 0; i < arr.length; i++) {
11+
long subTotal = 0;
12+
for (int j = i; j < arr.length; j++) {
13+
subTotal += arr[j];
14+
if (subTotal % 2 != 0) {
15+
oddCount++;
16+
}
17+
}
18+
}
19+
return (int) oddCount % 1000000007;
20+
}
21+
}
22+
23+
public static class Solution2 {
24+
public int numOfSubarrays(int[] arr) {
25+
int odd = 0;
26+
int even = 1;
27+
long count = 0;
28+
int sum = 0;
29+
for (int num : arr) {
30+
sum += num;
31+
if (sum % 2 == 0) {
32+
count += odd;
33+
} else {
34+
count += even;
35+
}
36+
if (sum % 2 == 0) {
37+
even++;
38+
} else {
39+
odd++;
40+
}
41+
count %= 1000000007;
42+
}
43+
return (int) count % 1000000007;
44+
}
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1524;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static junit.framework.TestCase.assertEquals;
8+
9+
public class _1524Test {
10+
private static _1524.Solution1 solution1;
11+
private static _1524.Solution2 solution2;
12+
private static int[] arr;
13+
14+
@BeforeClass
15+
public static void setup() {
16+
solution1 = new _1524.Solution1();
17+
solution2 = new _1524.Solution2();
18+
}
19+
20+
@Test
21+
public void test1() {
22+
arr = new int[]{1, 3, 5};
23+
assertEquals(4, solution1.numOfSubarrays(arr));
24+
}
25+
26+
@Test
27+
public void test2() {
28+
arr = new int[]{2, 4, 6};
29+
assertEquals(0, solution1.numOfSubarrays(arr));
30+
}
31+
32+
@Test
33+
public void test4() {
34+
arr = new int[]{1, 3, 5};
35+
assertEquals(4, solution2.numOfSubarrays(arr));
36+
}
37+
38+
@Test
39+
public void test5() {
40+
arr = new int[]{2, 4, 6};
41+
assertEquals(0, solution2.numOfSubarrays(arr));
42+
}
43+
44+
}

0 commit comments

Comments
 (0)