LeetCode 198 House Robber(强盗盗窃最大值)(动态规划)(*)

简介: 版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/50560560 翻译你是一个专业强盗,并计划沿街去盗窃每一个住户。
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/50560560

翻译

你是一个专业强盗,并计划沿街去盗窃每一个住户。

每个房子都有一定量的现金,阻止你盗窃的唯一阻碍是相邻的两个房子之间有安全系统。

一旦这两个房子同时被盗窃,系统就会自动联系警察。

给定一系列非负整数代表每个房子的金钱,

求出再不惊动警察的情况下能盗窃到的最大值。

原文

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.

分析

典型的动态规划问题。

int robIter(vector<int>& money, int c, int dp1, int dp2) {
    if (c >= money.size()) return dp2;
    else return robIter(money, c, dp2, max(dp1 + money[c++], dp2));
}

int rob(vector<int>& nums) {
    return robIter(nums, 0, 0, 0);
}

上面写的可能太简洁,这样或许方面理解一些:

int robIter(vector<int>& money, int c, int dp1, int dp2) {
    if (c >= money.size())
        return dp2;
    else {
        dp1 = max(dp1 + money[c++], dp2);
        return robIter(money, c, dp2, dp1);
    }
}

int rob(vector<int>& nums) {
    return robIter(nums, 0, 0, 0);
}

代码

class Solution {
public:
int robIter(vector<int>& money, int c, int dp1, int dp2) {
    if (c >= money.size()) return dp2;
    else return robIter(money, c, dp2, max(dp1 + money[c++], dp2));
}

int rob(vector<int>& nums) {
    return robIter(nums, 0, 0, 0);
}
};
目录
相关文章
|
6月前
|
机器学习/深度学习 算法 Go
【LeetCode 热题100】139:单词拆分(动态规划全解析+细节陷阱)(Go语言版)
本题是 LeetCode 热题 139:单词拆分(Word Break),需判断字符串 `s` 是否能由字典 `wordDict` 中的单词拼接而成。通过动态规划(DP)或记忆化搜索解决。DP 中定义布尔数组 `dp[i]` 表示前 `i` 个字符是否可拆分,状态转移方程为:若存在 `j` 使 `dp[j]=true` 且 `s[j:i]` 在字典中,则 `dp[i]=true`。初始条件 `dp[0]=true`。代码实现中用哈希集合优化查找效率。记忆化搜索则从起始位置递归尝试所有切割点。两种方法各有利弊,DP 更适合面试场景。思考扩展包括输出所有拆分方式及使用 Trie 优化大字典查找。
179 6
|
缓存
力扣每日一题 6/14 动态规划+数组
力扣每日一题 6/14 动态规划+数组
109 1
|
算法 索引
力扣每日一题 6/28 动态规划/数组
力扣每日一题 6/28 动态规划/数组
121 0
|
存储
力扣每日一题 6/19 排序+动态规划
力扣每日一题 6/19 排序+动态规划
107 0
|
算法
力扣每日一题 6/16 字符串 + 随机一题 动态规划/数学
力扣每日一题 6/16 字符串 + 随机一题 动态规划/数学
113 0
|
机器人
【LeetCode】--- 动态规划 集训(二)
【LeetCode】--- 动态规划 集训(二)
78 0
【LeetCode】--- 动态规划 集训(一)
【LeetCode】--- 动态规划 集训(一)
92 0
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
209 6
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
152 6
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
321 2

热门文章

最新文章