Skip to content

Commit 9ede0c0

Browse files
committed
Solution House Robber
1 parent 7a636df commit 9ede0c0

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

house-robber/HISEHOONAN.swift

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// House_Robber .swift
3+
// Algorithm
4+
//
5+
// Created by 안세훈 on 3/31/25.
6+
//
7+
///https://leetcode.com/problems/house-robber/description/
8+
9+
class Solution {
10+
//Dynamic Programming
11+
func rob(_ nums: [Int]) -> Int {
12+
13+
if nums.count == 1 {return nums[0]} //배열이 1개인 경우
14+
if nums.count == 2 {return max(nums[0],nums[1])} //배열이 2개인 경우
15+
16+
var dp = [nums[0], max(nums[0],nums[1])]
17+
//제일 base가 되는 두 값을 찾는게 제일 중요함.
18+
//nums[0]은 nums[1]보다 무조건 작아야함.
19+
for i in 2..<nums.count{
20+
dp.append(max(dp[i-2]+nums[i],dp[i-1]))
21+
//dp배열의 i-2번째 배열 + nums의 i번째 배열 vs dp의 i-1번째 배열 중 큰걸 append
22+
//
23+
}
24+
25+
return dp[dp.count-1]
26+
}
27+
}

0 commit comments

Comments
 (0)