LeetCode 2020 Likou Cup National Autumn Programming Competition (656/3244, top 20.2%)

1. Competition results

Two questions were made, and the third question was written for a long time to no avail. Still the strength is too bad, keep on going!

There are 3244 contestants who have submitted successfully, and 656 are ranked


The first few bosses are too strong!

Top 3 big brothers

2. Title

1. LeetCode LCP 17. Quick calculation robot easy

Topic link

Xiaokou found a quick calculation robot in the autumn market. The store tells the robot two numbers (denoted as x and y), please tell me the calculation instructions:

"A" operation: make x = 2 * x + y;
"B" operation: make y = 2 * y + x.
In this game, the numbers spoken by the store are x = 1 and y = 0. The calculation instructions spoken by Xiaokou are recorded as a string s consisting of capital letters A and B. The order of the characters in the string indicates the calculation Order, please return the final sum of x and y.

示例 1:
输入:s = "AB"
输出:4
解释:
经过一次 A 运算后,x = 2, y = 0。
再经过一次 B 运算,x = 2, y = 2。
最终 x 与 y 之和为 4。

提示:
0 <= s.length <= 10
s 由 'A''B' 组成

Problem solving:

  • Simulation according to the question
class Solution {
    
    
public:
    int calculate(string s) {
    
    
    	int x = 1, y = 0;
    	for(char c : s) 
    	{
    
    
    		if(c == 'A')
    			x = x*2+y;
    		else
    			y = y*2+x;
    	}
    	return x+y;
    }
};

2. LeetCode LCP 18. Breakfast combo easy

Topic link

Xiaokou chose a breakfast stall in the autumn market. The price of each staple food is recorded in the one-dimensional integer array staple, and the price of each beverage is recorded in the one-dimensional integer array drinks.
Xiaokou’s plan chooses a staple food and a beverage, and the cost does not exceed x yuan.
Please return how many purchase options there are in the small deduction.

Note: The answer needs to be modulo 1e9 + 7 (1000000007),
such as: the initial result of the calculation is: 1000000008, please return 1.

示例 1:
输入:staple = [10,20,5], drinks = [5,5,2], x = 15
输出:6
解释:小扣有 6 种购买方案,所选主食与所选饮料在数组中对应的下标分别是:
第 1 种方案:staple[0] + drinks[0] = 10 + 5 = 15;
第 2 种方案:staple[0] + drinks[1] = 10 + 5 = 15;
第 3 种方案:staple[0] + drinks[2] = 10 + 2 = 12;
第 4 种方案:staple[2] + drinks[0] = 5 + 5 = 10;
第 5 种方案:staple[2] + drinks[1] = 5 + 5 = 10;
第 6 种方案:staple[2] + drinks[2] = 5 + 2 = 7。

示例 2:
输入:staple = [2,1,1], drinks = [8,9,5,1], x = 9
输出:8
解释:小扣有 8 种购买方案,所选主食与所选饮料在数组中对应的下标分别是:
第 1 种方案:staple[0] + drinks[2] = 2 + 5 = 7;
第 2 种方案:staple[0] + drinks[3] = 2 + 1 = 3;
第 3 种方案:staple[1] + drinks[0] = 1 + 8 = 9;
第 4 种方案:staple[1] + drinks[2] = 1 + 5 = 6;
第 5 种方案:staple[1] + drinks[3] = 1 + 1 = 2;
第 6 种方案:staple[2] + drinks[0] = 1 + 8 = 9;
第 7 种方案:staple[2] + drinks[2] = 1 + 5 = 6;
第 8 种方案:staple[2] + drinks[3] = 1 + 1 = 2;

提示:
1 <= staple.length <= 10^5
1 <= drinks.length <= 10^5
1 <= staple[i],drinks[i] <= 10^5
1 <= x <= 2*10^5

Problem solving:

  • Sort an array A, traverse another array B, and find binary search in A
class Solution {
    
    
public:
    int breakfastNumber(vector<int>& staple, vector<int>& drinks, int x) {
    
    
        sort(drinks.begin(), drinks.end());
        long long ans = 0, mod = 1e9+7;
        for(int stp : staple) 
        {
    
    
            if(stp >= x)
                continue;
            int target = x - stp;
            int pos = bs(drinks, target);
            if(pos != -1)
            {
    
    
                ans = (ans+pos+1)%mod;
            }
        }
        return ans;
    }
    int bs(vector<int>& arr, int target)
    {
    
    
        int l = 0, r = arr.size()-1, n = arr.size(), mid;
        while(l <= r)	//查找小于等于 target的最后一个数
        {
    
    
            mid = (l + r) / 2;
            if(arr[mid] > target)
            {
    
    
                r = mid-1;
            }
            else
            {
    
    
                if(mid == n-1 || arr[mid+1] > target)
                    return mid;
                else
                    l = mid+1;
            }
        }
        return -1;
    }
};

