当前位置:网站首页>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)
边栏推荐
- Computer graduation design PHP sports goods online sales system website
- SDNUOJ1015
- [combinatorics] generating function (use generating function to solve the combination number of multiple sets R)
- Niuke monthly race 31 minus integer
- Market demand survey and marketing strategy analysis report of global and Chinese pet milk substitutes 2022-2028
- [combinatorics] generating function (positive integer splitting | basic model of positive integer splitting | disordered splitting with restrictions)
- [enumeration] annoying frogs always step on my rice fields: (who is the most hateful? (POJ hundred practice 2812)
- Golang string (string) and byte array ([]byte) are converted to each other
- Lesson 13 of the Blue Bridge Cup -- tree array and line segment tree [exercise]
- Keepalived setting does not preempt resources
猜你喜欢
Codeforces Round #803 (Div. 2) C. 3SUM Closure
As soon as we enter "remote", we will never regret, and several people will be happy and several people will be sad| Community essay solicitation
Computer graduation design PHP makeup sales Beauty shopping mall
Line by line explanation of yolox source code of anchor free series network (5) -- mosaic data enhancement and mathematical understanding
(8) HS corner detection
AcWing 271. 杨老师的照相排列【多维DP】
The number of incremental paths in the grid graph [dfs reverse path + memory dfs]
Grammaire anglaise Nom - Classification
2022-2028 global plasmid DNA cdmo industry research and trend analysis report
Redis core technology and practice - learning notes (IX): slicing cluster
随机推荐
Win 11 major updates, new features love love.
[combinatorics] generating function (positive integer splitting | unordered | ordered | allowed repetition | not allowed repetition | unordered not repeated splitting | unordered repeated splitting)
The vscode code is automatically modified to a compliance code when it is formatted and saved
PHP MySQL create database
PHP MySQL inserts multiple pieces of data
Theoretical description of linear equations and summary of methods for solving linear equations by eigen
2022-2028 global scar care product industry research and trend analysis report
[combinatorics] exponential generating function (concept of exponential generating function | permutation number exponential generating function = combinatorial number ordinary generating function | e
Gear2021 monthly update - December
Classroom attendance system based on face recognition tkinter+openpyxl+face_ recognition
Global and Chinese health care OEM and ODM market status survey and investment planning recommendations report 2022-2028
Sensor debugging process
2022-2028 global lithium battery copper foil industry research and trend analysis report
Redis cache avalanche, penetration, breakdown
Kotlin的协程:上下文
Research Report on competitive strategy Outlook Analysis and investment strategic planning of China's smart home equipment industry, 2022-2028
PHP MySQL preprocessing statement
Distributed task distribution framework gearman
The gbase 8A database does not support the DB2 function value (column_name, 0) cluster syntax
圖像24比特深度轉8比特深度