当前位置:网站首页>左右最值最大差问题
左右最值最大差问题
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]);
更多
边栏推荐
- Employment prospects and current situation of Internet of things application technology
- Small hair cat Internet of things platform construction and application model
- 做社交媒体营销应该注意些什么?Shopline卖家的成功秘笈在这里!
- Siemens HMI download prompts lack of panel image solution
- 2022 Health Exhibition, Beijing Health Expo, China Health Exhibition, great health exhibition November 13
- MySQL中的日期时间类型与格式化方式
- [QNX Hypervisor 2.2用户手册]6.3.1 工厂页和控制页
- Data set division
- QT writing the Internet of things management platform 38- multiple database support
- c# . Net MVC uses Baidu ueditor rich text box to upload files (pictures, videos, etc.)
猜你喜欢
What is the application technology of neural network and Internet of things
【ISMB2022教程】图表示学习的精准医疗,哈佛大学Marinka Zitnik主讲,附87页ppt
Chrome开发工具:VMxxx文件是什么鬼
node强缓存和协商缓存实战示例
Neural network IOT platform construction (IOT platform construction practical tutorial)
Installation and use of VMware Tools and open VM tools: solve the problems of incomplete screen and unable to transfer files of virtual machines
New wizard effect used by BCG
Crystal optoelectronics: ar-hud products of Chang'an dark blue sl03 are supplied by the company
多表操作-内连接查询
HMM hidden Markov model and code implementation
随机推荐
如何让你的小游戏适配不同尺寸的手机屏幕
应用实践 | 蜀海供应链基于 Apache Doris 的数据中台建设
In the first month of its launch, the tourist praise rate of this campsite was as high as 99.9%! How did he do it?
CANN算子:利用迭代器高效实现Tensor数据切割分块处理
Taishan Office Technology Lecture: about the order of background (shading and highlighting)
软件客户端数字签名一定要申请代码签名证书吗?
Niuke Xiaobai month race 7 e applese's super ability
Basic use of kotlin
What financial products can you buy with a deposit of 100000 yuan?
Kotlin cycle control
Siemens HMI download prompts lack of panel image solution
Pointnet / pointnet++ point cloud data set processing and training
同事的接口文档我每次看着就头大,毛病多多。。。
Pytoch learning (4)
Actual combat simulation │ JWT login authentication
Lingyun going to sea | Wenhua online & Huawei cloud: creating a new solution for smart teaching in Africa
Write it down once Net analysis of thread burst height of an industrial control data acquisition platform
Crystal optoelectronics: ar-hud products of Chang'an dark blue sl03 are supplied by the company
【毕业季】绿蚁新醅酒,红泥小火炉。晚来天欲雪,能饮一杯无?
记一次 .NET 某工控数据采集平台 线程数 爆高分析