1164 ms 146.5 MB

  • Or traverse both arrays with double pointers
class Solution {
    
    
public:
    int breakfastNumber(vector<int>& staple, vector<int>& drinks, int x) {
    
    
    	sort(staple.begin(), staple.end());
    	sort(drinks.begin(), drinks.end());
    	long long ans = 0, mod = 1e9+7;
    	int m = staple.size(), n = drinks.size(), i, j;
        i = 0, j = n-1;
        while(i < m && j >= 0)
        {
    
    
            if(staple[i]+drinks[j] <= x)
            {
    
    
                ans = (ans+j+1)%mod;
                i++;
            }
            else
                j--;
        }
    	return ans;
    }
};

1368 ms 146.4 MB

3. LeetCode LCP 19. Autumn leaves collection medium

Topic link

Xiaokou went out for an autumn tour and collected some red and yellow leaves on the way. He used these leaves to preliminarily organize a collection of autumn leaves leaves. The string leaves only contains lowercase characters r and y, where the character r represents a red leaf and the character y represents A yellow leaf.
For the sake of beauty and neatness, Xiaokou wanted to adjust the arrangement of leaves in the collection into three parts: red, yellow, and red. The number of leaves in each part may not be equal , but they must be greater than or equal to 1 .
For each adjustment operation, the small buckle can replace a red leaf with a yellow leaf or replace a yellow leaf with a red leaf.
May I ask how many adjustment operations are required for Xiaokou at least to adjust the Autumn Leaves Collection.

示例 1:
输入:leaves = "rrryyyrryyyrr"
输出:2
解释:调整两次,将中间的两片红叶替换成黄叶,得到 "rrryyyyyyyyrr"

示例 2:
输入:leaves = "ryr"
输出:0
解释:已符合要求,不需要额外操作

提示:
3 <= leaves.length <= 10^5
leaves 中只包含字符 'r' 和字符 'y'

Problem solving:

  • Refer to the DP of the IK boss

  • dp[i][0]It is to iend all red Rminimum number of operations

  • dp[i][1]Shows the iend of forming RYminimum number of operations

  • dp[i][2]Shows the iend of forming RYRminimum number of operations

class Solution {
    
    
public:
    int minimumOperations(string leaves) {
    
    
        int n = leaves.size(), i;
        vector<vector<int>> dp(n, vector<int>(3, INT_MAX));
        if(leaves[0]=='r')
            dp[0][0] = 0;
        else
            dp[0][0] = 1;//黄的改成红的
        if(leaves[1]=='y')
        {
    
    
            dp[1][0] = dp[0][0]+1;
            //全红      前面全红 + y改r
            dp[1][1] = dp[0][0];
            // RY     前面全红  + 当前y
        }
        else
        {
    
    
            dp[1][0] = dp[0][0];
            //全红     前面全红 ,当前也是红
            dp[1][1] = dp[0][0]+1;
            //RY      前面全红,当前r改y
        }
        if(leaves[2]=='r')
        {
    
    
            dp[2][0] = dp[1][0];
            dp[2][1] = min(dp[1][0]+1, dp[1][1]+1);
            dp[2][2] = dp[1][1];
        }
        else
        {
    
    
            dp[2][0] = dp[1][0]+1;
            dp[2][1] = min(dp[1][0], dp[1][1]);
            dp[2][2] = dp[1][1]+1;
        }
        for(i = 3; i < n; i++) 
        {
    
    
            if(leaves[i] == 'r')
            {
    
    
                dp[i][0] = dp[i-1][0];
                dp[i][1] = min(dp[i-1][0]+1, dp[i-1][1]+1);
                dp[i][2] = min(dp[i-1][1], dp[i-1][2]);
            }
            else
            {
    
    
                dp[i][0] = dp[i-1][0]+1;
                dp[i][1] = min(dp[i-1][1],dp[i-1][0]);
                dp[i][2] = min(dp[i-1][1]+1, dp[i-1][2]+1);
            }
        }
        return dp[n-1][2];
    }
};

704 ms 114.4 MB

4. LeetCode LCP 20. BRT hard

Topic link


Problem solving:

5. LeetCode LCP 21. Chase the game hard

Topic link


Problem solving:


My CSDN blog address https://michael.blog.csdn.net/

Long press or scan the QR code to follow my official account (Michael Amin), come on together, learn and make progress together!
Michael Amin

Guess you like

Origin blog.csdn.net/qq_21201267/article/details/108553304