LCP 50. 宝石补给
欢迎各位勇者来到力扣新手村,在开始试炼之前,请各位勇者先进行「宝石补给」。
每位勇者初始都拥有一些能量宝石, 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
将一半的宝石赠送给勇者2
,gem = [2,1,3]
第 2 次操作,勇者2
将一半的宝石赠送给勇者1
,gem = [2,2,2]
第 3 次操作,勇者2
将一半的宝石赠送给勇者0
,gem = [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
将一半的宝石赠送给勇者1
,gem = [25,25,100,100]
第 3 次操作,勇者3
将一半的宝石赠送给勇者0
,gem = [75,25,100,50]
第 4 次操作,勇者3
将一半的宝石赠送给勇者0
,gem = [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
原站题解
cpp 解法, 执行用时: 40 ms, 内存消耗: 21.8 MB, 提交时间: 2023-09-15 07:41:33
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; } };
rust 解法, 执行用时: 4 ms, 内存消耗: 2.3 MB, 提交时间: 2023-09-13 15:14:41
impl Solution { pub fn give_gem(mut gem: Vec<i32>, operations: Vec<Vec<i32>>) -> i32 { for op in operations { let num = gem[op[0] as usize] / 2; gem[op[0] as usize] -= num; gem[op[1] as usize] += num; } *gem.iter().max().unwrap() - *gem.iter().min().unwrap() } }
golang 解法, 执行用时: 20 ms, 内存消耗: 6.7 MB, 提交时间: 2023-09-13 15:10:14
func giveGem(gem []int, operations [][]int) int { for _, op := range operations { gem[op[0]], gem[op[1]] = gem[op[0]] - gem[op[0]]/2, gem[op[1]] + gem[op[0]]/2 } return max(gem) - min(gem) } func min(arr []int) int { _m := arr[0] for _, t := range arr { if t < _m { _m = t } } return _m } func max(arr []int) int { _m := arr[0] for _, t := range arr { if t > _m { _m = t } } return _m }
java 解法, 执行用时: 2 ms, 内存消耗: 42.8 MB, 提交时间: 2023-09-13 15:07:35
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; } }
javascript 解法, 执行用时: 64 ms, 内存消耗: 43.9 MB, 提交时间: 2023-09-13 15:07:12
/** * @param {number[]} gem * @param {number[][]} operations * @return {number} */ 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; };
php 解法, 执行用时: 52 ms, 内存消耗: 25.4 MB, 提交时间: 2023-09-13 15:04:10
class Solution { /** * @param Integer[] $gem * @param Integer[][] $operations * @return Integer */ function giveGem($gem, $operations) { foreach ( $operations as $op ) { $_temp = intval($gem[$op[0]]/2); $gem[$op[0]] -= $_temp; $gem[$op[1]] += $_temp; } return max($gem) - min($gem); } }
python3 解法, 执行用时: 40 ms, 内存消耗: 16.2 MB, 提交时间: 2022-05-26 15:48:03
class Solution: def giveGem(self, gem: List[int], operations: List[List[int]]) -> int: for x, y in operations: gem[x], gem[y] = gem[x] - (gem[x]//2), gem[y] + (gem[x]//2) return max(gem) - min(gem)