LCP.P50[宝石补给]

方法一:模拟

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public int giveGem(int[] gem, int[][] operations) {
int max = 0, min = Integer.MAX_VALUE;
for (int[] op : operations) {
int x = op[0], y = op[1], val = gem[x] >> 1;
gem[y] += val;
gem[x] -= val;
}
for (int x : gem) {
max = Math.max(max, x);
min = Math.min(min, x);
}
return max - min;
}
}
  • 时间复杂度:$O(m + n)$
  • 空间复杂度:$O(1)$