Skip to content

Latest commit

 

History

History
57 lines (43 loc) · 1.39 KB

_1051. Height Checker.md

File metadata and controls

57 lines (43 loc) · 1.39 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : June 10, 2024

Last updated : July 01, 2024


Related Topics : Array, Sorting, Counting Sort

Acceptance Rate : 80.95 %


Solutions

C

int compareHelper(const void* a, const void* b) {
    return *((int*) a) - *((int*) b);
}
int heightChecker(int* heights, int heightsSize) {
    int sortedHeights[heightsSize];
    for (int i = 0; i < heightsSize; i++) {
        sortedHeights[i] = heights[i];
    }

    qsort(sortedHeights, heightsSize, sizeof(int), compareHelper);

    int counter = 0;
    for (int i = 0; i < heightsSize; i++) {
        if (sortedHeights[i] != heights[i])
            counter++;
    }

    return counter;
}

Python

class Solution:
    def heightChecker(self, heights: List[int]) -> int:
        sortedHeights = sorted(heights)
        return len(heights) - [heights[x] - sortedHeights[x] for x in range(len(heights))].count(0)