当前位置:网站首页>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
边栏推荐
- Write it down once Net analysis of thread burst height of an industrial control data acquisition platform
- Template_ Large integer subtraction_ Regardless of size
- 2022 version of stronger jsonpath compatibility and performance test (snack3, fastjson2, jayway.jsonpath)
- Thinking on demand development
- 华为云云商店首页 Banner 资源位申请
- FS4061A升压8.4V充电IC芯片和FS4061B升压12.6V充电IC芯片规格书datasheet
- Qt编写物联网管理平台38-多种数据库支持
- 记一次 .NET 某工控数据采集平台 线程数 爆高分析
- Cbcgptabwnd control used by BCG (equivalent to MFC TabControl)
- Application practice | Shuhai supply chain construction of data center based on Apache Doris
猜你喜欢

ICML 2022 | Meta提出鲁棒的多目标贝叶斯优化方法,有效应对输入噪声

九齐NY8B062D MCU规格书/datasheet

C语言-入门-基础-语法-流程控制(七)

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

Actual combat simulation │ JWT login authentication

Form组件常用校验规则-1(持续更新中~)

Qt编写物联网管理平台38-多种数据库支持

Neural network IOT platform construction (IOT platform construction practical tutorial)

针对深度学习的“失忆症”,科学家提出基于相似性加权交错学习,登上PNAS

QT writing the Internet of things management platform 38- multiple database support
随机推荐
2022 Health Exhibition, health exhibition, Beijing Great Health Exhibition and health industry exhibition were held in November
更强的 JsonPath 兼容性及性能测试之2022版(Snack3,Fastjson2,jayway.jsonpath)
Delete the characters with the least number of occurrences in the string [JS, map sorting, regular]
[QNX Hypervisor 2.2用户手册]6.3.1 工厂页和控制页
Kotlin condition control
[in-depth learning] review pytoch's 19 loss functions
凌云出海记 | 文华在线&华为云:打造非洲智慧教学新方案
Selected review | machine learning technology for Cataract Classification / classification
[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
华为云云商店首页 Banner 资源位申请
Process of manually encrypt the mass-producing firmware and programming ESP devices
C语言-入门-基础-语法-流程控制(七)
HMM hidden Markov model and code implementation
Free soldier
Pointnet / pointnet++ point cloud data set processing and training
Chrome开发工具:VMxxx文件是什么鬼
On communication bus arbitration mechanism and network flow control from the perspective of real-time application
Integretee integrates into Moonriver through xcm, bringing enterprise class privacy solutions to its ecosystem
【ISMB2022教程】图表示学习的精准医疗,哈佛大学Marinka Zitnik主讲,附87页ppt