当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
mysql重置密码,忘记密码,重置root密码,重置mysql密码
从底层结构开始学习FPGA----Xilinx ROM IP的定制与测试
世界环境日 | 周大福用心服务推动减碳环保
Local dealers play the community group purchase mode and share millions of operations
Looking at Ctrip's toughness and vision from the Q1 financial report in 2022
odoo13搭建医院HRP环境(详细步骤)
Hanging mirror security won four global infosec awards on rsac2022
xshell配置xforward转发火狐浏览器
Methods to solve the tampering of Chrome browser and edeg browser homepage
【硬件】标准阻值的由来
随机推荐
Uniapp wechat login returns user name and Avatar
设置单击右键可以选择用VS Code打开文件
P1007 独木桥
Splunk audit 的设定
数据标注典型案例,景联文科技如何助力企业搭建数据方案
[leetcode] reverse string [344]
Qt QScrollArea
分布式监控系统zabbix
Gas station [problem analysis - > problem conversion - > greed]
[Solved] Splunk: Cannot get username when all users are selected“
Xiaopeng P7 had an accident and the airbag did not pop up. Is this normal?
Mask R-CNN
Share 10 JS closure interview questions (diagrams), come in and see how many you can answer correctly
Easyclick, EC Quanlang network verification source code
【喜欢的诗词】好了歌
Splunk audit setting
Golang面试整理 三 简历如何书写
Local dealers play the community group purchase mode and share millions of operations
[leetcode] most elements [169]
easyclick,ec权朗网络验证源码