1051. Height Checker

Sort

T: O(nlogn)

S: O(n)

class Solution {
    public int heightChecker(int[] heights) {
        int[] sortedHeights = heights.clone();
        Arrays.sort(sortedHeights);
        int count = 0;
        for (int i = 0; i < heights.length; i++) {
            if (sortedHeights[i] != heights[i]) {
                count++;
            }
        }
        return count;
    }
}

Bucket sort

T: O(max), see if max is too large or not

S: O(max)

Last updated

Was this helpful?