当前位置:网站首页>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
边栏推荐
- New wizard effect used by BCG
- Installation and use of VMware Tools and open VM tools: solve the problems of incomplete screen and unable to transfer files of virtual machines
- Swagger suddenly went crazy
- 复杂因子计算优化案例:深度不平衡、买卖压力指标、波动率计算
- Is it necessary to apply for code signing certificate for software client digital signature?
- 2022 version of stronger jsonpath compatibility and performance test (snack3, fastjson2, jayway.jsonpath)
- Application practice | Shuhai supply chain construction of data center based on Apache Doris
- Related concepts of federal learning and motivation (1)
- Process of manually encrypt the mass-producing firmware and programming ESP devices
- On communication bus arbitration mechanism and network flow control from the perspective of real-time application
猜你喜欢

Huawei Nova 10 series supports the application security detection function to build a strong mobile security firewall

Chrome development tool: what the hell is vmxxx file

Application practice | Shuhai supply chain construction of data center based on Apache Doris

C # better operation mongodb database

Introduction to ACM combination counting

How is the entered query SQL statement executed?

ICML 2022 | Meta提出鲁棒的多目标贝叶斯优化方法,有效应对输入噪声
实践示例理解js强缓存协商缓存

Ziguang zhanrui completed the first 5g R17 IOT NTN satellite on the Internet of things in the world

多表操作-外连接查询
随机推荐
NetCore3.1 Json web token 中间件
HMM hidden Markov model and code implementation
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
Actual combat simulation │ JWT login authentication
Template_ Judging prime_ Square root / six prime method
六石编程学:关于代码,有六个得意
解密函数计算异步任务能力之「任务的状态及生命周期管理」
什么叫内卷?
Integretee integrates into Moonriver through xcm, bringing enterprise class privacy solutions to its ecosystem
Kotlin condition control
C server log module
2022 Health Exhibition, Beijing Health Expo, China Health Exhibition, great health exhibition November 13
最长的可整合子数组的长度
太方便了,钉钉上就可完成代码发布审批啦!
实践示例理解js强缓存协商缓存
Cann operator: using iterators to efficiently realize tensor data cutting and blocking processing
针对深度学习的“失忆症”,科学家提出基于相似性加权交错学习,登上PNAS
Installation and use of VMware Tools and open VM tools: solve the problems of incomplete screen and unable to transfer files of virtual machines
Small hair cat Internet of things platform construction and application model
How is the entered query SQL statement executed?