[프로그래머스]-구명보트

김린네·2022년 12월 14일
post-thumbnail

남들이 하는 코드랑 비교하면서 했는데 이 splice 함수를 이용하는 과정에서 오류가 난거 같다..
결국 splice 함수를 사용하지 않고 two point 형식으로 가야된다..


const solution = function (arr, limit) {
   arr= arr.sort((a,b)=> a-b)
  var go = arr.length - 1;
  var count = 0;
  while (go >= 0) {
    var check = limit - arr[go];
    if (arr[0] > check) {
      count += 1;
      go -= 1;
    }
    else {
      const find_index = arr.findIndex((e) => e <= check);
      go -= 2;
      arr.splice(find_index, 1);
      count += 1;
    }
  }

  return count

}

내가 쓴 코드는 two point 을 응용한 코드다.

응용하지 않고 > splice함수를 사용하지 않고 쉽게 풀자..
단순한 문제이다.

profile
디자인 > https://dribbble.com/jongpil_77 코딩 > https://www.codewars.com/users/bikijjang

0개의 댓글