当前位置:网站首页>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;
}
边栏推荐
- P7072 [csp-j2020] live broadcast Award
- Data analysis learning records -- complete a simple one-way ANOVA with Excel
- DTM distributed transaction manager PHP collaboration client V0.1 beta release!!!
- 创新实力再获认可!腾讯安全MSS获2022年度云原生安全守护先锋
- Jerry's prototype has no touch, and the reinstallation becomes normal after dismantling [chapter]
- [chestnut sugar GIS] ArcMap - why should the tick of classic capture be removed when using custom capture?
- AES高级加密协议的动机阐述
- Qt QScrollArea
- 情感对话识别与生成简述
- WebRTC音视频采集和播放示例及MediaStream媒体流解析
猜你喜欢

AES高級加密協議的動機闡述

MySQL queries nearby data And sort by distance

Odoo13 build a hospital HRP environment (detailed steps)

全面解析分享购商业模式逻辑?分享购是如何赋能企业

Kubernetes uses the host name to allocate the pod on the specified node

Configuration clic droit pour choisir d'ouvrir le fichier avec vs Code

mysql重置密码,忘记密码,重置root密码,重置mysql密码

Generics and reflection, this is enough

首批 | 腾讯云完成国内首个云原生安全成熟度评估

数据分析学习记录(二)---响应曲面法及Design-Expert的简单使用
随机推荐
The motivation of AES Advanced Encryption Protocol
pytorch训练CPU占用持续增长(bug)
Lc173. Binary search tree iterator
地平线2022年4月最新方案介绍
P1007 single log bridge
Value sequence < detailed explanation of daily question >
首批 | 腾讯云完成国内首个云原生安全成熟度评估
Qt QProgressBar详解
Innovation strength is recognized again! Tencent security MSS was the pioneer of cloud native security guard in 2022
【板栗糖GIS】global mapper 如何通过dsm批量制作贴地等高线
Qt QSplitter拆分器
Learning Websites commonly used by circuit designers
设置单击右键可以选择用VS Code打开文件
Uniapp wechat login returns user name and Avatar
P1007 独木桥
Jerry's prototype will trigger shutdown after multiple touches [chapter]
[chestnut sugar GIS] ArcMap - how to batch modify the font, color, size, etc. of annotation elements
世界环境日 | 周大福用心服务推动减碳环保
Methods to solve the tampering of Chrome browser and edeg browser homepage
Rails 3 activerecord: sort by association count - rails 3 activerecord: order by count on Association