[프로그래머스] 하노이의 탑 - JavaScript

이수동·2022년 6월 24일
0
post-thumbnail

프로그래머스 Level 2 - 하노이의 탑


📌 생각한 풀이 방법

  1. 재귀함수를 활용하여 모든 원반을 옮긴다.

📌 풀이

function solution(n) {
  let answer = [];

  function hanoi(n, start, end, mid) {
    if (n === 1) {
      return answer.push([start, end]);
    }
    hanoi(n - 1, start, mid, end);
    answer.push([start, end]);
    hanoi(n - 1, mid, end, start);
  }

  hanoi(n, 1, 3, 2);

  return answer;
}
profile
기록을 통한 성장하기 🧐

1개의 댓글

comment-user-thumbnail
2022년 10월 7일

actually this is my favorite topic, it's in vietnam and i've been there color tunnel

답글 달기