当前位置:网站首页>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;
}
边栏推荐
- spark参数调整调优
- The supply chain collaborative management system, a new "engine" of digitalization in machinery manufacturing industry, helps enterprises' refined management to a new level
- English grammar_ Indefinite pronoun - other / other
- GameFramework制作游戏(二)制作UI界面
- Gameframework making games (I)
- Go语言创始人从Google离职
- The input input box of H5 page pops up the numeric keypad, which needs to support decimal points
- Under the epidemic, the biomedical industry may usher in breakthrough development
- 微信公众号正式环境上线部署,第三方公众平台接入
- Throwing OutOfMemoryError “Could not allocate JNI Env“
猜你喜欢

51单片机学习笔记(2)

The security market has entered a trillion era, and the security B2B online mall platform has been accurately connected to deepen the enterprise development path

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

牛客多校 E G J L

Gameframework making games (I)

用GaussDB(for Redis)存画像,推荐业务轻松降本60%

冈萨雷斯 数字图像处理 第一章绪论

sudo rosdep init Error ROS安装问题解决方案

疫情之下,生物医药行业或将迎来突破性发展

PS制作加载GIF图片教程
随机推荐
06、类神经网络
Two Sum
Filters get the data in data; Filters use data in data
为什么中建、中铁都需要这本证书?究竟是什么原因?
MySQL 45 talks about | 06 global locks and table locks: Why are there so many obstacles to adding a field to a table?
Alibaba cloud installs mysql5.7
H5页面input输入框弹起数字键盘,需要支持小数点
云安全技术发展综述
Idea error failed to determine a suitable driver class
基于浏览器的分屏阅读
SQL优化的一些建议,希望可以帮到和我一样被SQL折磨的你
Summary of some problems about left value and right value [easy to understand]
OverTheWire-Bandit
DVWA practice - brute force cracking
机械制造业数字化新“引擎”供应链协同管理系统助力企业精细化管理迈上新台阶
Why do China Construction and China Railway need this certificate? What is the reason?
Doris learning notes integration with other systems
Matplotlib data visualization three minutes entry, half an hour enchanted?
51 single chip microcomputer learning notes (1)
GameFramework制作游戏(一)