programmers_도둑질_java
2023. 3. 6. 21:40ㆍAlgorithm/Programmers
728x90
https://school.programmers.co.kr/learn/courses/30/lessons/42897#
class Solution {
public int solution(int[] money) {
int answer = 0;
int[][] dp = new int[money.length][2];
int moneySize = money.length;
dp[0][0] = money[0];
dp[1][0] = money[0];
for(int i = 2; i< moneySize-1 ; i++){
dp[i][0] = Math.max(money[i] + dp[i-2][0], dp[i-1][0]);
}
dp[0][1] = 0;
dp[1][1] = money[1];
for(int i = 2; i< moneySize; i++){
dp[i][1] = Math.max(money[i] + dp[i-2][1], dp[i-1][1]);
}
answer = Math.max(dp[moneySize-1][1], dp[moneySize-2][0]);
return answer;
}
}
'Algorithm > Programmers' 카테고리의 다른 글
programmers_합승 택시 요금_java (0) | 2023.03.12 |
---|---|
programmers_[카카오 인턴] 경주로 건설_java (0) | 2023.03.12 |
programmers_여행경로_java (0) | 2023.03.06 |
programmers_섬 연결하기_java (0) | 2023.03.06 |
programmers_가장 먼 노드_java (0) | 2023.03.06 |