2020년/코테

[코테 연습] House Robber

위지원 2020. 9. 15. 11:32

leetcode.com/problems/house-robber/

 

House Robber - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.

Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.


이 문제가 생각났다. 그렇다 동적 프로그래밍이다. 

2020/05/29 - [2020년 상반기/코테] - [코테 연습] 포도주 시식 Python

 

[코테 연습] 포도주 시식 Python

문제 효주는 포도주 시식회에 갔다. 그 곳에 갔더니, 테이블 위에 다양한 포도주가 들어있는 포도주 잔이 일렬로 놓여 있었다. 효주는 포도주 시식을 하려고 하는데, 여기에는 다음과 같은 두 가

weejw.tistory.com

바로 직전 집을 도둑질한다. + 지금 집을 도둑질하지 않는다.

바로 직전 집을 도둑질하지 않는다. + 지금 집을 도둑질한다.

중 더 큰 값을 취한다.

 

class Solution:
def rob(self, nums: List[int]) -> int:
dp = [0 for i in range(len(nums))]
print(dp)
listLen = len(nums)
if listLen == 0:
return 0
if listLen == 1:
return nums[0]
if listLen == 2:
return max(nums)
dp[0] = nums[0]
dp[1] = max(nums[0], nums[1])
for i in range(2, listLen):
dp[i] = max(nums[i]+dp[i-2], dp[i-1])
return dp[-1]
view raw house robber.py hosted with ❤ by GitHub