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 50 51 52 53 54
| class Solution { public String predictPartyVictory(String senate) { int length = senate.length(); boolean[] flag = new boolean[length]; int radiant = 0, dire = 0; char[] senates = senate.toCharArray(); for (char ch : senates) { if (ch == 'R') { radiant++; } else { dire++; } }
while (true) { for (int i = 0; i < length; i++) { if (flag[i]) { continue; } if (senates[i] == 'R') { if (dire == 0) { return "Radiant"; } for (int j = i; j < length; j++) { if (!flag[j] && senates[j] == 'D') { flag[j] = true; dire--; break; } if (j == length - 1) { j = -1; } } } else { if (radiant == 0) { return "Dire"; } for (int j = i; j < length; j++) { if (!flag[j] && senates[j] == 'R') { flag[j] = true; radiant--; break; } if (j == length - 1) { j = -1; } } } } }
} }
|