[LeetCode][Python3] 198. House Robber
                2018. 10. 1. 01:25 |
                
                    프로그래밍/LeetCode
                
            
            
            
        Problem :
https://leetcode.com/problems/house-robber/description/
My Solution :
class Solution:
    def rob(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        dp = [0] * (len(nums)+1)
        for i, n in enumerate(nums, 1):
            dp[i] = max(n + dp[i-2], dp[i-1])
        return dp[-1]
Comment :
전형적인 DP 문제이고 예전에 HackerRank에서 동일한 유형의 문제를 풀어봤기 때문에 쉽게 풀 수 있었다.
아래는 메모리 절약 버전
My Solution2 :
class Solution:
    def rob(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        a = b = 0
        for n in nums:
            a, b = b, max(n + a, b)
        return b
'프로그래밍 > LeetCode' 카테고리의 다른 글
| [LeetCode][Python3] 283. Move Zeroes (0) | 2018.10.05 | 
|---|---|
| [LeetCode][Python3] 234. Palindrome Linked List (0) | 2018.10.04 | 
| [LeetCode][Python3] 226. Invert Binary Tree (0) | 2018.10.03 | 
| [LeetCode][Python3] 206. Reverse Linked List (0) | 2018.10.01 | 
| [LeetCode][Python3] 169. Majority Element (0) | 2018.10.01 | 
| [LeetCode][Python3] 160. Intersection of Two Linked Lists (0) | 2018.09.28 | 
| [LeetCode][Python3] 155. Min Stack (0) | 2018.09.28 | 
| [LeetCode][Python3] 141. Linked List Cycle (0) | 2018.09.28 | 
최근에 달린 댓글 최근에 달린 댓글