Skip to content

Commit e7014fc

Browse files
add 1535
1 parent 7a587ce commit e7014fc

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-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+
|1535|[Find the Winner of an Array Game](https://leetcode.com/problems/find-the-winner-of-an-array-game/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1535.java) | |Medium|Array|
1112
|1534|[Count Good Triplets](https://leetcode.com/problems/count-good-triplets/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1534.java) | |Easy|Array|
1213
|1528|[Shuffle String](https://leetcode.com/problems/shuffle-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1528.java) | |Easy|Sort|
1314
|1526|[Minimum Number of Increments on Subarrays to Form a Target Array](https://leetcode.com/problems/minimum-number-of-increments-on-subarrays-to-form-a-target-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1526.java) | |Hard|Segment Tree|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _1535 {
4+
public static class Solution1 {
5+
public int getWinner(int[] arr, int k) {
6+
int winner = arr[0];
7+
int winTimes = 0;
8+
for (int i = 1; i < arr.length; i++) {
9+
if (arr[i] > winner) {
10+
winner = arr[i];
11+
winTimes = 1;
12+
} else {
13+
winTimes++;
14+
}
15+
16+
if (winTimes >= k) {
17+
return winner;
18+
}
19+
}
20+
return winner;
21+
}
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1535;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static junit.framework.TestCase.assertEquals;
8+
9+
public class _1535Test {
10+
private static _1535.Solution1 solution1;
11+
private static int[] arr;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _1535.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
arr = new int[]{2, 1, 3, 5, 4, 6, 7};
21+
assertEquals(5, solution1.getWinner(arr, 2));
22+
}
23+
24+
@Test
25+
public void test2() {
26+
arr = new int[]{1, 11, 22, 33, 44, 55, 66, 77, 88, 99};
27+
assertEquals(99, solution1.getWinner(arr, 100));
28+
}
29+
30+
@Test
31+
public void test3() {
32+
arr = new int[]{1, 9, 8, 2, 3, 7, 6, 4, 5};
33+
assertEquals(9, solution1.getWinner(arr, 7));
34+
}
35+
36+
}

0 commit comments

Comments
 (0)