[LeetCode week race 183] 3. Happy longest string (greedy, structure, ingenious solution)

1. Source title

Links: 5195. Happy longest string

2. Description title

Here Insert Picture Description

3. resolve title

Method a: Method greedy + ingenious construction +

This problem is actually positive considering more troublesome. The greedy will be able to construct a simple and intuitive point, here to talk about policy configuration:

  • First of all I can think of very intuitive, if more than one character, two by two as far as possible we will put it together, its interval with another letter. But it is very troublesome to determine the conditions and special circumstances in terms of input, once I did not know how to judge
  • Put another construction strategy, we do not consider the first two characters of continuous output, consider the output of a character, then it must be in line with the meaning of problems and is the longest, it may be some of the remaining letters, and must have some letters have been used up . So we first construct a continuous string are not the same
  • We will then backfilling the residual letter strings, that is, if the remaining letters a, it will be a single string aexpanded into two a, this is also certainly meet the requirements and is also the longest

That is, first fill in a character, a character if there is a surplus, it will supplement the two characters, so it must be legitimate and the longest.

Be greedy in this configuration, each time when you try to start from the current structure must be filled with the most number of letters, so as to construct the longest continuous string , a sort function on it.

During backfilling when looping through the string, the string and see if there is a surplus every character, if there is still a remaining characters to fill in, so traverse completed the longest string string it is to satisfy the meaning of the questions. Very second thought!

Construction methods varied, several former chiefs really quickly ...

See the code below:

// 执行用时 :4 ms, 在所有 C++ 提交中击败了100.00%的用户
// 内存消耗 :6.5 MB, 在所有 C++ 提交中击败了100.00%的用户

int cnt[4], idx[4];
inline bool cmp(int a, int b) {return cnt[a] > cnt[b];}

class Solution {
public:
    char help(int x) {
        if (x == 1) return 'a';
        if (x == 2) return 'b';
        return 'c';
    }
    string longestDiverseString(int a, int b, int c) {
        vector<int> vt; vt.push_back(0);
        cnt[1] = a, cnt[2] = b, cnt[3] = c;
        idx[1] = 1, idx[2] = 2, idx[3] = 3;
        
        bool flag = true;
        while (flag) {
            sort(idx + 1, idx + 4, cmp);
            flag = false;
            for (int i = 1; i <= 3 and flag == false; ++i) {
                int c = idx[i];
                if (vt.back() != c and cnt[c] > 0) {
                    --cnt[c];
                    vt.push_back(c);
                    flag = true;
                }
            }
        }
        
        int len = vt.size();
        string ans = "";
        for (int i = 1; i < len; ++i) {
            ans += help(vt[i]);
            if (cnt[vt[i]] > 0) {
                ans += help(vt[i]);
                --cnt[vt[i]];
            }
        }
        
        return ans;
    }
};
Published 406 original articles · won praise 368 · views 80000 +

Guess you like

Origin blog.csdn.net/yl_puyu/article/details/105336946