forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1823.java
45 lines (42 loc) · 1.48 KB
/
_1823.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
public class _1823 {
public static class Solution1 {
public int findTheWinner(int n, int k) {
List<Integer> list = new ArrayList<>(n);
for (int i = 0; i < n; i++) {
list.add(i + 1);
}
int startIndex = 0;
while (list.size() != 1) {
int removeIndex = (startIndex + k - 1) % list.size();
list.remove(removeIndex);
startIndex = removeIndex;
}
return list.get(0);
}
}
public static class Solution2 {
/**
* My completely original solution: use a double linked list to keep moving people from
* the tail of the queue to the head of the queue until there's only one person in the queue who is the winner.
*/
public int findTheWinner(int n, int k) {
Deque<Integer> doublyLinkedList = new LinkedList<>();
for (int i = 1; i <= n; i++) {
doublyLinkedList.addFirst(i);
}
while (doublyLinkedList.size() > 1) {
int counter = 1;
while (counter++ < k) {
doublyLinkedList.addFirst(doublyLinkedList.pollLast());
}
doublyLinkedList.pollLast();
}
return doublyLinkedList.getLast();
}
}
}