leetcode-HouseRobber

这道题比较简单,所以我会介绍的比较粗略:

题目:

  有一个小偷想沿着马路上的房子偷东西,每家每户都有一些钱,但这条街上装了监控系统,如果相邻的两户人家都被偷了的话那么就会触发报警器。小偷的目标就是在不触发报警器的前提下,如何才能偷到最多的钱?

解答:

  这道题目给的提示是使用dynamic programming来解决,也就是动态规划。动态规划的概念最重要的一点就是把问题一层层拆成规模更小的子问题。那么我们可以得出一套公式,给定nums为一维数组,数组元素代表每家所有的钱:

F(1) = nums[0]

F(2) = max(nums[0], nums[1])

F(3) = max(F(2), F(1) + nums[2])

...

F(n) = max(F(n-2) + nums[n-1], F(n-1))

PS.可能在第三步这里我们不能够准确的归纳出正确的公式,需要我们细心观察

  下面献上python代码:

class Solution(object):
    def rob(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        last, now = 0, 0
        for i in nums:
            last, now = now, max(last+i, now)
        return now
时间: 2024-11-05 23:25:28

leetcode-HouseRobber的相关文章

[LeetCode] HouseRobber 动态规划

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 autom

LeetCode—House Robber 寻找数组不相邻组合最大值DP

https://leetcode.com/problems/house-robber/ 题目设计了一个抢劫犯的情景,其实就是求数组中不相邻数据进行组合得到的最大值 举一个例子 假设数据: 8 3 6 15 4 9 7 10 那么首先可能选取 8 , 3 每一个数字的选取都是根据他的前两个数字,前三个数字得到的最大值进行选择,等到6的时候考虑前面只能和8组合  8,3,14 到数字15,那么就可以考虑在8,3中进行组合  8,3,14,23,4,9,7,10 接下来的步骤: 8,3,14,23,1

[LeetCode][JavaScript]House Robber

House Robber 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 

LeetCode中 House Robber问题随笔

先附上题目链接,有两问: https://leetcode.com/problems/house-robber/ https://leetcode.com/problems/house-robber-ii/ 第一问题目如下: You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constrain

【Leetcode】House Robber

题目链接:https://leetcode.com/problems/house-robber/ 题目: 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 ho

leetcode 198. House Robber (Easy)

https://leetcode.com/problems/house-robber/ 题意: 一维数组,相加不相邻的数组,返回最大的结果. 思路: 一开始思路就是DP,用一维数组保存dp[i]保存如果偷第i间,此时可偷到多少.DP的方向不太好,所以效率很低. Runtime: 4 ms, faster than 17.53% class Solution { public: int rob(vector<int> &nums) { int res = 0; int len = num

[LeetCode]198. 打家劫舍(DP)

题目 你是一个专业的小偷,计划偷窃沿街的房屋.每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警. 给定一个代表每个房屋存放金额的非负整数数组,计算你在不触动警报装置的情况下,能够偷窃到的最高金额. 示例 1: 输入: [1,2,3,1] 输出: 4 解释: 偷窃 1 号房屋 (金额 = 1) ,然后偷窃 3 号房屋 (金额 = 3). ? 偷窃到的最高金额 = 1 + 3 = 4 . 示例 2: 输入:

[LeetCode] 349 Intersection of Two Arrays &amp; 350 Intersection of Two Arrays II

这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/intersection-of-two-arrays/description/ 350 Intersection of Two Arrays II:https://leetcode.com/problems/intersection-of-two-arrays-ii/description/ 题目&解法

LeetCode 442. Find All Duplicates in an Array (在数组中找到所有的重复项)

Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements that appear twice in this array. Could you do it without extra space and in O(n) runtime? Example: Input: [4,3,2,7,

LeetCode OJ - Sum Root to Leaf Numbers

这道题也很简单,只要把二叉树按照宽度优先的策略遍历一遍,就可以解决问题,采用递归方法越是简单. 下面是AC代码: 1 /** 2 * Sum Root to Leaf Numbers 3 * 采用递归的方法,宽度遍历 4 */ 5 int result=0; 6 public int sumNumbers(TreeNode root){ 7 8 bFSearch(root,0); 9 return result; 10 } 11 private void bFSearch(TreeNode ro