当前位置:网站首页>左右最值最大差问题
左右最值最大差问题
2022-07-04 18:48:00 【GreyZeng】
左右最值最大差问题
作者:Grey
原文地址: 左右最值最大差问题
题目链接
描述
给定一个长度为N(N>1)的整型数组A,可以将A划分成左右两个部分,左部分
A[0..K]
,右部分A[K+1..N-1]
,K可以取值的范围是[0,N-2]
。求这么多划分方案中,左部分中的最大值减去右部分最大值的绝对值,最大是多少?
给定整数数组A和数组的大小n,请返回题目所求的答案。
测试样例:
A:[2,7,3,1,1]
n:5
返回:6
主要思路
假设数组长度为len
,遍历一遍数组,得到数组的最大值max
,然后比较0
位置和len-1
位置的值,取较小的那个,假设为m
,则max - m
即为答案。
完整代码
public class MaxGap {
public int findMaxGap(int[] A, int n) {
int max = A[0];
int len = A.length;
for (int i = 1; i < len; i++) {
max = Math.max(A[i], max);
}
return max - (Math.min(A[0], A[len - 1]));
}
}
证明
由于全局最大值是max
,所以无论max
被划分到哪个部分,都会成为这部分的最大值。假设max
被划分到了右边部分,所以右边部分的最大值就是max
,假设左边部分的最大值是m
,那么max - m
即为一个答案候选。要使得max - m
最大,而左边部分不能为空,所以左边部分必须要包含0
位置的值,所以,在左边只包含0
位置值的时候,max - m
才能做到最大,即:max - arr[0]
;同理,假设max
被划分到了左边,右边最大值假设为n
,要使得max - n
最大,len - 1
位置的值又必须包含在右边,那么max - arr[len-1]
才是最大的。所以最终的答案就是:
max - Math.min(arr[0],arr[len-1]);
更多
边栏推荐
- Offset function and windowing function
- [today in history] July 4: the first e-book came out; The inventor of magnetic stripe card was born; Palm computer pioneer was born
- Actual combat simulation │ JWT login authentication
- 凌云出海记 | 沐融科技&华为云:打造非洲金融SaaS解决方案样板
- Prometheus installation
- 华为云云商店首页 Banner 资源位申请
- Employment prospects and current situation of Internet of things application technology
- Cann operator: using iterators to efficiently realize tensor data cutting and blocking processing
- 太方便了,钉钉上就可完成代码发布审批啦!
- 一文搞懂Go语言中文件的读写与创建
猜你喜欢
Aiming at the "amnesia" of deep learning, scientists proposed that based on similarity weighted interleaved learning, they can board PNAS
node强缓存和协商缓存实战示例
Chrome开发工具:VMxxx文件是什么鬼
什么叫内卷?
Related concepts of federal learning and motivation (1)
Key rendering paths for performance optimization
The company needs to be monitored. How do ZABBIX and Prometheus choose? That's the right choice!
What should we pay attention to when doing social media marketing? Here is the success secret of shopline sellers!
QT writing the Internet of things management platform 38- multiple database support
Write it down once Net analysis of thread burst height of an industrial control data acquisition platform
随机推荐
Huawei cloud store homepage banner resource bit application
Cbcgpprogressdlg progress bar used by BCG
漫谈客户端存储技术之Cookie篇
Selected review | machine learning technology for Cataract Classification / classification
2022 Health Exhibition, Beijing Health Expo, China Health Exhibition, great health exhibition November 13
SSRS筛选器的IN运算(即包含于)用法
Basic use of kotlin
Dynamic memory management
kotlin 继承
NLP、视觉、芯片...AI重点方向发展几何?青源会展望报告发布[附下载]
What is involution?
[QNX hypervisor 2.2 user manual]6.3.1 factory page and control page
Utilisation de la barre de progression cbcggprogressdlgctrl utilisée par BCG
Lingyun going to sea | 10 jump &huawei cloud: jointly help Africa's inclusive financial services
MySQL中的日期时间类型与格式化方式
ICML 2022 | meta proposes a robust multi-objective Bayesian optimization method to effectively deal with input noise
泰山OFFICE技术讲座:关于背景(底纹和高亮)的顺序问题
实战模拟│JWT 登录认证
New wizard effect used by BCG
Multi table operation inner join query