LC.P1686[石子游戏VI]

方法一:贪心+排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public int stoneGameVI(int[] a, int[] b) {
int n = a.length;
Integer[] index = new Integer[n];
for (int i = 0; i < n; i++) {
index[i] = i;
}
Arrays.sort(index, (i, j) -> a[j] + b[j] - a[i] - b[i]);
int diff = 0;
for (int i = 0; i < n; i++) {
diff += i % 2 == 0 ? a[index[i]] : -b[index[i]];
}
return Integer.compare(diff, 0);
}
}
  • 时间复杂度:$O(nlogn)$
  • 空间复杂度:$O(n)$