LeetCode(Weekly Contest 183)题解

0. Introduction

1. problem solution

5376. 1.1 Non ascending order of the smallest sub-sequence (1403. Minimum Subsequence in Non-Increasing Order)

class Solution {
public:
    vector<int> minSubsequence(vector<int>& nums) {
        sort(nums.begin(), nums.end(), [](int x, int y) {
            return x > y;
        });
        int sum = 0;
        for (auto v : nums) {
            sum += v;
        }
        int cur = 0;
        vector<int> res;
        for (auto v : nums) {
            if (cur * 2 > sum)  break;
            cur += v;
            res.push_back(v);
        }
        return res;
    }
}; 

1.2 5377. The binary representation of the number of steps is reduced to 1 (1404. Number of Steps to Reduce a Number in Binary Representation to One)

class Solution {
public:
    int numSteps(string s) {
        int ans = 0, len = s.length(), cur = len-1;
        for (int i = len-1 ; i >= 1 ; ) {
            cout << i << endl;
            while (cur >= 0 && s[cur] == s[i])  cur--;
            ans += i-cur;
            if (s[i] == '1') {
                ans++;
                if (cur >= 0)   s[cur] = '1';
            }
            i = cur;
        }
        return ans;
    }
}; 

Happy 5195. 1.3 longest string (1405. Longest Happy String)

  • Chinese version of the problem description: https://leetcode-cn.com/problems/longest-happy-string/
  • The English version of the title Description: https://leetcode.com/problems/longest-happy-string/
  • Idea: Every time consumed by the largest possible number of characters, and if at the end of the constructed different, the consumption of two, otherwise consume a large selection times
    • The number of first time is not maintained by the structure of the automatic sorting map, select the judgment all special cases, the code length of life doubt, although over
    • After a moment to rethink, to maintain the relationship with the number of characters map to sort in ascending order of a keyword
  • code show as below:
class Solution {
public:
    string longestDiverseString(int a, int b, int c) {
        map<int, vector<char>, greater<int>> mp;
        if (a > 0)  mp[a].push_back('a');
        if (b > 0)  mp[b].push_back('b');
        if (c > 0)  mp[c].push_back('c');
        int last = a + b + c;
        string ans = "";
        char tail = ' ';
        int num = 0;
        while (last) {
            bool find = false;
            for (auto i = mp.begin() ; i != mp.end() ; i++) {
                int cnt = i->first;
                if (i == mp.begin()) {
                    for (auto j = i->second.begin() ; j != i->second.end() ; j++) {
                        char cur = *j;
                        if (tail != cur) {
                            if (cnt >= 2) {
                                ans += cur;
                                ans += cur;
                                cnt -= 2;
                                last -= 2;
                            } else {
                                ans += cur;
                                cnt -= 1;
                                last -= 1;
                            }
                            i->second.erase(j);
                            if (i->second.size() == 0) {
                                mp.erase(i);
                            }
                            if (cnt > 0) {
                                mp[cnt].push_back(cur);   
                            }
                            find = true;
                            tail = cur;
                            break;
                        }
                    }   
                } else {
                    for (auto j = i->second.begin() ; j != i->second.end() ; j++) {
                        char cur = *j;
                        if (tail != cur) {
                            if (cnt >= 1) {
                                ans += cur;
                                cnt -= 1;
                                last -= 1;
                            }
                            i->second.erase(j);
                            if (i->second.size() == 0) {
                                mp.erase(i);
                            }
                            if (cnt > 0) {
                                mp[cnt].push_back(cur);   
                            }
                            find = true;
                            tail = cur;
                            break;
                        }
                    }
                }
                if (find)    break;
            }
            if (!find)    break;
        }
        return ans;
    }
}; 

5379. 1.4 game stone III (1406. Stone Game III)

  • Chinese version of the problem description: https://leetcode-cn.com/problems/stone-game-iii/
  • The English version of the title Description: https://leetcode.com/problems/stone-game-iii/
  • Ideas: This title belongs to the equality Game transparent information, game theory is the most basic one, the idea is a step backwards from the last game of the initiation of anti count, is calculated for each state "players from that state can not win / Up how many points could get ", with a similar dynamic programming idea has been considered the first step
    • DP [i] denotes the i-heap of stones to n-1, taking the upper hand, the maximum value which can be obtained
    • After the forward calculation, dp [n] = 0
    • dp [i] = max (sum-dp [i + 1], sum-dp [i + 2], sum-dp [i + 3]), wherein the sum represents the fraction of the i-th to n-1 stack stones and
  • code show as below:
class Solution {
public:
    string stoneGameIII(vector<int>& stoneValue) {
        int n = stoneValue.size();
        vector<int> dp = vector<int>(n+1, INT_MIN);
        dp[n] = 0;
        int sum = 0;
        for (int i = n-1 ; i >= 0 ; i--) {
            sum += stoneValue[i];
            for (int j = 1 ; j <= 3 && i+j <= n ; j++) {
                dp[i] = max(dp[i], sum-dp[i+j]);
            }
        }
        int alice = dp[0], bob = sum-dp[0];
        cout << alice << endl;
        cout << bob << endl;
        if (alice > bob)    return "Alice";
        else if (alice < bob)   return "Bob";
        else    return "Tie";
    }
};

2. References

Guess you like

Origin www.cnblogs.com/wangao1236/p/12636784.html