LintCode-Sort Colors II
2014-12-30 06:22
323 查看
Given an array of n objects with k different colors (numbered from 1 to k), sort them so that objects of the same color are adjacent, with the colors in the order 1, 2, ... k.
Note
You are not suppose to use the library's sort function for this problem.
Example
GIven colors=[3, 2, 2, 1, 4], k=4, your code should sort colors in-place to [1, 2, 2, 3, 4].
Challenge
A rather straight forward solution is a two-pass algorithm using counting sort. That will cost O(k) extra memory.
Can you do it without using extra memory?
Analysis:
Sort: O(nlogn), quick or merge.
O(n): use the array itself as space to store counts. We use A[k-1] to store the count of color k. We use negtive number to store count, in order to be distnct with the color value. This method ASSUMES that every color between 1 and k will appear.
At position i, we check the value of A[A[i]-1], if it is a positive number, i.e., not counted yet, we then put A[A[i]-1] to A[i], and set A[A[i]-1] as -1 to indicate that there is one of this color.
If A[A[i]-1] is a negtive or zero value, we then simply decrease it by one and set A[i] as 0 to indicate that this position is couted already.
At position i, we repeat this procedure until A[i] becomes 0 or negtive, we then move to i+1.
At counting, we draw colors into array.
Solution:
Note
You are not suppose to use the library's sort function for this problem.
Example
GIven colors=[3, 2, 2, 1, 4], k=4, your code should sort colors in-place to [1, 2, 2, 3, 4].
Challenge
A rather straight forward solution is a two-pass algorithm using counting sort. That will cost O(k) extra memory.
Can you do it without using extra memory?
Analysis:
Sort: O(nlogn), quick or merge.
O(n): use the array itself as space to store counts. We use A[k-1] to store the count of color k. We use negtive number to store count, in order to be distnct with the color value. This method ASSUMES that every color between 1 and k will appear.
At position i, we check the value of A[A[i]-1], if it is a positive number, i.e., not counted yet, we then put A[A[i]-1] to A[i], and set A[A[i]-1] as -1 to indicate that there is one of this color.
If A[A[i]-1] is a negtive or zero value, we then simply decrease it by one and set A[i] as 0 to indicate that this position is couted already.
At position i, we repeat this procedure until A[i] becomes 0 or negtive, we then move to i+1.
At counting, we draw colors into array.
Solution:
class Solution { /** * @param colors: A list of integer * @param k: An integer * @return: nothing */ public void sortColors2(int[] colors, int k) { //The method assumes that every color much appear in the array. int len = colors.length; if (len<k) return; //count the number of each color. for (int i=0;i<len;i++){ while (colors[i]>0){ int key = colors[i]-1; if (colors[key]<=0){ colors[key]--; colors[i]=0; } else { colors[i] = colors[key]; colors[key] = -1; } } } //draw colors. int index = len-1; int cInd = k-1; while (cInd>=0){ int num = Math.abs(colors[cInd]); for (int i=0;i<num;i++){ colors[index]=cInd+1; index--; } cInd--; } } }
相关文章推荐
- LintCode 143. Sort Colors II
- Lintcode: Sort Colors II 解题报告
- lintcode:Sort Colors II
- Lintcode: Sort Colors II
- LintCode "Sort Colors II"
- lintcode:Wiggle Sort II
- lintcode:Sort Colors
- LintCode: Sort Colors
- Sort Colors II
- LintCode "Continuous Subarray Sum II"
- lintcode-36-翻转链表 II
- lintcode(666)Guess Number Game II
- [LintCode]Sort Colors II
- LintCode Find Peak Element II(查找峰值II)
- LINTCODE——房屋染色II
- lintcode--42. 最大子数组 II
- Lintcode 4 Ugly Number II
- 剑指offer(LintCode):112. 删除链表中重复节点I VS 113.删除链表中重复节点II
- LintCode--买卖股票的最佳时机 II
- LeetcCode Sort Colors