1051. Height Checker
All prompts are owned by LeetCode. To view the prompt, click the title link above.
First completed : June 10, 2024
Last updated : July 01, 2024
Related Topics : Array, Sorting, Counting Sort
Acceptance Rate : 80.95 %
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;
}
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)