当前位置:网站首页>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
边栏推荐
- Free soldier
- Hash哈希竞猜游戏系统开发如何开发丨哈希竞猜游戏系统开发(多套案例)
- [in-depth learning] review pytoch's 19 loss functions
- Kotlin classes and objects
- 栈:如何实现有效括号的判断?
- Decryption function calculates "task state and lifecycle management" of asynchronous task capability
- Integritee通过XCM集成至Moonriver,为其生态系统带来企业级隐私解决方案
- Creation of JVM family objects
- Is it necessary to apply for code signing certificate for software client digital signature?
- [graduation season] green ant new fermented grains wine, red mud small stove. If it snows late, can you drink a cup?
猜你喜欢
输入的查询SQL语句,是如何执行的?
Aiming at the "amnesia" of deep learning, scientists proposed that based on similarity weighted interleaved learning, they can board PNAS
What should we pay attention to when doing social media marketing? Here is the success secret of shopline sellers!
[Beijing Xunwei] i.mx6ull development board porting Debian file system
Dark horse programmer - software testing - stage 08 2-linux and database-23-30-process port related, modify file permissions, obtain port number information, program and process related operations, Li
Decryption function calculates "task state and lifecycle management" of asynchronous task capability
AP8022开关电源小家电ACDC芯片离线式开关电源IC
什么叫内卷?
B2B mall system development of electronic components: an example of enabling enterprises to build standardized purchase, sale and inventory processes
QT writing the Internet of things management platform 38- multiple database support
随机推荐
Process of manually encrypt the mass-producing firmware and programming ESP devices
[problem] Druid reports exception SQL injection violation, part always true condition not allow solution
In operation (i.e. included in) usage of SSRs filter
【深度学习】一文看尽Pytorch之十九种损失函数
PHP pseudo original API docking method
实战模拟│JWT 登录认证
What is involution?
【历史上的今天】7 月 4 日:第一本电子书问世;磁条卡的发明者出生;掌上电脑先驱诞生
Thinking on demand development
So this is the BGP agreement
c# . Net MVC uses Baidu ueditor rich text box to upload files (pictures, videos, etc.)
Huawei Nova 10 series supports the application security detection function to build a strong mobile security firewall
栈:如何实现有效括号的判断?
@Data source connection pool exhaustion caused by transactional abuse
Detailed explanation of Audi EDI invoice message
被奉为经典的「金字塔原理」,教给我们哪些PPT写作技巧?
华为nova 10系列支持应用安全检测功能 筑牢手机安全防火墙
[QNX hypervisor 2.2 user manual]6.3.1 factory page and control page
Dynamic memory management
Development and construction of DFI ecological NFT mobile mining system