当前位置:网站首页>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;
}
边栏推荐
- [leetcode] there are duplicate elements [217]
- Qt QScrollArea
- Distributed monitoring system ZABBIX
- 从底层结构开始学习FPGA----Xilinx ROM IP的定制与测试
- Uniapp wechat login returns user name and Avatar
- How does Jerry test the wrong touch rate of keys [chapter]
- Golang interview finishing three resumes how to write
- P7072 [CSP-J2020] 直播获奖
- JS syntax ES6, ES7, es8, es9, ES10, es11, ES12 new features (Abstract)
- Jerry's charge unplugged, unable to touch the boot [chapter]
猜你喜欢

Start from the bottom structure to learn the customization and testing of FPGA --- Xilinx ROM IP

Splunk audit setting

AES高級加密協議的動機闡述
![The kth largest element in the [leetcode] array [215]](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
The kth largest element in the [leetcode] array [215]

悬镜安全在RSAC2022上斩获Global InfoSec Awards四项大奖
![[hardware] origin of standard resistance value](/img/d1/b9184c508d363eb0fa018ea6a57a10.png)
[hardware] origin of standard resistance value

Go语言sqlx库操作SQLite3数据库增删改查

QT qpprogressbar details

Boot actuator - Prometheus use

Xiaopeng P7 had an accident and the airbag did not pop up. Is this normal?
随机推荐
Minimum spanning tree
Local dealers play the community group purchase mode and share millions of operations
E-commerce system microservice architecture
AES高級加密協議的動機闡述
分享 10 个 JS 闭包面试题(图解),进来看看你能答对多少
設置單擊右鍵可以選擇用VS Code打開文件
Jerry's built-in short press and long press, no matter how long it is, it is a short press [chapter]
Start from the bottom structure to learn the customization and testing of FPGA --- Xilinx ROM IP
Lambda表达式:一篇文章带你通透
Odoo13 build a hospital HRP environment (detailed steps)
Splunk audit setting
【洛谷P1541】乌龟棋【DP】
Boot actuator - Prometheus use
ServletContext learning diary 1
1px pixel compatibility of mobile terminal, 1px border
World Environment Day | Chow Tai Fook serves wholeheartedly to promote carbon reduction and environmental protection
2016. 增量元素之间的最大差值
go 4種單例模式
[chestnut sugar GIS] ArcMap - how to batch modify the font, color, size, etc. of annotation elements
How does Jerry test the wrong touch rate of keys [chapter]