Files
LeetCode-Go/leetcode/0213.House-Robber-II/213. House Robber II.go
2020-08-07 17:06:53 +08:00

35 lines
636 B
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package leetcode
func rob213(nums []int) int {
n := len(nums)
if n == 0 {
return 0
}
if n == 1 {
return nums[0]
}
if n == 2 {
return max(nums[0], nums[1])
}
// 由于首尾是相邻的,所以需要对比 [0n-1]、[1n] 这两个区间的最大值
return max(rob213_1(nums, 0, n-2), rob213_1(nums, 1, n-1))
}
func rob213_1(nums []int, start, end int) int {
preMax := nums[start]
curMax := max(preMax, nums[start+1])
for i := start + 2; i <= end; i++ {
tmp := curMax
curMax = max(curMax, nums[i]+preMax)
preMax = tmp
}
return curMax
}
func max(a int, b int) int {
if a > b {
return a
}
return b
}