当前位置:网站首页>Maximum average value of continuous interval
Maximum average value of continuous interval
2022-07-26 04:03:00 【Su Pimo】
catalog
Given an array A, And length Len;
Choose a length >= Len The range of [l, r], Its average value is the largest .
a n s = ∑ i = l r A [ i ] r − l + 1 ans = \frac{\sum_{i = l}^{r} {A[i]}}{r - l + 1} ans=r−l+1∑i=lrA[i]
namely , The denominator The smaller it is , molecular The bigger it is . The better .
such as : A = [100, 1, 100], Len = 2[100, 1] and [1, 100] All are 50.5;[100, 1, 100] It's the biggest
First, look at a wrong idea .
Sweep code r Endpoint , Calculate with r For the right endpoint , Maximum .
such as , We have got r-1 For the right endpoint , The largest interval is : [l, r-1]; Assuming the current r The maximum interval of the endpoint is : [ll, r] ( Of course, the length of these two intervals is >= Len Of )
that , Can they deduce ?
First , ll >= l It is verifiable ; therefore , The left endpoint is monotonous ;
Algorithm : ( Calculation [l, r] Value and [l-1, r] Value , Compare ) ( If [l-1, r] Bigger , Then remove it. l-1) ( otherwise , [l, r] That's the answer. )
although , With r The maximum interval of the right endpoint , Its left endpoint is monotonous , namely : [l1, 1][l2, 2][l3, 3], …
There will be : l1 <= l2 <= l3
however , For a fixed right endpoint , Suppose the answer is [l, r], The answer to the previous interval is :[ll, r-1]
that , [ll, r][ll + 1, r][ll + 2, r] … Is it monotonous ??
Not really : [4, 3, 2, 1, 9]Len = 3]
The maximum interval of each right endpoint is : [4, 3, 2][4, 3, 2, 1][2, 1, 9]
about 9 Come on , [4, 3, 2, 1, 9] = 3.8, [3, 2, 1, 9] = 3.75, [2, 1, 9] = 4, It does not conform to monotony
The above algorithm , It seems to lead us into a very confused idea …
Mainly still , From the above equation , Both on the left Variable , It's all unknown ; therefore , under these circumstances , Rashly use greed , Is a bad choice .
From the above equation , If we scan unknown r, be r It is known. .
however : ( Left side ans It is unknown. ) ( On the right side of the molecular , The denominator All carry l, It's also unknown ), That is to say 2 A variable .
We are now , One must be eliminated ; l There are many options for endpoints , So choose to eliminate ans This variable .
边栏推荐
- SDL2 Opengl遇到的坑
- 5 years, 1.4W times, NFT og's road to immortality Web3 column
- PHP method to find the location of session storage file
- Operator new, operator delete supplementary handouts
- Opencv learning notes -- Hough transform
- One stop monitoring of the software and hardware infrastructure of the whole university, and Suzhou University replaces PostgreSQL with time series database
- Laravel8 implements interface authentication encapsulation using JWT
- Dat of deep learning
- Failed to install the hcmon driver
- ACM mm 2022 | end to end multi granularity comparative learning for video text retrieval
猜你喜欢

Go plus security: an indispensable security ecological infrastructure for build Web3

按键消抖的Verilog实现

Leetcode: 102. Sequence traversal of binary tree

WAF details

Connect external MySQL databases in istio Service Grid

UFS Clk Gate介绍

Matlab paper illustration drawing template issue 39 - stairs

leetcode: 102. 二叉树的层序遍历

5 years, 1.4W times, NFT og's road to immortality Web3 column

Worked overtime for a week to develop a reporting system. This low code free it reporting artifact is very easy to use
随机推荐
Worked overtime for a week to develop a reporting system. This low code free it reporting artifact is very easy to use
Tactile intelligent sharing-rk3568 application in scenic spot navigation robot
Verilog implementation of key dithering elimination
安装VMware报错failed to install the hcmon driver
PHP method to find the location of session storage file
触觉智能分享-RK3568在景区导览机器人中的应用
day03_ 1_ Idea tutorial
Find My技术|物联网资产跟踪市场规模达66亿美元,Find My助力市场发展
php 实现从1累加到100的算法
【云原生之kubernetes】kubernetes集群下ConfigMap使用方法
开源许可证的传染性问题浅析
Opencv learning notes -- Hough transform
Where does international crude oil open an account, a formal, safe and secure platform
UFS Clk Gate介绍
Aike AI frontier promotion (7.18)
Six years of automated testing from scratch, I don't regret turning development to testing
(翻译)按钮位置约定能强化用户使用习惯
[MCU simulation project] external interrupt 0 and 1 control two digit nixie tube to count
Realization of online shopping mall system based on JSP
1311_硬件设计_ICT概念、应用以及优缺点学习小结