Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 17f87bc

Browse files
add 1567
1 parent 365b5b6 commit 17f87bc

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
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+
|1567|[Maximum Length of Subarray With Positive Product](https://leetcode.com/problems/maximum-length-of-subarray-with-positive-product/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1567.java) ||Greedy|Medium|
1112
|1566|[Detect Pattern of Length M Repeated K or More Times](https://leetcode.com/problems/detect-pattern-of-length-m-repeated-k-or-more-times/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1566.java) ||Array|Easy|
1213
|1561|[Maximum Number of Coins You Can Get](https://leetcode.com/problems/maximum-number-of-coins-you-can-get/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1561.java) |[:tv:](https://youtu.be/hPe9Z3TiUrA)|Sort|Medium|
1314
|1560|[Most Visited Sector in a Circular Track](https://leetcode.com/problems/most-visited-sector-in-a-circular-track/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1560.java) ||Array|Easy|
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _1567 {
4+
public static class Solution1 {
5+
public int getMaxLen(int[] nums) {
6+
int max = 0;
7+
for (int i = 0; i < nums.length; i++) {
8+
if (nums.length - i <= max) {
9+
return max;
10+
}
11+
if (nums[i] != 0) {
12+
int negatives = nums[i] > 0 ? 0 : 1;
13+
max = Math.max(max, nums[i] > 0 ? 1 : 0);
14+
for (int j = i + 1; j < nums.length; j++) {
15+
if (nums[j] < 0) {
16+
negatives++;
17+
} else if (nums[j] == 0) {
18+
break;
19+
}
20+
if (negatives % 2 == 0) {
21+
max = Math.max(max, j - i + 1);
22+
}
23+
}
24+
}
25+
}
26+
return max;
27+
}
28+
}
29+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1567;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static junit.framework.TestCase.assertEquals;
8+
9+
public class _1567Test {
10+
private static _1567.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1567.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(4, solution1.getMaxLen(new int[]{1, -2, -3, 4}));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(3, solution1.getMaxLen(new int[]{0, 1, -2, -3, -4}));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(2, solution1.getMaxLen(new int[]{-1, -2, -3, 0, 1}));
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals(1, solution1.getMaxLen(new int[]{-1, 2}));
35+
}
36+
37+
@Test
38+
public void test5() {
39+
assertEquals(4, solution1.getMaxLen(new int[]{1, 2, 3, 5, -6, 4, 0, 10}));
40+
}
41+
42+
@Test
43+
public void test6() {
44+
assertEquals(0, solution1.getMaxLen(new int[]{-1}));
45+
}
46+
47+
}

0 commit comments

Comments
 (0)