当前位置:网站首页>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;
}
边栏推荐
- Storage unit conversion
- 存储单位换算
- Methods of adding styles to native JS
- Jerry's built-in shutdown current is 1.2ua, and then it can't be turned on by long pressing [chapter]
- 钟薛高回应产品1小时不化:含固体成分 融化不能变成水
- 创新实力再获认可!腾讯安全MSS获2022年度云原生安全守护先锋
- Jatpack------LiveData
- 悬镜安全在RSAC2022上斩获Global InfoSec Awards四项大奖
- Array advanced improvement
- 泛型与反射,看这篇就够了
猜你喜欢
Go语言sqlx库操作SQLite3数据库增删改查
数组进阶提高
wait解决僵尸进程
分享 10 个 JS 闭包面试题(图解),进来看看你能答对多少
Addition, deletion, modification and query of handwritten ORM (object relationship mapping)
小鹏P7出事故,安全气囊未弹出,这正常吗?
P7072 [csp-j2020] live broadcast Award
boot actuator - prometheus使用
Gas station [problem analysis - > problem conversion - > greed]
Oracle cursor
随机推荐
杰理之如何测试按键的误触率【篇】
AES高级加密协议的动机阐述
easyclick,ec权朗网络验证源码
Addition, deletion, modification and query of handwritten ORM (object relationship mapping)
Golang的学习路线
最小生成树 Minimum Spanning Tree
Objects and object variables
[LeetCode] 数组中的第K个最大元素【215】
Additional: [login information storage] and [login status verification]; (including: summarizing all the contents of [login information storage] and [login status verification] so far;)
Oracle PL / SQL programming
[羊城杯2020]easyphp
图形视图框架
Jerry's modification does not require long press the boot function [chapter]
杰理之样机在多次触摸后会触发关机【篇】
E-commerce system microservice architecture
`${}`的用法
电商系统微服务架构
Go condition variable
[chestnut sugar GIS] how does global mapper batch produce ground contour lines through DSM
Comprehensively analyze the logic of the shared purchase business model? How sharing purchase empowers Enterprises