当前位置:网站首页>The problem of the maximum difference between the left and right maxima
The problem of the maximum difference between the left and right maxima
2022-07-04 20:32:00 【GreyZeng】
The problem of the maximum difference between the left and right maxima
author :Grey
Original address : The problem of the maximum difference between the left and right maxima
Topic link
Cattle guest : Maximum difference between left and right maxima
describe
Given a length of N(N>1) Integer array A, Can be A Divided into left and right parts , Left part
A[0..K]
, Right sectionA[K+1..N-1]
,K The range of values can be[0,N-2]
. Find so many partition schemes , The absolute value of the maximum in the left part minus the maximum in the right part , What's the biggest ?
Given an array of integers A And array size n, Please return the answer to the question .
The test sample :
A:[2,7,3,1,1]
n:5
return :6
Main idea
Suppose the length of the array is len
, Go through the array , Get the maximum value of the array max
, Then compare 0
Location and len-1
The value of the location , Take the smaller one , Assuming that m
, be max - m
Is the answer .
Complete code
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]));
}
}
prove
Because the global maximum is max
, So no matter max
To which part , Will become the maximum value of this part . hypothesis max
It is divided into the right part , So the maximum value of the right part is max
, Suppose the maximum value of the left part is m
, that max - m
It is a candidate for an answer . To make max - m
Maximum , The left part cannot be empty , So the left part must contain 0
The value of the location , therefore , On the left only 0
Position value ,max - m
To maximize , namely :max - arr[0]
; Empathy , hypothesis max
Is divided to the left , The maximum value on the right is assumed to be n
, To make max - n
Maximum ,len - 1
The value of the position must be included on the right , that max - arr[len-1]
Is the biggest . So the final answer is :
max - Math.min(arr[0],arr[len-1]);
more
边栏推荐
- 六石编程学:关于代码,有六个得意
- What is involution?
- 华为云云商店首页 Banner 资源位申请
- Application practice | Shuhai supply chain construction of data center based on Apache Doris
- FS4061A升压8.4V充电IC芯片和FS4061B升压12.6V充电IC芯片规格书datasheet
- How is the entered query SQL statement executed?
- Informatics Olympiad 1336: [example 3-1] find roots and children
- 如何让你的小游戏适配不同尺寸的手机屏幕
- 华为nova 10系列支持应用安全检测功能 筑牢手机安全防火墙
- c# .net mvc 使用百度Ueditor富文本框上传文件(图片,视频等)
猜你喜欢
实战模拟│JWT 登录认证
关于联邦学习和激励的相关概念(1)
What should we pay attention to when doing social media marketing? Here is the success secret of shopline sellers!
C language - Introduction - Foundation - grammar - process control (VII)
The company needs to be monitored. How do ZABBIX and Prometheus choose? That's the right choice!
Introduction to ACM combination counting
Wireshark network packet capture
AP8022开关电源小家电ACDC芯片离线式开关电源IC
Small hair cat Internet of things platform construction and application model
Dynamic memory management
随机推荐
C语言-入门-基础-语法-流程控制(七)
Detailed explanation of Audi EDI invoice message
SSRS筛选器的IN运算(即包含于)用法
【深度学习】一文看尽Pytorch之十九种损失函数
Delete the characters with the least number of occurrences in the string [JS, map sorting, regular]
MySQL中的日期时间类型与格式化方式
C server log module
九齐单片机NY8B062D单按键控制4种LED状态
C # use stopwatch to measure the running time of the program
软件客户端数字签名一定要申请代码签名证书吗?
原来这才是 BGP 协议
2022 Health Exhibition, Beijing Health Expo, China Health Exhibition, great health exhibition November 13
Why is the maximum speed the speed of light
凌云出海记 | 文华在线&华为云:打造非洲智慧教学新方案
Lingyun going to sea | Murong Technology & Huawei cloud: creating a model of financial SaaS solutions in Africa
六石编程学:关于代码,有六个得意
Regular replacement [JS, regular expression]
C # better operation mongodb database
Kotlin condition control
实践示例理解js强缓存协商缓存