LeetCode 298th Weekly Competition

foreword

        Replay various competitions, mainly some competitions of leetcode , CodeForces , AtCode and so on. A rookie, basically can only pass the sign-in questions, and the rest of the questions will be understood as much as possible and then come out.
       Matches: LeetCode 298th weekly match
        

1. Topic

topic difficulty
5242. The best English alphabet with both upper and lower case ⭐️
5218. The sum of integers whose single digit is K ⭐️⭐️
6099. Longest Binary Subsequence Less than or Equal to K ⭐️⭐️⭐️
5254. Sell Wood Blocks ⭐️⭐️⭐️

2. Algorithm idea

1. The best English letters with both upper and lower case

        Topic understanding: For each uppercase letter, if its lowercase letter also appears in the string, then it is considered to be one 美好英文字母, and the larger one is returned, and if it does not exist, it is returned "".
        (1) Build a hash table to record the letters that appear in the string.
        (2) Z-ATraverse , query whether there are both uppercase and lowercase letters in the hash table, and return the current letter if yes.
        Time complexity: O ( n ) O(n)O ( n )

class Solution {
    
    
public:
    string greatestLetter(string s) {
    
    
        set<char> st;
        for (auto& x: s)
            st.insert(x);
        for (char i = 'Z'; i >= 'A'; i --) {
    
    
            if (st.count(i) && st.count(i + 32))
                return string(1, i);
        }
        return "";
    }
};

2. The single digit is the sum of K integers

        Topic understanding: Select the number of the smallest single digit kis and numthe number of . nAssuming knumbers ki k_i ending inkiThe sum of is num, then ∑ 1 nki = num \sum^n_1{k_i}=num1nki=n u m,即( n ∗ k − num )% 10 = 0 (n*k-num)\%10=0(nknum)%10=0 . (Consider the single digit separately, and add other digits depending on the situation. As long as the rationality of the single digit is ensured, the answer can be found. Since it is in decimal, nthe possible10)
        Because Each number is a positive integer, if numis
        (2) traverse1-10, if i*kandnumare congruent, then return the answer, ifnum − i ∗ k < 0 num-i *k<0numik<0 , indicating that there is no solution.
        Time complexity:O ( n ) O(n)O ( n )

class Solution {
    
    
public:
    int minimumNumbers(int num, int k) {
    
    
        if (num == 0) return 0;
        for (int i = 1; i <= 10; i++) {
    
    
            if (num - i * k < 0) break;
            if ((num - i * k) % 10 == 0)
                return i;
        }
        return -1;
    }
};

3. The longest binary subsequence less than or equal to K

        Topic understanding: To find the maximum length of a subsequence corresponding to a binary number less than or equal to k, Wo's suggestion is to be greedy directly .
        (1) First convert the number k into the corresponding binary sequence t.
        (2) For two strings s, t:
                1. If the length of t is less than the length of s, then the binary number corresponding to the string s is strictly less than k, and the length of s is returned directly.
                2. For the sequence of strings before the string n-ms (n is the length of s, and m is the length of t), the maximum length that can be added to the answer is the number of 0 characters;
                3. For the sequence after the string s m strings, if greater than t, then take the length m - 1, otherwise take m;
                4. The returned answer is the sum of steps 2 and 3.
        Time complexity: O ( n ) O(n)O ( n )

class Solution {
    
    
public:
    int longestSubsequence(string s, int k) {
    
    
        string t;
        while (k) t += to_string(k % 2), k /= 2;
        reverse(t.begin(), t.end());
        int n = s.size(), m = t.size();
        if (n < m) return n;
        int ans = m;
        if (s.substr(n - m) > t) ans --;
        for (int i = n - m - 1; i >= 0; i --)
            if (s[i] == '0') ans ++;
        return ans;
    }
};

4. Sell wood blocks

        Topic understanding: Simplification of the classic chessboard segmentation problem.

  • Set representation: f [ i , j ] f[i,j]f[i,j]
    • Collection description: the split collection of all boards with height andi width .j
    • Collection property: maximum value.
  • 状态转移: f [ i , j ] = m a x ( f [ k ] [ j ] + f [ i − k ] [ j ] , f [ i ] [ k ] + f [ i ] [ m − k ] ) f[i,j]=max(f[k][j]+f[i-k][j],f[i][k]+f[i][m-k]) f[i,j]=max(f[k][j]+f[ik][j],f[i][k]+f[i][mk ] )
            (1) For cutting boards with a height ofi, and a width jof1~nand column-from1~m, and the largest board division is selected from them. Since the row and column divisions do not interfere with each other, it is necessary to find the maximum value of the two schemes.
            Time complexity:O ( n 3 ) O(n^3)O ( n3)
typedef long long LL;

class Solution {
    
    
public:
    long long sellingWood(int n, int m, vector<vector<int>>& prices) {
    
    
        vector<vector<LL>> f(n + 1, vector<LL>(m + 1));
        for (auto& p:prices) {
    
    
            int h = p[0], w = p[1], v = p[2];
            f[h][w] = v;
        }

        for (int i = 1; i <= n; i ++) {
    
    
            for (int j = 1; j <= m; j ++) {
    
    
                for (int k = 1; k < i; k ++) {
    
    
                    f[i][j] = max(f[i][j], f[k][j] + f[i - k][j]);
                }
                for (int k = 1; k < j; k ++) {
    
    
                    f[i][j] = max(f[i][j], f[i][k] + f[i][j - k]);
                }
            }
        }
        return f[n][m];
    }
};

epilogue

        After so many games, Wo You was so stupid that he only asked 1~2 questions in the past few days, and some of them couldn't even ask questions. To sum up, I found that there are relatively many topics related to graph theory and mathematical principles recently released, such as leetcode, cf, atcoder. As soon as I saw graph theory, my brain hurt, and I usually couldn't think of a solution related to mathematical principles. The next goal is to brush up the sense of questions related to graph theory, and then start to review, learn something new every day, and finally force myself to be self-disciplined, and my work and rest have been a bit messy recently.

Guess you like

Origin blog.csdn.net/mumuynsi/article/details/125359045