LeetCode solution summary LCP 50. Gem supply

 Directory link:

Likou Programming Questions-Solution Summary_Sharing + Recording-CSDN Blog

GitHub synchronization problem solving project:

GitHub - September26/java-algorithms: A summary of algorithm questions, including solutions and codes for questions from Niuke, leetCode, lintCode and other websites, as well as complete mode classes and even linked list code generation tools.

Original title link: LeetCode official website - the technology growth platform loved by geeks around the world


describe:

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  i the number of gems owned by the first hero. Now these brave men have made a series of gifts, operations[j] = [x, y] which means that in the first  j gift, the  x brave man will give half of his gems (rounded down) to the brave  y man.

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  0 will give half of the gems to the brave  2gem = [2,1,3] In the second operation, the brave  2 will give half of the gems to the brave  1gem = [2,2,2] In the third operation, the brave  2 will give half of the gems to the brave  0gem = [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 man  0 will give half of the gems to the brave man  2gem = [50,0,100,100] In the second operation, the brave man  0 will give half of the gems to the brave man  1gem = [25,25,100,100] In the third operation, the brave man  will give half of the gems to the brave man  . In the fourth  operation, the brave man 3 will give away half of the gems to  0the  brave man.  Give half of the gems to the brave  and   return 100 - 25 = 75gem = [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

Problem-solving ideas:

First follow the execution in operations, and then calculate the maximum and minimum values.

Code:

class SolutionLCP50
{
public:
    int giveGem(vector<int> &gem, vector<vector<int>> &operations)
    {
        for (vector<int> operation : operations)
        {
            int value = gem[operation[0]] / 2;
            gem[operation[1]] = gem[operation[1]] + value;
            gem[operation[0]] -= value;
        }
        int maxValue = 0;
        int minValue = 10000;
        for (int value : gem)
        {
            maxValue = max(maxValue, value);
            minValue = min(minValue, value);
        }
        return maxValue - minValue;
    }
};

Guess you like

Origin blog.csdn.net/AA5279AA/article/details/132909741