GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode/python/213_House_Robber_II.py at master · qiyuangong/leetcode

Expand file tree

/

213_House_Robber_II.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
41 lines (38 loc) · 1.23 KB
/

213_House_Robber_II.py

Copy path

File metadata and controls

41 lines (38 loc) · 1.23 KB
Raw
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
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class Solution(object):
# def rob(self, nums):
# """
# :type nums: List[int]
# :rtype: int
# """
# if nums is None or len(nums) == 0:
# return 0
# ls = len(nums)
# dp = [0] * ls
# # dp from 0 ~ ls - 2
# dp[0] = nums[0]
# for i in range(1, ls - 1):
# if i < 2:
# dp[i] = max(nums[i], dp[i - 1])
# else:
# dp[i] = max(dp[i - 2] + nums[i], dp[i - 1])
# res = dp[ls - 2]
# # dp from 1 ~ ls - 1
# dp[0] = 0
# for i in range(1, ls):
# if i < 2:
# dp[i] = max(nums[i], dp[i - 1])
# else:
# dp[i] = max(dp[i - 2] + nums[i], dp[i - 1])
# return max(res, dp[ls - 1])
def rob(self, nums):
if len(nums) == 1:
return nums[0]
return max(self.rob_helper(nums, 0, len(nums) - 2),
self.rob_helper(nums, 1, len(nums) - 1))
def rob_helper(self, nums, low, high):
prevMax = currMax = 0
for index in range(low, high + 1):
temp = currMax
currMax = max(prevMax + nums[index], currMax)
prevMax = temp
return currMax
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.