Skip to content

Commit af0f6ef

Browse files
add 3175
1 parent 572cbbe commit af0f6ef

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-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+
| 3175 | [Find The First Player to win K Games in a Row](https://leetcode.com/problems/find-the-first-player-to-win-k-games-in-a-row/) | [Java](../master/src/main/java/com/fishercoder/solutions/_3175.java) | | Medium |
1112
| 3174 | [Clear Digits](https://leetcode.com/problems/clear-digits/) | [Java](../master/src/main/java/com/fishercoder/solutions/_3174.java) | | Easy |
1213
| 3164 | [Find the Number of Good Pairs II](https://leetcode.com/problems/find-the-number-of-good-pairs-ii/) | [Java](../master/src/main/java/com/fishercoder/solutions/_3164.java) | | Medium |
1314
| 3162 | [Find the Number of Good Pairs I](https://leetcode.com/problems/find-the-number-of-good-pairs-i/) | [Java](../master/src/main/java/com/fishercoder/solutions/_3162.java) | | Easy |
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.Deque;
4+
import java.util.LinkedList;
5+
6+
public class _3175 {
7+
public static class Solution1 {
8+
public int findWinningPlayer(int[] skills, int k) {
9+
Deque<int[]> q = new LinkedList<>();
10+
int highestSkill = 0;
11+
for (int i = 0; i < skills.length; i++) {
12+
q.offer(new int[]{i, skills[i], 0});
13+
highestSkill = Math.max(highestSkill, skills[i]);
14+
}
15+
int count = 0;
16+
while (true) {
17+
int[] first = q.pollFirst();
18+
if (first[1] == highestSkill) {
19+
//if the highest skill stands at the head of the queue, then it'll keep standing there
20+
//so it's guaranteed that it'll be the winner
21+
return first[0];
22+
}
23+
int[] second = q.pollFirst();
24+
if (first[2] >= k) {
25+
return first[0];
26+
}
27+
if (first[1] > second[1]) {
28+
first[2]++;
29+
q.addLast(second);
30+
q.addFirst(first);
31+
} else {
32+
second[2]++;
33+
q.addFirst(second);
34+
q.addLast(first);
35+
}
36+
count++;
37+
}
38+
}
39+
}
40+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._3175;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
public class _3175Test {
10+
private static _3175.Solution1 solution1;
11+
private static int[] skills;
12+
private static int k;
13+
14+
@BeforeEach
15+
public void setup() {
16+
solution1 = new _3175.Solution1();
17+
}
18+
19+
@Test
20+
public void test1() {
21+
skills = new int[]{16, 4, 7, 17};
22+
k = 562084119;
23+
assertEquals(3, solution1.findWinningPlayer(skills, k));
24+
}
25+
26+
}

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