[LeetCode] 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 it will automatically contact the police if two adjacent houses were broken into on the same night.

Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.

解题思路

动态规划
状态转移方程:f[i] =max(f[i-1], f[i-2]+c[i])
f[i]表示进入第i+1个房间时所得到的最大财富。
为了节省空间,只是用三个变量prepre, pre, cur即可。

实现代码1

//Runtime:2ms
class Solution {
public:
    int rob(vector<int>& nums) {
        int len = nums.size();
        if (len == 0) return 0;
        if (len == 1) return nums[0];
        if (len == 2) return max(nums[0], nums[1]);
        int prepre = nums[0];
        int pre = max(nums[0], nums[1]);
        int cur;
        for (int i = 2; i < len; i++)
        {
            cur = max(pre, prepre + nums[i]);
            prepre = pre;
            pre = cur;
        }
        return cur;
    }
};

实现代码2

# Runtime:76ms
class Solution:
    # @param {integer[]} nums
    # @return {integer}
    def rob(self, nums):
        size = len(nums)
        if size == 0:
            return 0
        elif size == 1:
            return nums[0]
        elif size == 2:
            return max(nums[0], nums[1])

        prepre = nums[0]
        pre = max(nums[0], nums[1])
        for i in range(2, size):
            cur = max(pre, prepre + nums[i])
            prepre = pre
            pre = cur

        return cur
时间: 2024-10-22 22:00:33

[LeetCode] House Robber的相关文章

【LeetCode从零单排】No198.House Robber &amp;amp;&amp;amp;No91.Decode Ways&amp;amp;&amp;amp;139 word break(动态规划典型应用)

1.题目 一道典型的Dynamic Programming的题目. 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

[LeetCode]198.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 it will au

LeetCode All in One 题目讲解汇总(持续更新中...)

终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通过OJ,或是有更好的解法,或是有任何疑问,意见和建议的话,请一定要在对应的帖子下面评论区留言告知博主啊,多谢多谢,祝大家刷得愉快,刷得精彩,刷出美好未来- 博主制作了一款iOS的应用"Leetcode Meet Me",里面有Leetcode上所有的题目,并且贴上了博主的解法,随时随地都能

代码分析-leetcode:Scramble String问题

问题描述 leetcode:Scramble String问题 题目:Given a string s1 we may represent it as a binary tree by partitioning it to two non-empty substrings recursively. Below is one possible representation of s1 = ""great"": great / gr eat / / g r e at /

c++-leetcode Median of Two Sorted Arrays

问题描述 leetcode Median of Two Sorted Arrays class Solution { public: double findMedianSortedArrays(vector& nums1, vector& nums2) { if(nums1.size()==0){ if(nums2.size()%2==0)return ((double)nums2[nums2.size()/2]+nums2[nums2.size()/2-1])/2.0; else ret

[LeetCode]61.Rotate List

[题目] Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL. [题意] 给定一个链表,向右旋转k个位置,其中k是非负的. [分析] 先遍历一遍,得出链表长度 len,注意 k 可能大于

[LeetCode]91.Decode Ways

题目 A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 - 'Z' -> 26 Given an encoded message containing digits, determine the total number of ways to decode it. For example, Given encode

LeetCode总结之二分查找篇

二分查找算法虽然简单,但面试中也比较常见,经常用来在有序的数列查找某个特定的位置.在LeetCode用到此算法的主要题目有: Search Insert Position Search for a Range Sqrt(x) Search a 2D Matrix Search in Rotated Sorted Array Search in Rotated Sorted Array II 这类题目基本可以分为如下四种题型: 1. Search Insert Position和Search fo

Restore IP Addresses:LeetCode

原题链接: http://oj.leetcode.com/problems/restore-ip-addresses/ 这道题的解法非常接近于NP问题,也是采用递归的解法.基本思路就是取出一个合法的数字,作为IP地址的一项,然后递归处理剩下的项.可以想象出一颗树,每个结点有三个可能的分支(因为范围是0-255,所以可以由一位两位或者三位组成).并且这里树的层数不会超过四层,因为IP地址由四段组成,到了之后我们就没必要再递归下去,可以结束了.这里除了上述的结束条件外,另一个就是字符串读完了.可以看