当前位置:网站首页>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;
}
边栏推荐
- 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
- Yes, UDP protocol can also be used to request DNS server
- Structure size
- Products on Apple's official website can save 600 yuan by buying iPhone 13 Pro max at a discount
- Can the variable name be in Chinese? Directly fooled people
- English语法_不定代词 - other / another
- The supply chain collaborative management system, a new "engine" of digitalization in machinery manufacturing industry, helps enterprises' refined management to a new level
- Introduction to PHP
- Awk from entry to earth (24) extract the IP of the instruction network card
- GameFramework制作游戏(一)
猜你喜欢

Realsense-Ros安装配置介绍与问题解决

SQL优化的一些建议,希望可以帮到和我一样被SQL折磨的你

GameFramework制作游戏(二)制作UI界面

English grammar_ Indefinite pronoun - other / other

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

Can the variable name be in Chinese? Directly fooled people

基于AMD EPYC服务器的EDA芯片设计解决方案

Idea error failed to determine a suitable driver class

Why do China Construction and China Railway need this certificate? What is the reason?

Niuke multi school E G J L
随机推荐
GameFramework制作游戏(一)
GameFramework制作游戏(二)制作UI界面
C language and SQL Server database technology
SSM framework integration, simple case
优质数对的数目[位运算特点+抽象能力考察+分组快速统计]
sqli-labs Basic Challenges Less1-10
网络安全应急响应技术实战指南(奇安信)
Introduction to PHP
Examples of bio, NiO, AIO
Ten common application scenarios of redis
物理量与单位符号的书写标准
Wechat official account official environment online deployment, third-party public platform access
Sqli labs installation environment: ubuntu18 php7
Bond0 script
Two Sum
Quickly set up dobbo demo
That day, I installed a database for my sister... Just help her sort out another shortcut
Huawei ENSP router static route (the next hop address of the default route)
SQL优化的一些建议,希望可以帮到和我一样被SQL折磨的你
元器件采购系统的主要功能,数字化采购助力元器件企业飞速发展