Skip to content
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

[Algorithm] 땅따먹기 #106

Closed
hwangJi-dev opened this issue Feb 8, 2023 · 0 comments
Closed

[Algorithm] 땅따먹기 #106

hwangJi-dev opened this issue Feb 8, 2023 · 0 comments

Comments

@hwangJi-dev
Copy link
Owner

hwangJi-dev commented Feb 8, 2023

💬 문제

https://school.programmers.co.kr/learn/courses/30/lessons/12913


💬 Idea

  1. 원래는 땅의 최댓값을 다니면서 다음 땅에서 전 땅의 인덱스를 밟지 않으면서 최댓값인 땅을 가도록 아이디어를 생각했었다.
  2. 그러나 이렇게 되면 다음 땅일 때 최댓값을 도출할 수 있는 땅을 밟지 못할 수도 있다.
  3. 따라서 DP를 사용해서 풀어야 한다는 것을 알게 되었다.

💬 풀이

func solution(land:[[Int]]) -> Int {
    var dp = land
    
    for i in 0..<land.count - 1 {
        dp[i + 1][0] += max(dp[i][1], dp[i][2], dp[i][3])
        dp[i + 1][1] += max(dp[i][0], dp[i][2], dp[i][3])
        dp[i + 1][2] += max(dp[i][0], dp[i][1], dp[i][3])
        dp[i + 1][3] += max(dp[i][0], dp[i][1], dp[i][2])
    }
    
    return dp[land.count - 1].max()!
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant