Skip to content

[서인] 10-2. 돌다리 건너기 #257

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open

Conversation

Seoin02
Copy link
Contributor

@Seoin02 Seoin02 commented Sep 21, 2024

✔ 문제 제목: 돌다리 건너기

✔ 문제 유형: DP

✔ 문제 풀이 날짜: 2024.09.21

💡 문제 분석 요약

  • 철수는 돌 n개를 건너 개울을 건넌다.
  • 철수는 한 번에 한 개 또는 두 개씩 건너뛰면서 돌다리를 건넌다.

💡 알고리즘 설계

  • dp 배열을 선언한다.
  • 현재 밟고 있는 돌을 건너는 방법의 수는 직전 돌을 건너는 방법의 수와 그 이전 돌을 건너는 방법의 수를 더하면 된다.

💡코드

function solution(n) {
  if (n === 1) return 1;
  if (n === 2) return 2;

  let dp = new Array(n + 1).fill(0);
  dp[1] = 1;
  dp[2] = 2;

  for (let i = 3; i <= n + 1; i++) {
    dp[i] = dp[i - 1] + dp[i - 2]
  }

  return dp[n + 1]
}

💡 시간복잡도

O(n)

💡 틀린 이유와 틀린 부분 수정 or 다른 풀이

// 다른 풀이를 작성해주세요 !

💡 느낀 점 or 기억할 정보

  • 10-1 문제와 풀이가 비슷합니다!

Copy link
Contributor

@devwqc devwqc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

좋아요~ 🥌

저는 for 문으로 마지막 돌다리 까지만 계산하고 나와서 육지에 도착하는 경우를 더해줬는데 아예 육지까지 도착하는 경우를 계산하면 되는 거였네요! 👏🌠👏🌠👏🌠👏🌠👏🌠👏🌠👏🌠👏🌠👏🌠👏🌠👏🌠👏🌠👏🌠👏🌠

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants