当前位置:网站首页>左右最值最大差问题
左右最值最大差问题
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]);
更多
边栏推荐
- 华为nova 10系列支持应用安全检测功能 筑牢手机安全防火墙
- Is it safe for Great Wall Securities to open an account? Stock account opening process online account opening
- Qt编写物联网管理平台38-多种数据库支持
- Huawei cloud store homepage banner resource bit application
- @Data source connection pool exhaustion caused by transactional abuse
- 漫谈客户端存储技术之Cookie篇
- #夏日挑战赛#带你玩转HarmonyOS多端钢琴演奏
- Employment prospects and current situation of Internet of things application technology
- Process of manually encrypt the mass-producing firmware and programming ESP devices
- Offset function and windowing function
猜你喜欢

Detailed explanation of Audi EDI invoice message

Multi table operation - external connection query

Swagger suddenly went crazy

Wireshark network packet capture

【历史上的今天】7 月 4 日:第一本电子书问世;磁条卡的发明者出生;掌上电脑先驱诞生
![[ismb2022 tutorial] the picture shows the precision medicine of learning. Marinka zitnik, Harvard University, keynote speaker, with 87 ppt](/img/0d/aa7f82fada743ea2ec23355ef954df.jpg)
[ismb2022 tutorial] the picture shows the precision medicine of learning. Marinka zitnik, Harvard University, keynote speaker, with 87 ppt

如何让你的小游戏适配不同尺寸的手机屏幕
MySQL中的日期时间类型与格式化方式

C # better operation mongodb database

水晶光电:长安深蓝SL03的AR-HUD产品由公司供应
随机推荐
QT writing the Internet of things management platform 38- multiple database support
Multi table operation - external connection query
Related concepts of federal learning and motivation (1)
Kotlin basic data type
输入的查询SQL语句,是如何执行的?
华为nova 10系列支持应用安全检测功能 筑牢手机安全防火墙
Taishan Office Technology Lecture: about the order of background (shading and highlighting)
Template_ Judging prime_ Square root / six prime method
凌云出海记 | 沐融科技&华为云:打造非洲金融SaaS解决方案样板
Lingyun going to sea | Wenhua online & Huawei cloud: creating a new solution for smart teaching in Africa
Niuke Xiaobai month race 7 who is the divine Archer
1005 spell it right (20 points) (pat a)
C # better operation mongodb database
2022 version of stronger jsonpath compatibility and performance test (snack3, fastjson2, jayway.jsonpath)
Chrome development tool: what the hell is vmxxx file
1007 maximum subsequence sum (25 points) (PAT class a)
What are the consequences of closing the read / write channel?
[today in history] July 4: the first e-book came out; The inventor of magnetic stripe card was born; Palm computer pioneer was born
What should we pay attention to when doing social media marketing? Here is the success secret of shopline sellers!
应用实践 | 蜀海供应链基于 Apache Doris 的数据中台建设