[leetcode, JS] 1051. Height Checker

mxxn·2023년 9월 1일
0

leetcode

목록 보기
62/198

문제

문제 링크 : Height Checker

풀이

/**
 * @param {number[]} heights
 * @return {number}
 */
var heightChecker = function(heights) {
    const sortedHeights = [...heights].sort((a,b) => a-b)
    return sortedHeights.reduce((cnt,cur, idx) => cur !== heights[idx] ? cnt+1 : cnt, 0)
};
  1. sort한 heigts array 변수를 만들고
  2. 기존 heights array와 비교하여 다른 값일때만 cnt+1 하여 cnt return
  • Runtime 58 ms, Memory 41.6 MB
profile
내일도 글쓰기

0개의 댓글