当前位置:网站首页>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;
}
边栏推荐
- 容器化技术在嵌入式领域的应用
- antd组件upload上传xlsx文件,并读取文件内容
- WebRTC音视频采集和播放示例及MediaStream媒体流解析
- Innovation strength is recognized again! Tencent security MSS was the pioneer of cloud native security guard in 2022
- Xiaopeng P7 had an accident and the airbag did not pop up. Is this normal?
- 海思3559万能平台搭建:在截获的YUV图像上画框
- stop slave卡住--事务的事件没有复制完整
- [Solved] Splunk: Cannot get username when all users are selected“
- 世界环境日 | 周大福用心服务推动减碳环保
- 設置單擊右鍵可以選擇用VS Code打開文件
猜你喜欢

Hanging mirror security won four global infosec awards on rsac2022

Splunk audit setting
![[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

Webrtc audio and video capture and playback examples and mediastream media stream analysis

STM32之ADC

Set right click to select vs code to open the file

从底层结构开始学习FPGA----Xilinx ROM IP的定制与测试

从2022年Q1财报看携程的韧性和远景

MySQL reset password, forget password, reset root password, reset MySQL password

Local dealers play the community group purchase mode and share millions of operations
随机推荐
Splunk audit 的设定
【板栗糖GIS】arcmap—为什么使用自定义捕捉的时候,经典捕捉的勾要去掉呢?
Splunk audit setting
Share 10 JS closure interview questions (diagrams), come in and see how many you can answer correctly
Comprehensively analyze the logic of the shared purchase business model? How sharing purchase empowers Enterprises
How does Jerry test the wrong touch rate of keys [chapter]
Jerry's fast touch does not respond [chapter]
Qt QSplitter拆分器
成功改变splunk 默认URL root path
Jerry's prototype will trigger shutdown after multiple touches [chapter]
从底层结构开始学习FPGA----Xilinx ROM IP的定制与测试
加油站[问题分析->问题转换->贪心]
設置單擊右鍵可以選擇用VS Code打開文件
Chow-Liu Tree
Boot actuator - Prometheus use
Dahua cloud native load balancing article - the passenger flow of small restaurants has increased
全面解析分享购商业模式逻辑?分享购是如何赋能企业
设置单击右键可以选择用VS Code打开文件
Zhong Xuegao responded that the product will not melt for 1 hour: it contains solid components and cannot melt into water
Go condition variable