Online Judge Solutions

Sunday, December 28, 2014

Sort Colors II

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?
 
class Solution{
public:
    /**
     * @param colors: A list of integer
     * @param k: An integer
     * @return: nothing
     */    
    void sortColors2(vector<int> &colors, int k) {
        int n = colors.size();
        sort(colors, 0, n-1, 1, k);
    }
    
    void swap(vector<int> &colors, int i, int j) 
    {
        int t = colors[i];
        colors[i] = colors[j];
        colors[j] = t;
    }
    void sort(vector<int> &colors, int left, int right, int low, int high)
    {
        if (high == low || left >= right) return;
        
        int L = left, R = right;
        
        int mid = (low+high)/2;
        
        while(L <= R){
            while( L <= R && colors[L] <= mid) L++;
            while( L <= R && colors[R] > mid) R--;
            if (L < R) swap(colors, L, R);   
        }
        
        sort(colors, left, R, low, mid);
        sort(colors, R+1, right, mid+1, high);
    }
};

// Credit to http://www.cnblogs.com/yuzhangcmu/p/4177326.html
//inplace,并且O(N)时间复杂度的算法。
// 1. 从左扫描到右边,遇到一个数字,先找到对应的bucket.比如
//    3 2 2 1 4
//    第一个3对应的bucket是index = 2 (bucket从0开始计算)
// 2. Bucket 如果有数字,则把这个数字移动到i的position(就是存放起来),然后把bucket记为-1(表示该位置是一个计数器,计1)。
// 3. Bucket 存的是负数,表示这个bucket已经是计数器,直接减1. 并把color[i] 设置为0 (表示此处已经计算过)
// 4. Bucket 存的是0,与3一样处理,将bucket设置为-1, 并把color[i] 设置为0 (表示此处已经计算过)
// 5. 回到position i,再判断此处是否为0(只要不是为0,就一直重复2-4的步骤)。
// 6.完成1-5的步骤后,从尾部到头部将数组置结果。(从尾至头是为了避免开头的计数器被覆盖)
// 例子(按以上步骤运算):

//  3 2 2 1 4
//  2 2 -1 1 4
//  2 -1 -1 1 4
//  0 -2 -1 1 4
// -1 -2 -1 0 4
// -1 -2 -1 -1 0

class Solution{
public:
    /**
     * @param colors: A list of integer
     * @param k: An integer
     * @return: nothing
     */    
    void sortColors2(vector<int> &colors, int k) {
        
        for(int i = 0; i < colors.size(); i++){
            if (colors[i] > 0) {
                int pos = colors[i]-1;
                if (colors[pos] <= 0) {
                    colors[pos]--;
                    colors[i] = 0; 
                }
                else {
                    colors[i] = colors[pos];
                    colors[pos] = -1;
                    i--;
                }
            }
        }
        
        int i = colors.size()-1;
        int j = k-1;
        while(j >= 0) {
            while(colors[j] < 0) {
                colors[j] += 1;
                colors[i--] = j+1;
            }
            j--;
        } 
    }
};

No comments:

Post a Comment