-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
49 lines (47 loc) · 1.37 KB
/
Copy pathSolution.java
File metadata and controls
49 lines (47 loc) · 1.37 KB
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
46
47
48
49
public class Solution
{
public String predictPartyVictory(String senate)
{
int radiant = (int) senate.chars().filter(c -> c == 'R').count(), dire = senate.length() - radiant;
boolean[] banned = new boolean[senate.length()];
while (radiant != 0 && dire != 0)
{
for (int i = 0; i < senate.length(); i++)
{
if (banned[i])
{
continue;
}
char current = senate.charAt(i);
banned[getNextIdx(senate, i, banned)] = false;
if (current == 'R')
{
dire--;
}
else
{
radiant--;
}
}
}
return radiant == 0 ? "Dire" : "Radiant";
}
private int getNextIdx(String senate, int currentIdx, boolean[] banned)
{
char current = senate.charAt(currentIdx);
int nextId = currentIdx;
while (nextId < senate.length() && (senate.charAt(nextId) == current || banned[nextId]))
{
nextId++;
}
if (nextId == senate.length())
{
nextId = currentIdx;
while (senate.charAt(nextId) == current || banned[nextId])
{
nextId--;
}
}
return nextId;
}
}