当前位置:网站首页>198. Looting - Dynamic Planning
198. Looting - Dynamic Planning
2022-07-03 18:19:00 【The_ Dan】
class Solution {
public:
int rob(vector<int>& nums) {
int ans = 0;
int n = nums.size();
if(n == 1) // A special case : Only 1 Elements
return nums[0];
if(n == 2) // A special case : Only 2 Elements
return max(nums[0], nums[1]);
int dp[n];
dp[0] = nums[0]; //dp[1] It's the first element
dp[1] = max(nums[0], nums[1]); //dp[2] Is the larger of the first two elements
for(int i = 2; i < n; i++){
// The popular formula is , Intrinsic value + Previous And Compare with the previous
dp[i] = max(dp[i - 1], dp[i - 2] + nums[i]);
}
return dp[n - 1];
}
};
Accepted
68/68 cases passed (0 ms)
Your runtime beats 100 % of cpp submissions
Your memory usage beats 73.66 % of cpp submissions (7.4 MB)
边栏推荐
- [enumeration] annoying frogs always step on my rice fields: (who is the most hateful? (POJ hundred practice 2812)
- Naoqi robot summary 27
- PHP MySQL inserts data
- How do microservices aggregate API documents? This wave of operation is too good
- [combinatorics] generating function (summation property)
- [combinatorics] generating function (use generating function to solve the number of solutions of indefinite equation example 2 | extended to integer solution)
- [tutorial] build your first application on coreos
- Computer graduation design PHP makeup sales Beauty shopping mall
- Introduction to PHP MySQL
- 聊聊支付流程的設計與實現邏輯
猜你喜欢

基于人脸识别的课堂考勤系统 tkinter+openpyxl+face_recognition

On Data Mining

Three gradient descent methods and code implementation

Mature port AI ceaspectus leads the world in the application of AI in terminals, CIMC Feitong advanced products go global, smart terminals, intelligent ports, intelligent terminals

Market demand survey and marketing strategy analysis report of global and Chinese pet milk substitutes 2022-2028

Codeforces Round #803 (Div. 2) C. 3SUM Closure

2022-2028 global aircraft head up display (HUD) industry research and trend analysis report

BFS - topology sort

Valentine's day, send you a little red flower~

Global and Chinese health care OEM and ODM market status survey and investment planning recommendations report 2022-2028
随机推荐
企业级自定义表单引擎解决方案(十二)--表单规则引擎2
Postfix tips and troubleshooting commands
The vscode code is automatically modified to a compliance code when it is formatted and saved
SSL / bio pour OpenSSL Get FD
Talk about the design and implementation logic of payment process
Records of long objects and long judgments in the stream of list
PHP MySQL create database
Distributed task distribution framework gearman
Line by line explanation of yolox source code of anchor free series network (6) -- mixup data enhancement
SDNUOJ1015
Theoretical description of linear equations and summary of methods for solving linear equations by eigen
Redis core technology and practice - learning notes (IX): slicing cluster
(9) Opencv Canny edge detection
This diversion
Redis core technology and practice - learning notes (VI) how to achieve data consistency between master and slave Libraries
OpenSSL的SSL/BIO_get_fd
2022-2028 global petroleum pipe joint industry research and trend analysis report
Image 24 bit depth to 8 bit depth
[combinatorics] generating function (use generating function to solve the number of solutions of indefinite equation)
[Tongxin UOS] scanner device management driver installation