Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
18 lines (16 loc) · 604 Bytes

File metadata and controls

18 lines (16 loc) · 604 Bytes
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package ch23;
public class P94_2 {
public int rob(int[] nums) {
// 입력값이 1개인 경우 예외 처리
if (nums.length == 1)
return nums[0];
int[] dp = new int[nums.length];
dp[0] = nums[0];
dp[1] = Math.max(dp[0], nums[1]);
// 이전 결과와 (전전 결과 + 현재 결과) 중 큰 값 저장
for (int i = 2; i < nums.length; i++)
dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i]);
// 항상 최댓값이 저장되므로 마지막 값을 정답으로 리턴
return dp[nums.length - 1];
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.