Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit84152b2

Browse files
add a solution for 1823
1 parent8b08ebf commit84152b2

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

‎src/main/java/com/fishercoder/solutions/_1823.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
packagecom.fishercoder.solutions;
22

33
importjava.util.ArrayList;
4+
importjava.util.Deque;
5+
importjava.util.LinkedList;
46
importjava.util.List;
57

68
publicclass_1823 {
@@ -19,4 +21,25 @@ public int findTheWinner(int n, int k) {
1921
returnlist.get(0);
2022
}
2123
}
24+
25+
publicstaticclassSolution2 {
26+
/**
27+
* My completely original solution: use a double linked list to keep moving people from
28+
* the tail of the queue to the head of the queue until there's only one person in the queue who is the winner.
29+
*/
30+
publicintfindTheWinner(intn,intk) {
31+
Deque<Integer>doublyLinkedList =newLinkedList<>();
32+
for (inti =1;i <=n;i++) {
33+
doublyLinkedList.addFirst(i);
34+
}
35+
while (doublyLinkedList.size() >1) {
36+
intcounter =1;
37+
while (counter++ <k) {
38+
doublyLinkedList.addFirst(doublyLinkedList.pollLast());
39+
}
40+
doublyLinkedList.pollLast();
41+
}
42+
returndoublyLinkedList.getLast();
43+
}
44+
}
2245
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._1823;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importstaticorg.junit.Assert.assertEquals;
8+
9+
publicclass_1823Test {
10+
privatestatic_1823.Solution1solution1;
11+
privatestatic_1823.Solution2solution2;
12+
privatestaticintexpected;
13+
14+
@BeforeClass
15+
publicstaticvoidsetup() {
16+
solution1 =new_1823.Solution1();
17+
solution2 =new_1823.Solution2();
18+
}
19+
20+
@Test
21+
publicvoidtest1() {
22+
expected =1;
23+
assertEquals(expected,solution1.findTheWinner(6,5));
24+
assertEquals(expected,solution2.findTheWinner(6,5));
25+
}
26+
27+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp