当前位置:网站首页>Prefix and (one dimension)
Prefix and (one dimension)
2022-06-28 11:57:00 【Q_ ming_ code】
The prefix and
Preface
Space complexity has a very important influence on the running time of code
effect , In the code, prefix and are properly used for maintenance
It can save time and complexity .
One 、 What is the prefix and ?
The prefix and , Popular said : For an array with some numbers , The prefix is the prefix of the array k Value of item addition , Therefore, the corresponding prefix sum is before the array k Sum of items . For an array , ask [L,R] And ( From L Items to R The sum of the values of the items ), You can use prefix and to solve .
Two 、 One dimensional prefix and explanation
The one-dimensional prefix sum is probably : There is a N An array of numbers arr[ ], On the q Times of inquiry [L,R] And .
Illustrate with examples
Array arr[ ] By 1,3,7,5,2 Form in order , On the 3 The second inquiry is
[2,4],[0,3],[3,4] And .
General solution :
For the first time : Interval and [2,4]=7+2+5=14;
The second time : Interval and [0,3]=1+3+7+5=16;
For the third time : Interval and [3,4]=5+2=7;
It would be too much trouble to add the array one by one before each query , At this point we can introduce prefixes and arrays :
sum[i]=sum[i-1]+arr[i] i>0
sum[0]=arr[0] i=0
Explain it. sum[i]==arr[0]+arr[i]+…+arr[i], namely sum[i] Namely arr Array from 0 Items to i Sum of items .
arr 1 3 7 5 2
sum 1 4 11 16 18
from sum It is much easier to solve the problem by array
For the first time : Interval and [2,4]=sum[4]-sum[1]=18-4=14=7+5+2
reason : take arr The array is divided into two pieces , One piece is made of 0 Item and the first item form the non required part ,
The other is from the 2 Items to 4 The required part of the item , The two together make up the whole sum[4],
From the first 0 The non desired part of the term and the first term can be regarded as sum[1],
Required part = whole sum[4]- Not required sum[1]
| Not required | Required |
|---|---|
| 1 3 | 7 5 2 |
Expand the scope , Open your eyes ,
Set interval and [L,R] use sum[L,R] Express
According to the first solving process , We can launch
sum[L,R]=sum[R]-sum[L-1] L>0
sum[L.R]=sum[R] L=0
The code is as follows ( Example ):
#include<iostream>
using namespace std;
const int n=5;
int sum[n];
int get_sum(int L,int R)
{
if(L!=0)
return sum[R]-sum[L-1];
else
return sum[R];
}
int main()
{
int arr[n]={
1,3,7,5,2};
sum[0]=arr[0];
for(int i=1;i<n;i++)
sum[i]=sum[i-1]+arr[i];
cout<<get_sum(2,4)<<endl;
cout<<get_sum(0,3)<<endl;
cout<<get_sum(3,4)<<endl;
return 0;
}
Code presents , Help to understand
summary
This chapter is about one-dimensional prefixes and simplifications , I passed through b Station learning , It's my own notes , I hope you can also learn .
边栏推荐
- Web3安全连载(3) | 深入揭秘NFT钓鱼流程及防范技巧
- day34 js笔记 正则表达式 2021.09.29
- Timestamp and date conversion "suggested collection"
- Jetpack Compose Desktop 桌面版本的打包和发布应用
- [semidrive source code analysis] [x9 chip startup process] 32 - play module analysis - RTOS side
- Redis hash hash type string (5)
- [sciter]: how sciter uses i18 to realize multi language switching of desktop applications and its advantages and disadvantages
- TiDB v6.0.0 (DMR) :缓存表初试丨TiDB Book Rush
- Contract quantification system development (construction explanation) - contract quantification system development (source code analysis and ready-made cases)
- Share the easy-to-use fastadmin open source system - practical part
猜你喜欢

如临现场的视觉感染力,NBA决赛直播还能这样看?

Industry analysis - quick intercom, building intercom

Cannot redeclare block range variables
![Connectionreseterror: [winerror 10054] the remote host forced an existing connection to be closed](/img/9a/97813f5ac4d7c15711891cff25b9dd.jpg)
Connectionreseterror: [winerror 10054] the remote host forced an existing connection to be closed

Redis6 1: what problems can be solved by the introduction of NoSQL and redis?

Day33 JS note event (Part 2) September 28, 2021

If you want to change to software testing, how can you package your resume as a test engineer with 1 year of work experience

JS foundation 8

Software test interview classic + 1000 high-frequency real questions, and the hit rate of big companies is 80%

Tidb v6.0.0 (DMR): initial test of cache table - tidb Book rush
随机推荐
day36 js笔记 ECMA6语法 2021.10.09
js中的数组方法 2021.09.18
智联招聘基于 Nebula Graph 的推荐实践分享
行业分析| 快对讲,楼宇对讲
Day28 strict mode, string JS 2021.09.22
100 important knowledge points that SQL must master: retrieving data
5. Sum of N numbers
Web3 security serials (3) | in depth disclosure of NFT fishing process and prevention techniques
Cannot redeclare block range variables
6.A-B
Swin, three degrees! Eth open source VRT: a transformer that refreshes multi domain indicators of video restoration
赛尔号抽奖模拟求期望
Practice and Thinking on the architecture of a set of 100000 TPS im integrated message system
Recommended practice sharing of Zhilian recruitment based on Nebula graph
NFT card chain game system development DAPP construction technical details
day25 js中的预解析、递归函数、事件 2021.09.16
MySQL cannot query the maximum value using the max function
ProCAST finite element casting process simulation software
Day24 JS notes 2021.09.15
day29 js笔记 2021.09.23