My AC java solution [LSD sorting]


  • 0
    G
    public void sortColors(int[] A) {
        int N = A.length;
        int[] aux = new int[N];
        int[] count = new int[4];
        //compute frequency counts //+1 will make sense when we accumulate count array
        for(int i=0; i<N;i++){
            
            count[A[i]+1]++;
        }
        //transform count to indexes
        for(int r=0;r<3;r++){
            count[r+1]+=count[r];
        }
        for(int i=0; i<N;i++){
            aux[count[A[i]]++]=A[i];
        }
        for(int i=0; i<N;i++){
            A[i]=aux[i];
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.