[LeetCode] String Reorder Distance Apart

Question:

Given a string of lowercase characters, reorder them such that the same characters are at least distance d from each other.

Input: { a, b, b }, distance = 2

Output: { b, a, b }

http://leetcode.com/2010/05/here-is-another-google-phone-interview.html

public void reorder(int[] A, int d)
{
    // Stats
    Map<Integer, Integer> map = new HashMap<>();
    for (int i : A) 
    {
        Integer occurance = map.get(i);
        if (occurance == null)
            occurance = 0;
        occurance++;
        map.put(i, occurance);
    }
    
    // Sort
    List<Map.Entry<Integer, Integer>> list = new ArrayList<>(map.entrySet());
    Collections.sort(list, new Comparator<Map.Entry<Integer, Integer>>()
    {
        public int compare(Entry<Integer, Integer> a, Entry<Integer, Integer> b) 
        {
            return -1 * Integer.compare(a.getValue(), b.getValue());
        }
    });
    
    // Re-assign
    int curIndex = 0;
    Map.Entry<Integer, Integer> cur = list.get(curIndex);
    int curOccurance = 0;
    for (int offset = 0; offset < d; offset++)
    {
        for (int i = offset; i < A.length; i += d)
        {
            A[i] = cur.getKey();
            curOccurance++;
            
            if (curOccurance == cur.getValue())
            {
                curIndex++;
                if (curIndex == list.size())
                    return;
                cur = list.get(curIndex);
                curOccurance = 0;
            }
        }
    }
}
时间: 2024-10-11 05:18:14

[LeetCode] String Reorder Distance Apart的相关文章

【leetcode】Edit Distance 详解

下图为TI C6xx DSP Nyquist总线拓扑图,总线连接了master与slave,提供了高速的数据传输.有很多种速率不同的总线,如图中的红色方框,最高速总线为CPU/2 TeraNet SCR(即VBUSM SCR),带宽为256bit,其他低速总线为CPU/3,CPU/6,带宽参考图中所示.总线之间用Bridge(桥)连接,作用包括转换总线的速率,使之与所流向总线的速率相同等. 在具体应用中,各种速率的总线完全可以满足复杂的数据传输,而数据传输的瓶颈往往在于连接总线之间的Bridge

leetcode——String to Integer (atoi) 字符串转换为整型数(AC)

mnesia在频繁操作数据的过程可能会报错:** WARNING ** Mnesia is overloaded: {dump_log, write_threshold},可以看出,mnesia应该是过载了.这个警告在mnesia dump操作会发生这个问题,表类型为disc_only_copies .disc_copies都可能会发生. 如何重现这个问题,例子的场景是多个进程同时在不断地mnesia:dirty_write/2 mnesia过载分析 1.抛出警告是在mnesia 增加dump

LeetCode——String to Integer (atoi)

Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes: It is intended for this problem to be spe

[leetcode] [leetcode] String to Integer (atoi)

Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes: It is intended for this problem to be spe

[LeetCode] String to Integer (atoi) [7]

题目 Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes: It is intended for this problem to be

[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

LeetCode &quot;Rearrange String k Distance Apart&quot;

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 ope

[LeetCode] One Edit Distance 一个编辑距离

Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance的拓展,然而这道题并没有那道题难,这道题只让我们判断两个字符串的编辑距离是否为1,那么我们只需分下列三种情况来考虑就行了: 1. 两个字符串的长度之差大于1,那么直接返回False 2. 两个字符串的长度之差等于1,那么长的那个字符串去掉一个字符,剩下的应该和短的字符串相同 3. 两个字符串的长度之

LeetCode One Edit Distance

原题链接在这里:https://leetcode.com/problems/one-edit-distance/ Given two strings S and T, determine if they are both one edit distance apart. 与Edit Distance类似. 若是长度相差大于1, return false. 若是长度相差等于1, 遇到不同char时, 长的那个向后挪一位. 若是长度相等, 遇到不同char时同时向后挪一位. 出了loop还没有返回,