LeetCode字母排列

这篇博客探讨了三个LeetCode题目,分别是621. Task Scheduler,关于CPU任务调度;767. Reorganize String,要求重新排列字符串使得相邻字符不同;358. Rearrange String k Distance Apart,需要将字符串重新排列使得相同字符间至少间隔k个位置。对于每个题目,都给出了示例和问题背景。


主要思路是贪婪算法,先借助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;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值