Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

198. 打家劫舍 #33

Open
yankewei opened this issue Mar 23, 2020 · 0 comments
Open

198. 打家劫舍 #33

yankewei opened this issue Mar 23, 2020 · 0 comments
Labels
动态规划 题目包含动态规划解法 简单 题目难度为简单

Comments

@yankewei
Copy link
Owner

你是一个专业的小偷,计划偷窃沿街的房屋。每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警。

给定一个代表每个房屋存放金额的非负整数数组,计算你在不触动警报装置的情况下,能够偷窃到的最高金额。

示例 1:

输入: [1,2,3,1]
输出: 4
解释: 偷窃 1 号房屋 (金额 = 1) ,然后偷窃 3 号房屋 (金额 = 3)。
     偷窃到的最高金额 = 1 + 3 = 4 。

示例 2:

输入: [2,7,9,3,1]
输出: 12
解释: 偷窃 1 号房屋 (金额 = 2), 偷窃 3 号房屋 (金额 = 9),接着偷窃 5 号房屋 (金额 = 1)。
     偷窃到的最高金额 = 2 + 9 + 1 = 12 。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/house-robber

解答

看题目很明显是一个道动态规划题目

func rob(nums []int) int {
    d := [2]int{0, 0}
    for _,value := range nums {
	tmp := d[0]
	d[0] = d[1]
	d[1] = max(tmp + value, d[1])
    }
    return d[1]
}

func max(a int, b int) int {
    if a > b {
        return a
    }
    return b
}

相似题目

@yankewei yankewei added 简单 题目难度为简单 动态规划 题目包含动态规划解法 labels Mar 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
动态规划 题目包含动态规划解法 简单 题目难度为简单
Projects
None yet
Development

No branches or pull requests

1 participant