LeetCode字母排列

LeetCode字母排列

    • 621. Task Scheduler
    • 767. Reorganize String
    • 358. Rearrange String k Distance Apart

主要思路是贪婪算法,先借助priority_queue排出现次数最多的字母,并将其禁用,放入临时存储queue,queue中数目等于k个时,第一个可解禁,将其放回priority_queue。

621. Task Scheduler

Given a char array representing tasks CPU need to do. It contains capital letters A to Z where different letters represent different tasks.Tasks could be done without original order. Each task could be done in one interval. For each interval, CPU could finish one task or just be idle.

However, there is a non-negative cooling interval n that means between two same tasks, there must be at least n intervals that CPU are doing different tasks or just be idle.

You need to return the least number of intervals the CPU will take to finish all the given tasks.

Example 1:
Input: tasks = [“A”,“A”,“A”,“B”,“B”,“B”], n = 2
Output: 8
Explanation: A -> B -> idle -> A -> B -> idle -> A -> B.
Note:
The number of tasks is in the range [1, 10000].
The integer n is in the range [0, 100].

class Solution {
public:
    int leastInterval(vector<char>& tasks, int n) {
        unordered_map<char,int> task;
        int m=0;
        for(char c:tasks){
            task[c]++;
            m = max(m,task[c]);
        }
        int re = (m-1)*(n+1);
        for(auto i:task){
            if(i.second==m)
                re++;
        }
        return tasks.size()>re?tasks.size():re;        
    }
};

767. Reorganize String

Given a string S, check if the letters can be rearranged so that two characters that are adjacent to each other are not the same.

If possible, output any possible result. If not possible, return the empty string.

Example 1:

Input: S = “aab”
Output: “aba”
Example 2:

Input: S = “aaab”
Output: “”

358. Rearrange String k Distance Apart

Given a non-empty string str and an integer k, rearrange the string such that the same characters are at least distance k from each other.

All input strings are given in lowercase letters. If it is not possible to rearrange the string, return an empty string “”.
Example 1:
str = “aabbcc”, k = 3

Result: “abcabc”

The same letters are at least distance 3 from each other.

Example 2:
str = “aaabc”, k = 3

Answer: “”

It is not possible to rearrange the string.

class Solution {
public:
    string reorganizeString(string S) {
        map<char,int>m;
        for(char c:S){
            m[c]++;
        }
        priority_queue<pair<int,char>> pq;
        for(auto mm:m){
            pq.push({mm.second,mm.first});
        }        
        string re;
        int len=S.size();
        queue<pair<int,char>> q;
        while(len>0){
            if(pq.size()==0)
                return "";
            auto tmp=pq.top();
            pq.pop();
            re+=tmp.second;
            len--;
            tmp.first--;
            q.push(tmp);
            if(q.size()>=3){
                auto t=q.front();
                if(t.first>0)
                    pq.push(t);
                q.pop();
            }
        }
        return re;
    }
};

你可能感兴趣的:(刷题)