当前位置:网站首页>2016. 增量元素之间的最大差值
2016. 增量元素之间的最大差值
2022-07-02 22:09:00 【Mr Gao】
2016. 增量元素之间的最大差值
给你一个下标从 0 开始的整数数组 nums ,该数组的大小为 n ,请你计算 nums[j] - nums[i] 能求得的 最大差值 ,其中 0 <= i < j < n 且 nums[i] < nums[j] 。
返回 最大差值 。如果不存在满足要求的 i 和 j ,返回 -1 。
示例 1:
输入:nums = [7,1,5,4]
输出:4
解释:
最大差值出现在 i = 1 且 j = 2 时,nums[j] - nums[i] = 5 - 1 = 4 。
注意,尽管 i = 1 且 j = 0 时 ,nums[j] - nums[i] = 7 - 1 = 6 > 4 ,但 i > j 不满足题面要求,所以 6 不是有效的答案。
示例 2:
输入:nums = [9,4,3,2]
输出:-1
解释:
不存在同时满足 i < j 和 nums[i] < nums[j] 这两个条件的 i, j 组合。
示例 3:
输入:nums = [1,5,2,10]
输出:9
解释:
最大差值出现在 i = 0 且 j = 3 时,nums[j] - nums[i] = 10 - 1 = 9 。
这题很简单了,但是建议同学们,做这题仍然不要一味的使用暴力解法,仍然要重可以最快解决问题的方式去解决问题:
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;
}
边栏推荐
猜你喜欢

Niuke network: maximum submatrix
![Additional: [login information storage] and [login status verification]; (including: summarizing all the contents of [login information storage] and [login status verification] so far;)](/img/b7/0f543829b57cf2f2544efec4910c17.png)
Additional: [login information storage] and [login status verification]; (including: summarizing all the contents of [login information storage] and [login status verification] so far;)
![[chestnut sugar GIS] ArcMap - why should the tick of classic capture be removed when using custom capture?](/img/b5/e746dd115995e82c93f667c58a601c.png)
[chestnut sugar GIS] ArcMap - why should the tick of classic capture be removed when using custom capture?

成功改变splunk 默认URL root path

World Environment Day | Chow Tai Fook serves wholeheartedly to promote carbon reduction and environmental protection

位的高阶运算

Xiaopeng P7 had an accident and the airbag did not pop up. Is this normal?

QT qsplitter splitter
![Jielizhi, production line assembly link [chapter]](/img/0d/102596ad13aafd9c6133509d9064dd.png)
Jielizhi, production line assembly link [chapter]

Array advanced improvement
随机推荐
Data analysis learning records -- complete a simple one-way ANOVA with Excel
杰理之样机在多次触摸后会触发关机【篇】
`${}`的用法
首批 | 腾讯云完成国内首个云原生安全成熟度评估
泛型与反射,看这篇就够了
【喜欢的诗词】好了歌
从2022年Q1财报看携程的韧性和远景
[Luogu p1541] tortoise chess [DP]
I admire that someone explained such an obscure subject as advanced mathematics so easily
[chestnut sugar GIS] ArcMap - why should the tick of classic capture be removed when using custom capture?
[leetcode] number of palindromes [9]
图形视图框架
成功改变splunk 默认URL root path
杰理之修改不需要长按开机功能【篇】
杰理之直接触摸样机的顶针反应不正常【篇】
Array advanced improvement
How should programmers write logs
Jerry's built-in shutdown current is 1.2ua, and then it can't be turned on by long pressing [chapter]
'when to use const char * and when to use const char []' - when to use const char * and when to use const char []
[LeetCode] 回文数【9】