当前位置:网站首页>Brainstorm: Raiders
Brainstorm: Raiders
2022-08-06 23:18:00 【InfoQ】
Topic
Example 1:
Example 2:
Problem-solving ideas
Code Implementation
class Solution {
public int rob(int[] nums) {
if (nums == null || nums.length ==0) {
return 0;
}
int length = nums.length;
if(length == 1){
return nums[0];
}
int[] dp = new int[length];
dp[0] = nums[0];
dp[1] = Math.max(nums[0], nums[1]);
for (int i = 2; i < length; i++) {
dp[i] = Math.max(dp[i-2] + nums[i], dp[i-1]);
}
return dp[length - 1];
}
}
Finally
- Time complexity: O(n), where n is the length of the array.You only need to iterate over the array once.
- Space complexity: O(1).
边栏推荐
- What about online stock account opening?Is it safe to open an account?
- LC买卖股票的最佳时机Ⅱ(三种解法详解)
- D. Number into Sequence【1300】
- 【超好懂的比赛题解】National Taiwan University NCPC Preliminary 2021 比赛题解
- Nacos配置中心之动态感知
- 哪家证券公司开户佣金低信息精准?手机开户股票开户安全吗?
- 基于TCP的网络聊天系统
- 抓取商品上传提示“图片宽度不能大于5000,长度不能大于5000,请修改!”,怎么解决?
- 企业即时通讯软件有哪些功能?对企业有什么帮助?
- golang使用josn.Unmarshal报错:unexpected end of JSON input
猜你喜欢

MySQL 免安装版安装配置

AVL 树应用

Cesium快速上手1-CesiumWidget-Scene结构

编译调试插件功能总结

基于FTP协议文件的上传与下载

#私藏项目实操分享#精锐5加密锁如何删除锁内许可

【机器学习】数据科学基础——机器学习基础实践(一)

头像上传功能

Three ways to introduce external fonts in Mini Programs and problems
![[C language] Write a simple multi-choice game with the most basic C language knowledge (the source code is at the end of the article)](/img/81/afac9d2ca36be64dbe524cb59d2f27.png)
[C language] Write a simple multi-choice game with the most basic C language knowledge (the source code is at the end of the article)
随机推荐
bugku 0和1的故事
Redis持久化
猿人学-第二题
猿人学-第一题
复制天猫的宝贝上传到淘宝,SKU自定义属性值没有复制过来是什么原因?
同花顺是软件吗?请问网上开户安全么?
bugku simple forensics 1
cxf反向根据.net wsdl内容生成服务器端代码
2.基于ITIL的IT服务管理基础篇 --- IT服务管理的背景
双十一接口流量突增,如何做好性能调优?
micronet ICCV2021
Day120.尚医通:项目总结
抓取商品上传提示“图片宽度不能大于5000,长度不能大于5000,请修改!”,怎么解决?
This article takes you to understand the basic principles of webrtc (hands-on 1v1 video call)
MySQL 8.0.28 忘记密码,重置密码
PAT乙级-B1024 科学计数法(20)
小程序中引入外部字体的三种方式以及出现的问题
【超好懂的比赛题解】HNCPC Multi-university Training Round3 比赛题解
PAT乙级-B1026 程序运行时间(15)
PAT serie b - B1025 inversion list (25)