当前位置:网站首页>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
边栏推荐
- Optimization cases of complex factor calculation: deep imbalance, buying and selling pressure index, volatility calculation
- Small hair cat Internet of things platform construction and application model
- Neural network IOT platform construction (IOT platform construction practical tutorial)
- AP8022开关电源小家电ACDC芯片离线式开关电源IC
- Introduction to ACM combination counting
- Huawei Nova 10 series supports the application security detection function to build a strong mobile security firewall
- NetCore3.1 Json web token 中间件
- 同事的接口文档我每次看着就头大,毛病多多。。。
- Kotlin inheritance
- Write it down once Net analysis of thread burst height of an industrial control data acquisition platform
猜你喜欢
如何让你的小游戏适配不同尺寸的手机屏幕
Related concepts of federal learning and motivation (1)
c# . Net MVC uses Baidu ueditor rich text box to upload files (pictures, videos, etc.)
[problem] Druid reports exception SQL injection violation, part always true condition not allow solution
FS4061A升压8.4V充电IC芯片和FS4061B升压12.6V充电IC芯片规格书datasheet
YOLOv5s-ShuffleNetV2
记一次 .NET 某工控数据采集平台 线程数 爆高分析
针对深度学习的“失忆症”,科学家提出基于相似性加权交错学习,登上PNAS
水晶光电:长安深蓝SL03的AR-HUD产品由公司供应
C # better operation mongodb database
随机推荐
Dark horse programmer - software testing - stage 07 2-linux and database -09-24-linux command learning steps, wildcards, absolute paths, relative paths, common commands for files and directories, file
Multi table operation - external connection query
NLP、视觉、芯片...AI重点方向发展几何?青源会展望报告发布[附下载]
九齐NY8B062D MCU规格书/datasheet
Ziguang zhanrui completed the first 5g R17 IOT NTN satellite on the Internet of things in the world
实战模拟│JWT 登录认证
Is it necessary to apply for code signing certificate for software client digital signature?
记一次 .NET 某工控数据采集平台 线程数 爆高分析
2022 version of stronger jsonpath compatibility and performance test (snack3, fastjson2, jayway.jsonpath)
In operation (i.e. included in) usage of SSRs filter
解密函数计算异步任务能力之「任务的状态及生命周期管理」
Small hair cat Internet of things platform construction and application model
ICML 2022 | meta proposes a robust multi-objective Bayesian optimization method to effectively deal with input noise
NetCore3.1 Json web token 中间件
应用实践 | 蜀海供应链基于 Apache Doris 的数据中台建设
Prometheus installation
Chrome开发工具:VMxxx文件是什么鬼
2022 Health Exhibition, health exhibition, Beijing Great Health Exhibition and health industry exhibition were held in November
Kotlin basic data type
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?