0%

LeetCode-649

题目

Snipaste_2020-12-11_09-37-02.png

结果

Snipaste_2020-12-11_09-46-33.png

代码

BAN掉身后的敌人

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();
// Right to vote
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;
}
}
}
}
}

}
}

复杂度

时间复杂度:O(n²)

空间复杂度:O(n)