알고리즘 풀이
.leetcode(1051. Height Checker)
silvergoni
2022. 4. 26. 07:22
반응형
https://leetcode.com/problems/height-checker/
1. 2022/04/26 시도
소요시간: 4분
class Solution {
public int heightChecker(int[] heights) {
int[] copied = new int[heights.length];
for (int i=0; i<heights.length; i++) {
copied[i] = heights[i];
}
Arrays.sort(copied);
int counter =0;
for (int i=0; i<heights.length; i++) {
if (copied[i] != heights[i]) {
counter++;
}
}
return counter;
}
}
풀이 접근 과정
비교를 위해 복사본을 만든다
만든 복사본을 정렬한다.
정렬한 내용을 원본과 비교하여 다른 숫자의 갯수를 구한다.
느낀점
- 더 간단히 하는 방법이 있을지 고민된다.
알고리즘 정리노트: .leetcode(알고리즘 문제풀이 접근)
반응형