当前位置:网站首页>2016. maximum difference between incremental elements
2016. maximum difference between incremental elements
2022-07-02 23:03:00 【Mr Gao】
2016. 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 .
This question is very simple , But I suggest students , Don't blindly use violence to solve this problem , We should still focus on the fastest way to solve the problem :
int maximumDifference(int* nums, int numsSize){
int min=nums[0];
int re=0;
int i;
for(i=1;i<numsSize;i++){
if(nums[i]-min>re){
re=nums[i]-min;
}
if(min>nums[i]){
min=nums[i];
}
}
if(re==0){
return -1;
}
return re;
}
边栏推荐
- Qt QScrollArea
- Boot actuator - Prometheus use
- [favorite poems] OK, song
- China Academy of information technology, Tsinghua University, Tencent security, cloud native security, industry university research and use strong alliance!
- [leetcode] reverse string [344]
- 景联文科技低价策略帮助AI企业降低模型训练成本
- ServletContext learning diary 1
- Niuke network: maximum submatrix
- LeetCode 968. Monitor binary tree
- stop slave卡住--事务的事件没有复制完整
猜你喜欢
![Jielizhi, production line assembly link [chapter]](/img/0d/102596ad13aafd9c6133509d9064dd.png)
Jielizhi, production line assembly link [chapter]

Innovation strength is recognized again! Tencent security MSS was the pioneer of cloud native security guard in 2022
![P7072 [csp-j2020] live broadcast Award](/img/bc/fcbc2b1b9595a3bd31d8577aba9b8b.png)
P7072 [csp-j2020] live broadcast Award

P1007 single log bridge

MySQL reset password, forget password, reset root password, reset MySQL password
![[chestnut sugar GIS] ArcMap - how to batch modify the font, color, size, etc. of annotation elements](/img/b1/1fae909fb6a9231096a93d741d6426.png)
[chestnut sugar GIS] ArcMap - how to batch modify the font, color, size, etc. of annotation elements

boot actuator - prometheus使用

Splunk audit setting
![The kth largest element in the [leetcode] array [215]](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
The kth largest element in the [leetcode] array [215]

Comprehensively analyze the logic of the shared purchase business model? How sharing purchase empowers Enterprises
随机推荐
Jerry's prototype will trigger shutdown after multiple touches [chapter]
Array advanced improvement
Tronapi-波场接口-源码无加密-可二开--附接口文档-基于ThinkPHP5封装-作者详细指导-2022年7月1日08:43:06
[hardware] origin of standard resistance value
World Environment Day | Chow Tai Fook serves wholeheartedly to promote carbon reduction and environmental protection
Qt QSplitter拆分器
[chestnut sugar GIS] how does global mapper batch produce ground contour lines through DSM
数据分析学习记录--用EXCEL完成简单的单因素方差分析
E-commerce system microservice architecture
Go language sqlx library operation SQLite3 database addition, deletion, modification and query
【洛谷P1541】乌龟棋【DP】
Gas station [problem analysis - > problem conversion - > greed]
加油站[问题分析->问题转换->贪心]
go 4种单例模式
Go语言sqlx库操作SQLite3数据库增删改查
[NPUCTF2020]ezlogin xPATH注入
Xiaopeng P7 had an accident and the airbag did not pop up. Is this normal?
DTM distributed transaction manager PHP collaboration client V0.1 beta release!!!
MySQL queries nearby data And sort by distance
China Academy of information technology, Tsinghua University, Tencent security, cloud native security, industry university research and use strong alliance!