반응형
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(알고리즘 문제풀이 접근)
반응형
'알고리즘 풀이' 카테고리의 다른 글
.leetcode(414. Third Maximum Number) (0) | 2022.04.26 |
---|---|
.leetcode(487. Max Consecutive Ones II) (0) | 2022.04.26 |
.leetcode(27. Remove Element) (0) | 2022.04.26 |
.leetcode(905. Sort Array By Parity) (0) | 2022.04.25 |
.leetcode(283. Move Zeroes) (0) | 2022.04.25 |