当前位置:网站首页>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
边栏推荐
- Lingyun going to sea | Wenhua online & Huawei cloud: creating a new solution for smart teaching in Africa
- Form组件常用校验规则-1(持续更新中~)
- 九齐单片机NY8B062D单按键控制4种LED状态
- Delete the characters with the least number of occurrences in the string [JS, map sorting, regular]
- So this is the BGP agreement
- c# .net mvc 使用百度Ueditor富文本框上传文件(图片,视频等)
- Decryption function calculates "task state and lifecycle management" of asynchronous task capability
- 15million employees are easy to manage, and the cloud native database gaussdb makes HR office more efficient
- What does the neural network Internet of things mean? Popular explanation
- 哈希(Hash)竞猜游戏系统开发功能分析及源码
猜你喜欢
Dark horse programmer - software testing - 09 stage 2-linux and database -31-43 instructions issued by modifying the file permission letter, - find the link to modify the file, find the file command,
[ismb2022 tutorial] the picture shows the precision medicine of learning. Marinka zitnik, Harvard University, keynote speaker, with 87 ppt
So this is the BGP agreement
C # better operation mongodb database
C language - Introduction - Foundation - grammar - process control (VII)
Swagger suddenly went crazy
[in-depth learning] review pytoch's 19 loss functions
NLP, vision, chip What is the development direction of AI? Release of the outlook report of Qingyuan Association [download attached]
What are the consequences of closing the read / write channel?
The company needs to be monitored. How do ZABBIX and Prometheus choose? That's the right choice!
随机推荐
更强的 JsonPath 兼容性及性能测试之2022版(Snack3,Fastjson2,jayway.jsonpath)
repeat_ P1002 [NOIP2002 popularization group] cross the river pawn_ dp
Kotlin classes and objects
1500万员工轻松管理,云原生数据库GaussDB让HR办公更高效
Thinking on demand development
What is involution?
Actual combat simulation │ JWT login authentication
上线首月,这家露营地游客好评率高达99.9%!他是怎么做到的?
Lingyun going to sea | Wenhua online & Huawei cloud: creating a new solution for smart teaching in Africa
What is the application technology of neural network and Internet of things
C # use stopwatch to measure the running time of the program
YOLOv5s-ShuffleNetV2
SSRS筛选器的IN运算(即包含于)用法
Kotlin cycle control
C language - Introduction - Foundation - grammar - process control (VII)
What should we pay attention to when doing social media marketing? Here is the success secret of shopline sellers!
2022 Health Exhibition, health exhibition, Beijing Great Health Exhibition and health industry exhibition were held in November
Creation of JVM family objects
凌云出海记 | 文华在线&华为云:打造非洲智慧教学新方案
栈:如何实现有效括号的判断?