반응형
https://leetcode.com/problems/height-checker
카운트 정렬을 통해 O(n)에 풀 수 있었습니다.
class Solution {
public:
int heightChecker(vector<int>& heights) {
int cnt[101] = {0};
for(int h : heights) cnt[h]++;
int res = 0, idx = 0;
for(int i=1; i<=100; i++) {
while(cnt[i]--) {
if(heights[idx++] != i) res++;
}
}
return res;
}
};
반응형
'기록' 카테고리의 다른 글
LeetCode 1791. Find Center of Star Graph (0) | 2024.06.29 |
---|---|
LeetCode 2192. All Ancestors of a Node in a Directed Acyclic Graph (0) | 2024.06.29 |
LeetCode 945. Minimum Increment to Make Array Unique (0) | 2024.06.15 |
LeetCode 1122. Relative Sort Array (0) | 2024.06.12 |
LeetCode 846. Hand of Straights (1) | 2024.06.12 |