LeetCode "Rearrange String k Distance Apart"

Greedy using priority_queue and hashmap. The basic idea is to have k buckets - and we fill it greedily.

And I agree that the code below can be cleaner:

struct Rec
{
    Rec(char _c, int _cnt) :c(_c), cnt(_cnt) {}
    char c;
    int cnt;
};

struct Cmp
{
    bool operator()(const Rec &r1, const Rec &r2)
    {
        return r1.cnt < r2.cnt;
    }
};
class Solution {
public:
    string rearrangeString(string str, int k)
    {
        if (k < 2)return str;
        int n = str.length();

        //    cnt
        int maxcnt = 0;
        vector<int> cnt(26);
        for (auto c : str)
        {
            cnt[c - ‘a‘] ++;
            maxcnt = max(maxcnt, cnt[c - ‘a‘]);
        }
        //    Check validity
        int max_need = (maxcnt - 1) * k + 1;
        if (max_need > n) return "";

        //    Queue
        priority_queue<Rec, vector<Rec>, Cmp> q;
        for (int i = 0; i < 26; i++)
        {
            if (cnt[i] > 0)
            {
                q.push(Rec(i + ‘a‘, cnt[i]));
            }
        }

        //
        vector<string> secs(maxcnt);
        int j = 0;
        while (!q.empty())
        {
            Rec r = q.top(); q.pop();
            for (int i = 0; i < r.cnt; i++)
            {
                secs[j] += r.c;
                j = (j + 1) % maxcnt;
            }
        }

        string ret;
        for (int i = 0; i < secs.size(); i++)
        {
            if (i < secs.size() - 1 && secs[i].length() < k) return "";
            ret += secs[i];
        }

        return ret;
    }
};
时间: 2024-11-05 06:27:38

LeetCode "Rearrange String k Distance Apart"的相关文章

[LeetCode] Rearrange String k Distance Apart 按距离为k隔离重排字符串

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 empt

Rearrange String k Distance Apart

1 public class Solution { 2 public String rearrangeString(String str, int k) { 3 if (k <= 1) { 4 return str; 5 } 6 7 Map<Character, Integer> letters = new HashMap<>(); 8 for (char c : str.toCharArray()) { 9 letters.put(c, letters.getOrDefau

621. Task Scheduler &amp;&amp; Rearrange String k Distance Apart

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 cou

LeetCode: Interleaving String

LeetCode: Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 = "aabcc", s2 = "dbbca", When s3 = "aadbbcbcac", return true. When s3 = "aadbbbaccc"

leetcode第一刷_Edit Distance

最小编辑距离,很经典的问题,今年微软实习生的笔试有一个这个的扩展版,牵扯到模板之类的,当时一行代码也没写出来.. dp可以很优雅的解决这个问题,状态转移方程也很明确.用pos[i][j]表示word1的前i个字符与word2的前j个字符之间的编辑距离.如果word[i-1]与word[j-1]相等,那pos[i][j]与pos[i-1][j-1]相等,否则的话,根据编辑的几种操作,可以从三种情况中选取代价最小的一种,从word1中删除一个字符?从word2中删除一个字符?修改其中一个?边界也很简

[LeetCode] Interleaving String [30]

题目 Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 = "aabcc", s2 = "dbbca", When s3 = "aadbbcbcac", return true. When s3 = "aadbbbaccc", return false. 原题链接(点我) 解题

[LeetCode] 244. Shortest Word Distance II 最短单词距离 II

This is a follow up of Shortest Word Distance. The only difference is now you are given the list of words and your method will be called repeatedly many times with different parameters. How would you optimize it? Design a class which receives a list

[LeetCode] 243. Shortest Word Distance 最短单词距离

Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list. For example,Assume that words = ["practice", "makes", "perfect", "coding", "makes"]. G

LeetCode:String to Integer (atoi)

1.题目名称 String to Integer (atoi) (字符串到数字的转换) 2.题目地址 https://leetcode.com/problems/string-to-integer-atoi/ 3.题目内容 英文:Implement atoi to convert a string to an integer. 中文:实现atoi函数,将输入的字符串(String类型)转换为整型数据(Integer类型) 提示:实现的atoi函数需要满足以下特征 忽略字符串第一个非空格字符前的所