当前位置:网站首页>2016. maximum difference between incremental elements
2016. maximum difference between incremental elements
2022-06-13 03:25:00 【Python's path to becoming a God】
- The maximum difference between incremental elements
I'll give you a subscript from 0 The starting array of integers nums , The size of the array is n , Please calculate nums[j] - nums[i] Obtainable Maximum difference , among 0 <= i < j < n And nums[i] < nums[j] .
return Maximum difference . If there is no... That meets the requirements i and j , return -1 .
Example 1:
Input :nums = [7,1,5,4]
Output :4
explain :
The maximum difference appears in i = 1 And j = 2 when ,nums[j] - nums[i] = 5 - 1 = 4 .
Be careful , Even though i = 1 And j = 0 when ,nums[j] - nums[i] = 7 - 1 = 6 > 4 , but i > j Does not meet the requirements of the problem surface , therefore 6 Not a valid answer .
Example 2:
Input :nums = [9,4,3,2]
Output :-1
explain :
There is no simultaneous satisfaction i < j and nums[i] < nums[j] Of these two conditions i, j Combine .
Example 3:
Input :nums = [1,5,2,10]
Output :9
explain :
The maximum difference appears in i = 0 And j = 3 when ,nums[j] - nums[i] = 10 - 1 = 9 .
class Solution {
public:
int maximumDifference(vector<int>& nums) {
int len = nums.size();
vector<int> dp(len, 0);
int cost = nums[0];
for(int i = 1; i < len; ++i){
cost = min(cost, nums[i]);
dp[i] = max(dp[i - 1], nums[i] - cost);
}
return dp[len - 1] > 0 ? dp[len - 1] : -1;
}
};
边栏推荐
- Introduction to redis (using redis, common commands, persistence methods, and cluster operations)
- MySQL transactions and locks (V)
- PHP import classes in namespace
- JVM JMM (VI)
- DDL operation table
- PostgreSQL common SQL
- Azure SQL db/dw series (14) -- using query store (3) -- common scenarios
- Understanding of intermediatelayergetter
- JVM GC (V)
- Least recently used cache (source force deduction)
猜你喜欢
English语法_方式副词-位置
Open source - campus forum and resource sharing applet
[JVM Series 2] runtime data area
Azure SQL db/dw series (13) -- using query store (2) -- report Introduction (2)
[JVM series 8] overview of JVM knowledge points
JVM JMM (VI)
MySQL index bottom layer (I)
Querywrapper constructor method
Time processing class in PHP
MySQL learning summary Xi: detailed explanation of the use of stored procedures and stored functions
随机推荐
Data from the first to seventh census (to counties)
MySQL index optimization (4)
Simple use of qtreeview of QT (including source code + comments)
技术博客,经验分享宝典
MySQL learning summary Xi: detailed explanation of the use of stored procedures and stored functions
Spark Foundation
C simple understanding - generics
Azure SQL db/dw series (11) -- re understanding the query store (4) -- Query store maintenance
How to write product requirements documents
[azure data platform] ETL tool (2) -- azure data factory "copy data" tool (cloud copy)
C # simple understanding - static members and static classes
C simple understanding - arrays and sets
The use of curl in PHP
English grammar_ Frequency adverb
Brief introduction: distributed cap theory and base theory
C # simple understanding - method overloading and rewriting
Understanding the ongdb open source map data foundation from the development of MariaDB
C method parameter: in
Querywrapper constructor method
Dish recommendation system based on graph database