[data structure and algorithm] in-depth analysis of the solution idea and algorithm example of "stone game VI"

Posted by yuan22m on Mon, 03 Jan 2022 22:42:33 +0100

1, Title Requirements

  • Alice and Bob play a game in turn. Alice takes the lead. There are n stones in a pile of stones. When it's a player's turn, he can remove a stone and get the value of the stone. Alice and Bob have different criteria for the value of stones. Both sides know each other's criteria.
  • Give you two integer arrays of length n, aliceValues and bobValues. aliceValues[i] and bobValues[i] represent the value of the ith stone considered by Alice and Bob respectively.
  • After all the stones are taken, the one with higher score will be the winner. If two players score the same, it will be a draw, and both players will use the optimal strategy to play the game.
  • Please infer the result of the game in the following way:
    • If Alice wins, return 1;
    • If Bob wins, return - 1;
    • If the game draws, return 0.
  • Example 1:
Input: aliceValues = [1,3], bobValues = [2,1]
Output: 1
 Explanation:
If Alice Take stone 1 (subscript starts from 0), then Alice You can get 3 points.
Bob Only stone 0 can be selected and 2 points will be obtained.
Alice win victory.
  • Example 2:
Input: aliceValues = [1,2], bobValues = [3,1]
Output: 0
 Explanation:
Alice Take the stone 0, Bob Take stone 1, they all score 1.
Draw.
  • Example 3:
Input: aliceValues = [2,4,3], bobValues = [1,6,7]
Output:-1
 Explanation:
Ignore Alice How to operate, Bob Can be compared Alice Higher score.
For example, Alice Take stone 1, Bob Take the stone 2, Alice Take the stone 0, Alice Will get 6 points and Bob The score is 7.
Bob Will win.

2, Solving algorithm

① Greedy algorithm

  • Assuming that there are only two stones, the values for a and b are a1, a2, b1 and b2 respectively:
    • The first scheme is that a takes the first, B takes the second, and the value difference between a and B is c1 = a1 - b2;
    • The second scheme is that A takes the second, B takes the first, and the value difference between A and B is c2 = a2 - b1;
  • Which of the two schemes is better for A depends on the comparison of the value difference between the two schemes.
  • Note that c = c1 - c2 = (a1 - b2) - (a2 - b1) = (a1 + b1) - (a2 + b2). If C > 0, scheme 1 is better. If c == 0, the two schemes have the same value. If C < 0, scheme 2 is better.
  • Comparing the advantages and disadvantages of the two schemes is like comparing the advantages and disadvantages of a1 + b1 and a2 + b2. To sum up, it is to compare the advantages and disadvantages of a[i] + b[i] of each subscript I. therefore, the greedy strategy: merge the values of the two groups of stones and take the group with the greatest value each time.
  • Combine the arrays of aliceValues and bobValues, and arrange them according to the maximum value from large to small. Alice selects first and Bob selects later. The even subscript is the nominal total value of the stones selected by Alice, and Bob selects the stones with odd subscripts. Compare the sum of the two.
  • C + + example:
class Solution {
public:
    int stoneGameVI(vector<int>& aliceValues, vector<int>& bobValues) {
		vector<pair<int, int>> mp; // Record values and Subscripts
		int n = aliceValues.size();
		for(int i = 0; i < n; i++) {
			int dis = aliceValues[i] + bobValues[i];
			mp.emplace_back(dis, i);
		}
		sort(mp.rbegin(), mp.rend()); // Sort from large to small
		int sum1 = 0, sum2 = 0;
		for(int i = 0; i < n; i++) {
			if(i % 2 == 0) sum1 += aliceValues[mp[i].second];// Even subscript a
			else sum2 += bobValues[mp[i].second];  		  	 // Odd subscript b
		}
		if(sum1 == sum2) return 0; // Comparison results
		else if(sum1 > sum2) return 1;
		else return -1;
    }
};

② Mathematical reasoning

  • Win win is win. Not only does Alice think a stone is valuable, but also Bob thinks it is really big.
  • Therefore, add and sort the value of a stone in Alice's eyes + the value in Bob's eyes, and then Alice and Bob choose in turn to compare the scores of Alice and Bob.
  • C + + example:
class Solution {
public:
    int stoneGameVI(vector<int>& aliceValues, vector<int>& bobValues) {
        vector<pair<int,int>>vc;
        int len=aliceValues.size();
        for(int i=0;i<len;i++){
            //The value of merging stones from both sides
            int sum=aliceValues[i]+bobValues[i];
            vc.push_back({sum,i});
        }
        //Rank the value of the combined stones
        sort(vc.rbegin(),vc.rend());
        //Alice's score
        int A=0;
        //Bob's score
        int B=0;
        for(int i=0;i<len;i++){
            //Take turns taking the stones
            if(i%2==0){
                A+=aliceValues[vc[i].second];
            }
            else{
                B+=bobValues[vc[i].second];
            }
        }
        //Returns the final result
        if(A==B){
            return 0;
        }
        return A>B?1:-1;
    }
};

Topics: data structure leetcode greedy algorithm