LCP 50-宝石补给

Raphael Liu Lv10

欢迎各位勇者来到力扣新手村,在开始试炼之前,请各位勇者先进行「宝石补给」。 每位勇者初始都拥有一些能量宝石, gem[i] 表示第 i
位勇者的宝石数量。现在这些勇者们进行了一系列的赠送,operations[j] = [x, y] 表示在第 j 次的赠送中 第 x
位勇者将自己一半的宝石(需向下取整)赠送给第 y 位勇者。
在完成所有的赠送后,请找到拥有最多宝石的勇者和拥有最少宝石的勇者,并返回他们二者的宝石数量之差注意: -
赠送将按顺序逐步进行。 示例 1: >输入:gem = [3,1,2], operations = [[0,2],[2,1],[2,0]] >

输出:2 > >解释: >第 1 次操作,勇者 0 将一半的宝石赠送给勇者 2gem = [2,1,3] >第 2 次操作,勇者
2 将一半的宝石赠送给勇者 1gem = [2,2,2] >第 3 次操作,勇者 2 将一半的宝石赠送给勇者 0gem = [3,2,1] >返回 3 - 1 = 2 示例 2: >输入:gem = [100,0,50,100], operations = [[0,2],[0,1],[3,0],[3,0]] > >输出:75 > >解释: >第 1 次操作,勇者 0 将一半的宝石赠送给勇者 2
gem = [50,0,100,100] >第 2 次操作,勇者 0 将一半的宝石赠送给勇者 1gem = [25,25,100,100] >第 3 次操作,勇者 3 将一半的宝石赠送给勇者 0gem = [75,25,100,50] >第 4
次操作,勇者 3 将一半的宝石赠送给勇者 0gem = [100,25,100,25] >返回 100 - 25 = 75 示例
3:
>输入:gem = [0,0,0,0], operations = [[1,2],[3,1],[1,2]] > >输出:0 提示:
- 2 <= gem.length <= 10^3 - 0 <= gem[i] <= 10^3 - 0 <= operations.length <= 10^4 - operations[i].length == 2 - 0 <= operations[i][0], operations[i][1] < gem.length

方法一:模拟

思路

按照题目描述,首先遍历 operations,计算出赠予的宝石数量,然后直接在 gem 数组上对两位勇者的宝石数增减相应的宝石数量。然后遍历 gem 数组,计算出宝石数量最大值和最小值,计算差值后返回。

代码

[sol1-Python3]
1
2
3
4
5
6
7
8
9
10
11
class Solution:
def giveGem(self, gem: List[int], operations: List[List[int]]) -> int:
for x, y in operations:
number = gem[x] // 2
gem[x] -= number
gem[y] += number
mn, mx = gem[0], gem[0]
for number in gem:
mn = min(number, mn)
mx = max(number, mx)
return mx - mn
[sol1-Java]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public int giveGem(int[] gem, int[][] operations) {
for (int[] operation : operations) {
int x = operation[0], y = operation[1];
int number = gem[x] / 2;
gem[x] -= number;
gem[y] += number;
}
int mn = gem[0], mx = gem[0];
for (int number : gem) {
mn = Math.min(number, mn);
mx = Math.max(number, mx);
}
return mx - mn;
}
}
[sol1-C#]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class Solution {
public int GiveGem(int[] gem, int[][] operations) {
foreach (int[] operation in operations) {
int x = operation[0], y = operation[1];
int number = gem[x] / 2;
gem[x] -= number;
gem[y] += number;
}
int mn = gem[0], mx = gem[0];
foreach (int number in gem) {
mn = Math.Min(number, mn);
mx = Math.Max(number, mx);
}
return mx - mn;
}
}
[sol1-C++]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
int giveGem(vector<int>& gem, vector<vector<int>>& operations) {
for (auto &operation : operations) {
int x = operation[0], y = operation[1];
int number = gem[x] / 2;
gem[x] -= number;
gem[y] += number;
}
int mn = *min_element(gem.begin(), gem.end());
int mx = *max_element(gem.begin(), gem.end());
return mx - mn;
}
};
[sol1-C]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int giveGem(int* gem, int gemSize, int** operations, int operationsSize, int* operationsColSize) {
for (int i = 0; i < operationsSize; i++) {
int x = operations[i][0], y = operations[i][1];
int number = gem[x] / 2;
gem[x] -= number;
gem[y] += number;
}
int mn = gem[0], mx = gem[0];
for (int i = 0; i < gemSize; i++) {
int number = gem[i];
mn = fmin(number, mn);
mx = fmax(number, mx);
}
return mx - mn;
}
[sol1-Go]
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
func giveGem(gem []int, operations [][]int) int {
for _, operation := range operations {
x, y := operation[0], operation[1]
number := gem[x] / 2
gem[x] -= number
gem[y] += number
}
mn, mx := gem[0], gem[0];
for _, number := range gem {
mn = min(number, mn)
mx = max(number, mx)
}
return mx - mn
}

func max(x int, y int) int {
if x < y {
return y
}
return x
}

func min(x int, y int) int {
if x > y {
return y
}
return x
}
[sol1-JavaScript]
1
2
3
4
5
6
7
8
9
10
11
12
var giveGem = function(gem, operations) {
for (const operation of operations) {
const x = operation[0];
const y = operation[1];
const number = Math.floor(gem[x] / 2);
gem[x] -= number;
gem[y] += number;
}
const mn = Math.min(...gem);
const mx = Math.max(...gem);
return mx - mn;
};

复杂度分析

  • 时间复杂度:O(m+n),其中 m 是数组 operations 的长度,n 是数组 gem 的长度。我们需要遍历两个数组各一次。

  • 空间复杂度:O(1),我们仅使用常数空间。

 Comments
On this page
LCP 50-宝石补给