当前位置:网站首页>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
边栏推荐
- 2022 version of stronger jsonpath compatibility and performance test (snack3, fastjson2, jayway.jsonpath)
- Thinking on demand development
- 2022 Health Exhibition, health exhibition, Beijing Great Health Exhibition and health industry exhibition were held in November
- 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
- NLP, vision, chip What is the development direction of AI? Release of the outlook report of Qingyuan Association [download attached]
- Small hair cat Internet of things platform construction and application model
- Data set division
- C # use stopwatch to measure the running time of the program
- Regular replacement [JS, regular expression]
- 更强的 JsonPath 兼容性及性能测试之2022版(Snack3,Fastjson2,jayway.jsonpath)
猜你喜欢

如何让你的小游戏适配不同尺寸的手机屏幕

C # use stopwatch to measure the running time of the program

So this is the BGP agreement

What is the application technology of neural network and Internet of things

New wizard effect used by BCG

多表操作-内连接查询

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?
![Regular replacement [JS, regular expression]](/img/66/abfe0bd6050b7266ad269d655be644.png)
Regular replacement [JS, regular expression]

The company needs to be monitored. How do ZABBIX and Prometheus choose? That's the right choice!

C language - Introduction - Foundation - grammar - process control (VII)
随机推荐
华为nova 10系列支持应用安全检测功能 筑牢手机安全防火墙
复杂因子计算优化案例:深度不平衡、买卖压力指标、波动率计算
做社交媒体营销应该注意些什么?Shopline卖家的成功秘笈在这里!
更强的 JsonPath 兼容性及性能测试之2022版(Snack3,Fastjson2,jayway.jsonpath)
#夏日挑战赛#带你玩转HarmonyOS多端钢琴演奏
Process of manually encrypt the mass-producing firmware and programming ESP devices
漫谈客户端存储技术之Cookie篇
PHP pseudo original API docking method
1009 product of polynomials (25 points) (PAT class a)
What does the neural network Internet of things mean? Popular explanation
Related concepts of federal learning and motivation (1)
[in-depth learning] review pytoch's 19 loss functions
Every time I look at the interface documents of my colleagues, I get confused and have a lot of problems...
FS4061A升压8.4V充电IC芯片和FS4061B升压12.6V充电IC芯片规格书datasheet
如何让你的小游戏适配不同尺寸的手机屏幕
Aiming at the "amnesia" of deep learning, scientists proposed that based on similarity weighted interleaved learning, they can board PNAS
Actual combat simulation │ JWT login authentication
How is the entered query SQL statement executed?
Oracle database, numbers Force 2 decimal places to display-Alibaba Cloud
Delete the characters with the least number of occurrences in the string [JS, map sorting, regular]