[Leetcode] Height Checker - 자바스크립트, JavaScript

Jin·2023년 3월 1일

Algorithm

목록 보기
10/13

문제

내 풀이

var heightChecker = function (heights) {
  let count = 0;

  const origin = [...heights];
  const expected = heights.sort((a, b) => a - b);

  for (let i = 0; i < expected.length; i++) {
    if (origin[i] !== expected[i]) count++;
  }

  return count;
};
  • Your runtime beats 75.79 % of javascript submissions.

다른 사람 풀이

var heightChecker = function (heights) {
  	const sortedHeight = [...heights].sort((a,b) => a - b);
  	return sortedHeight
    	.filter((v,i) => v !== heights[i])
  		.length;
};
profile
Nothing changes if nothing changes

0개의 댓글