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

Commit 23e60ed

Browse files
refactor 649
1 parent 7acaafe commit 23e60ed

File tree

2 files changed

+30
-28
lines changed

2 files changed

+30
-28
lines changed

src/main/java/com/fishercoder/solutions/_649.java

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,27 +51,29 @@
5151
*/
5252
public class _649 {
5353

54-
public String predictPartyVictory(String senate) {
55-
Queue<Integer> radiantQ = new LinkedList<>();
56-
Queue<Integer> direQ = new LinkedList<>();
57-
int len = senate.length();
58-
for (int i = 0; i < len; i++) {
59-
if (senate.charAt(i) == 'R') {
60-
radiantQ.offer(i);
61-
} else {
62-
direQ.offer(i);
54+
public static class Solution1 {
55+
public String predictPartyVictory(String senate) {
56+
Queue<Integer> radiantQ = new LinkedList<>();
57+
Queue<Integer> direQ = new LinkedList<>();
58+
int len = senate.length();
59+
for (int i = 0; i < len; i++) {
60+
if (senate.charAt(i) == 'R') {
61+
radiantQ.offer(i);
62+
} else {
63+
direQ.offer(i);
64+
}
6365
}
64-
}
65-
while (!radiantQ.isEmpty() && !direQ.isEmpty()) {
66-
int radiantIndex = radiantQ.poll();
67-
int direIndex = direQ.poll();
68-
if (radiantIndex < direIndex) {
69-
/**Radiant will ban Dire in this case, so we'll add radiant index back to the queue plus n*/
70-
radiantQ.offer(radiantIndex + len);
71-
} else {
72-
direQ.offer(direIndex + len);
66+
while (!radiantQ.isEmpty() && !direQ.isEmpty()) {
67+
int radiantIndex = radiantQ.poll();
68+
int direIndex = direQ.poll();
69+
if (radiantIndex < direIndex) {
70+
/**Radiant will ban Dire in this case, so we'll add radiant index back to the queue plus n*/
71+
radiantQ.offer(radiantIndex + len);
72+
} else {
73+
direQ.offer(direIndex + len);
74+
}
7375
}
76+
return radiantQ.isEmpty() ? "Dire" : "Radiant";
7477
}
75-
return radiantQ.isEmpty() ? "Dire" : "Radiant";
7678
}
7779
}

src/test/java/com/fishercoder/_649Test.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,45 +10,45 @@
1010
* Created by fishercoder on 5/8/17.
1111
*/
1212
public class _649Test {
13-
private static _649 test;
13+
private static _649.Solution1 solution1;
1414

1515
@BeforeClass
1616
public static void setup() {
17-
test = new _649();
17+
solution1 = new _649.Solution1();
1818
}
1919

2020
@Test
2121
public void test1() {
22-
assertEquals("Dire", test.predictPartyVictory("RDDDR"));
22+
assertEquals("Dire", solution1.predictPartyVictory("RDDDR"));
2323
}
2424

2525
@Test
2626
public void test2() {
27-
assertEquals("Radiant", test.predictPartyVictory("RD"));
27+
assertEquals("Radiant", solution1.predictPartyVictory("RD"));
2828
}
2929

3030
@Test
3131
public void test3() {
32-
assertEquals("Dire", test.predictPartyVictory("RDD"));
32+
assertEquals("Dire", solution1.predictPartyVictory("RDD"));
3333
}
3434

3535
@Test
3636
public void test4() {
37-
assertEquals("Radiant", test.predictPartyVictory("RDDR"));
37+
assertEquals("Radiant", solution1.predictPartyVictory("RDDR"));
3838
}
3939

4040
@Test
4141
public void test5() {
42-
assertEquals("Dire", test.predictPartyVictory("RDDRD"));
42+
assertEquals("Dire", solution1.predictPartyVictory("RDDRD"));
4343
}
4444

4545
@Test
4646
public void test6() {
47-
assertEquals("Dire", test.predictPartyVictory("RDDDDDRR"));
47+
assertEquals("Dire", solution1.predictPartyVictory("RDDDDDRR"));
4848
}
4949

5050
@Test
5151
public void test7() {
52-
assertEquals("Dire", test.predictPartyVictory("RDDDDRR"));
52+
assertEquals("Dire", solution1.predictPartyVictory("RDDDDRR"));
5353
}
5454
}

0 commit comments

Comments
 (0)