当前位置:网站首页>Leetcode-198- house raiding
Leetcode-198- house raiding
2022-07-25 14:40:00 【z754916067】
subject

Ideas
- It feels like dynamic planning , Or from 0 Start grabbing Or from 1 Start grabbing
- dp[i] The representative must rob the i The maximum amount of account , But it may rob i-2 Hu Hejie i-3 Household , Because it's impossible to rob i-1 Household , And if robbery i-4 Households can also rob i-2 Household , So we only need to consider robbing i-2 and i-3j that will do
Code
public int rob(int[] nums) {
if(nums.length<2) return nums[0];
// It feels like dynamic planning
int[] dp = new int[nums.length];
int ans = Integer.MIN_VALUE;
// Or from 0 Start grabbing Or from 1 Start grabbing
dp[0]=nums[0];
dp[1]=nums[1];
ans = Math.max(dp[0],dp[1]);
//dp[i] The representative must rob the i The maximum amount of account
// But it may not rob i-2 Household robbery i-3 Household
for(int i=2;i<nums.length;i++){
if(i-3>=0) dp[i]=Math.max(nums[i]+dp[i-2],nums[i]+dp[i-3]);
else dp[i]=nums[i]+dp[i-2];
ans = Math.max(ans,dp[i]);
}
return ans;
}
边栏推荐
猜你喜欢

Filters get the data in data; Filters use data in data

51 single chip microcomputer learning notes (2)

为什么中建、中铁都需要这本证书?究竟是什么原因?

sqli-labs Basic Challenges Less1-10

【MySQL系列】-索引知多少

D2. picking carrots (hard version) (one question per day)

安防市场进入万亿时代,安防B2B网上商城平台精准对接深化企业发展路径

Gameframework making games (II) making UI interface

云安全技术发展综述

IP地址分类,判断一个网段是子网超网
随机推荐
Runtimeerror: CUDA out of memory (solved) [easy to understand]
Melodic + Realsense D435i 配置及错误问题解决
Flask SSTI injection learning
PHP website design ideas
Pytorch training code writing skills, dataloader, Einstein logo
Under the epidemic, the biomedical industry may usher in breakthrough development
关于RDBMS和非RDBMS【数据库系统】
SSH服务器拒绝了密码
OverTheWire-Natas
Can the variable name be in Chinese? Directly fooled people
机械制造业数字化新“引擎”供应链协同管理系统助力企业精细化管理迈上新台阶
Resource not found: rgbd_launch 解决方案
MySQL 45 talks about | 06 global locks and table locks: Why are there so many obstacles to adding a field to a table?
Doris learning notes integration with other systems
Go语言创始人从Google离职
Practical guide for network security emergency response technology (Qianxin)
06、类神经网络
微信公众号正式环境上线部署,第三方公众平台接入
Idea regular expression replacement (idea regular search)
Thymeleaf setting disabled