当前位置:网站首页>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;
}
边栏推荐
- Jerry's built-in shutdown current is 1.2ua, and then it can't be turned on by long pressing [chapter]
- Golang interview finishing three resumes how to write
- [favorite poems] OK, song
- [leetcode] reverse the word III in the string [557]
- Jerry's built-in short press and long press, no matter how long it is, it is a short press [chapter]
- 情感对话识别与生成简述
- 从底层结构开始学习FPGA----Xilinx ROM IP的定制与测试
- 悬镜安全在RSAC2022上斩获Global InfoSec Awards四项大奖
- 成功改变splunk 默认URL root path
- 编辑卡顿
猜你喜欢
随机推荐
Hanging mirror security won four global infosec awards on rsac2022
[Yangcheng cup 2020] easyphp
[leetcode] reverse string [344]
Chow-Liu Tree
地平线2022年4月最新方案介绍
从2022年Q1财报看携程的韧性和远景
Innovation strength is recognized again! Tencent security MSS was the pioneer of cloud native security guard in 2022
容器化技术在嵌入式领域的应用
Freshman learning sharing
Tronapi-波场接口-源码无加密-可二开--附接口文档-基于ThinkPHP5封装-作者详细指导-2022年7月1日08:43:06
[羊城杯2020]easyphp
剑指 Offer II 099. 最小路径之和-双百代码
Jielizhi, production line assembly link [chapter]
编辑卡顿
Construction of Hisilicon 3559 universal platform: rotation operation on the captured YUV image
Jerry's built-in shutdown current is 1.2ua, and then it can't be turned on by long pressing [chapter]
Start from the bottom structure to learn the customization and testing of FPGA --- Xilinx ROM IP
海思3559万能平台搭建:在截获的YUV图像上旋转操作
Qt QScrollArea
go 4種單例模式






![[npuctf2020]ezlogin XPath injection](/img/6e/dac4dfa0970829775084bada740542.png)

![P7072 [csp-j2020] live broadcast Award](/img/bc/fcbc2b1b9595a3bd31d8577aba9b8b.png)
![加油站[问题分析->问题转换->贪心]](/img/15/5313f900abedb46ce82d8ab81af1d7.png)