Algorithm/LeetCode

[LeetCode] - Height Checker

JunGi Jeong 2021. 4. 23. 13:41

height 배열을 주고 오름차 순으로 정렬한다.

그 다음 원본 height 배열의 요소와 다른 값을 찾아서 누산한 다음에 리턴하는 문제

오늘 문제는 쉬웠다 좋다 :)

class Solution {
    public int heightChecker(int[] heights) {
        /*
        의사 코드
        정수 배열인 heights를 오름차순으로 정렬하면서 바뀌는 수를 누산하여 리턴
        */
        int change = 0;
		int[] copy = Arrays.copyOf(heights, heights.length);
		Arrays.sort(heights);
		
		for(int idx = 0; idx < heights.length; idx++) {
			if(heights[idx] != copy[idx]) change++;	
		}
		
		return change;
        
    }
}