27주차 알고리즘

이동규·2024년 9월 23일

코테

목록 보기
18/22

카펫

function solution(brown = 10, yellow = 2) {
  let answer = [0, 0];
  let result = brown + yellow;

  for (let index = 1; index < result; index++) {
    for (let i = 3; i < result; i++) {
      if (
        index * i === result &&
        index >= i &&
        (index - 2) * (i - 2) === yellow
      ) {
        answer[0] = index;
        answer[1] = i;
          return answer;
      }
    }
  }

  return answer;
}

점프와 순간이동

function solution(n) {
  let ans = 1;

  while (n !== 1) {
    if (Number.isInteger(n / 2)) {
      n /= 2;
    } else {
      --n;
      ++ans;
    }
  }
 
  return ans;
}

구명보트

function solution(people = [70, 80, 50], limit = 100) {
  let answer = 0;

  let i = 0;
  let j = people.length - 1;
  people.sort((a, b) => a - b);
  while (i <= j) {
    if (people[i] + people[j] <= limit) {
      i++;
      j--;
    } else {
      j--;
    }
    ++answer;
  }

  return answer;
}

0개의 댓글