LeetCode LCP 50. Gem Supply

【LetMeFly】LCP 50.Gem Supply

Leetcode question link: https://leetcode.cn/problems/WHnhjV/

Welcome to the Likou Newbie Village. Before starting the trial, please carry out "Gem Supply".

Each hero initially has some energy gems, gem[i]which represents ithe number of gems for the No. 1 hero. Now these brave men have made a series of gifts, operations[j] = [x, y]which means that in the first jgift, the xbrave man will give half of his gems (rounded down) to the brave yman.

After completing all the gifts, please find the hero with the most gems and the hero with the least gems, and return the difference in the number of gems between them .

Notice:

  • The giveaways will be made gradually and sequentially.

Example 1:

enter:gem = [3,1,2], operations = [[0,2],[2,1],[2,0]]

Output:2

Explanation:
In the first operation, the brave 0will give half of the gems to the brave 2. gem = [2,1,3]
In the second operation, the brave 2will give half of the gems to the brave 1. gem = [2,2,2]
In the third operation, the brave 2will give half of the gems to the brave 0. gem = [3,2,1]
Return 3 - 1 = 2

Example 2:

enter:gem = [100,0,50,100], operations = [[0,2],[0,1],[3,0],[3,0]]

Output:75

Explanation:
In the first operation, the brave 0will give half of the gems to the brave 2. gem = [50,0,100,100]
In the second operation, the brave 0will give half of the gems to the brave 1. gem = [25,25,100,100]
In the third operation, the brave will give half of the gems to the brave . In the fourth operation, the brave 3will give half of the gems to the brave. Give half of the gems to the brave and return 100 - 25 = 750gem = [75,25,100,50]
30gem = [100,25,100,25]

Example 3:

enter:gem = [0,0,0,0], operations = [[1,2],[3,1],[1,2]]

Output:0

hint:

  • 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

Method 1: Simulation

Traverse the operations array, each time for one operation [a, b] [a, b][a,b ] , changegem [ a ] ​​gem[a]half of g e m [ a ] ​​(rounded down) givesgem [ b ] gem[b]gem[b]

Finally, the difference between the maximum and minimum values ​​of the gem array is returned.

  • Time complexity O (len (gem) + len (operations)) O(len(gem) + len(operations))O ( l e n ( g e m )+len(operations))
  • Space complexity O ( 1 ) O(1)O(1)

AC code

C++
class Solution {
    
    
public:
    int giveGem(vector<int>& gem, vector<vector<int>>& operations) {
    
    
        for (auto& ab : operations) {
    
    
            int change = gem[ab[0]] / 2;
            gem[ab[0]] -= change;
            gem[ab[1]] += change;
        }
        int m = 1e7, M = 0;
        for (int g : gem) {
    
    
            m = min(m, g);
            M = max(M, g);
        }
        return M - m;
    }
};
Python
# from typing import List

class Solution:
    def giveGem(self, gem: List[int], operations: List[List[int]]) -> int:
        for a, b in operations:
            change = gem[a] // 2
            gem[a] -= change
            gem[b] += change
        return max(gem) - min(gem)

The article is published simultaneously on CSDN. It is not easy to be original. Please attach the link to the original article after reprinting with the author's consent ~
Tisfy: https://letmefly.blog.csdn.net/article/details/132895132

Guess you like

Origin blog.csdn.net/Tisfy/article/details/132895132