classSolution: defgiveGem(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
publicclassSolution { publicintGiveGem(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
classSolution { public: intgiveGem(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
intgiveGem(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; }