[LeetCode] String Reorder Distance Apart
2015-01-18 14:14
1626 查看
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
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; } } } }
相关文章推荐
- String Reorder Distance apart
- 【LeetCode 358】 Rearrange String k Distance Apart
- LeetCode Rearrange String k Distance Apart
- Leetcode: Rearrange String k Distance Apart
- [LeetCode] Rearrange String k Distance Apart 按距离为k隔离重排字符串
- LeetCode "Rearrange String k Distance Apart"
- LeetCode-Rearrange String k Distance Apart
- Rearrange String k Distance Apart
- 【微软2014实习生及秋令营技术类职位在线测试】题目1 : String reorder
- LeetCode之“链表”:Reorder List
- LeetCode() Reorder List
- leetcode 【 Reorder List 】python 实现
- leetcode Reorder List
- LeetCode--Reorder List
- LeetCode (Reorder List)
- [leetcode] Reorder List
- Reorder List|leetcode题解
- Leetcode -- Reorder List
- [Leetcode]Reorder List
- 【leetcode】Reorder List (python)