当前位置:网站首页>leetcode825. Age appropriate friends
leetcode825. Age appropriate friends
2022-07-04 06:48:00 【2021dragon】

LeetCode Series articles
List of articles
One 、 Title Description
On social media sites n n n Users . Give you an array of integers a g e s ages ages, among a g e s [ i ] ( 1 ≤ a g e s [ i ] ≤ 120 ) ages[i](1\leq ages[i] \leq 120) ages[i](1≤ages[i]≤120) It's No i i i Age of users .
If any of the following conditions is true , So users x x x The user will not be y y y Send friend request :
- a g e s [ y ] ≤ 0.5 × a g e s [ x ] + 7 ages[y]\leq0.5\times ages[x]+7 ages[y]≤0.5×ages[x]+7
- a g e s [ y ] > a g e s [ x ] ages[y]>ages[x] ages[y]>ages[x]
- a g e s [ y ] > 100 & & a g e s [ x ] < 100 ages[y]>100\&\&ages[x]<100 ages[y]>100&&ages[x]<100
otherwise , x x x Will y y y Send a friend request .
Be careful , If x x x towards y y y Send a friend request , y y y You don't have to x x x Send a friend request . in addition , Users will not send friend requests to themselves .
Returns the total number of friend requests generated on this social media site .
Two 、 Example
Input : ages = [16, 16]
Output : 2
explain : 2 People send friend requests to each other .
3、 ... and 、 Main idea
1、 Violence solution
It is very simple to solve the problem with violent solution , To determine which users a user can send friend requests , Just traverse this n − 1 n-1 n−1( Not including myself ) Users , Judge whether the conditions for sending friend requests are met in turn .
Obviously, the time complexity of this violent solution is O ( N 2 ) O(N^2) O(N2), When the amount of user data is too large, the computing time will soar , Therefore, this solution is not recommended .
2、 Sort + Double pointer
Carefully observe the three conditions given by the topic , You will find that condition three is actually contained in condition two , If condition 3 is met, condition 2 must be met , So as long as the first two conditions are not met , So users x x x You can tell users y y y Send friend request , That is, users y y y Need to meet : 0.5 × a g e s [ x ] + 7 < a g e s [ y ] ≤ a g e s [ x ] 0.5\times ages[x]+7<ages[y]\leq ages[x] 0.5×ages[x]+7<ages[y]≤ages[x]
This relationship is represented by a number axis as follows :
If there are users who meet the requirements y y y, that 0.5 × x + 7 < x 0.5\times x+7<x 0.5×x+7<x, namely x > 14 x > 14 x>14. in other words , If a user's age is less than or equal to 14, Then the user will not be able to send friend requests to any user .
Now it seems , Want to know the user x x x To which users y y y Send friend request , In fact, it is necessary to find the corresponding left and right boundaries on the number axis , Users whose age is in this area are users x x x Users who can send friend requests .
When looking for the left and right boundaries, you can use l e f t left left and r i g h t right right Subscript indicating the left and right boundaries ( Double pointer ), If you use closed intervals on both sides , namely l e f t left left It points to the first user who meets the requirements , r i g h t right right It refers to the last user who meets the requirements , So in the end r i g h t − l e f t right-left right−left The value of is the user who meets the requirements y y y The number of .( Be careful : It's not here r i g h t − l e f t + 1 right-left+1 right−left+1, Because the user x x x It is also in the region , But users x x x Don't send friend requests to yourself )
So the steps to solve the problem are as follows :
- To the given n n n Sort by the age of users .
- Traverse this one by one n n n Age of users .
- When traversing users , If the user's age is less than or equal to 14, Then the number of friend requests that the user can send out is 0; If the user is older than 14, Then find the area that meets the transmission requirements through the double pointer , Then we can get the number of friend requests that the user can send out .
- Finally, add up the number of friend requests that each user can send out .
Particular attention , When determining the age area that meets the requirements through the double pointer , Only for the first time n n n Search from scratch based on users . Later, when determining the area , The left and right boundaries of the region only need to be searched backwards based on the left and right boundaries of the previous user . because 0.5 × x + 7 0.5\times x+7 0.5×x+7 And x x x It's a linear relationship , When x x x When it increases , 0.5 × x + 7 0.5\times x+7 0.5×x+7 The value of will also increase , So we don't need to judge the last user's l e f t left left Whether the user's age before the pointer is in this limited area .
3、 Count sorting + The prefix and
If you look closely, you will find that the age of the given user of the title is [ 1 , 120 ] [1, 120] [1,120] Within the scope of , Therefore, we can count and sort the number of users of all ages .
For example, we store the sorting results in c n t cnt cnt In the array , here c n t [ a g e ] cnt[age] cnt[age] It means that the age is a g e age age Number of users , And each age is a g e age age The number of friend requests that users can send out is : ( ∑ i = 0.5 × a g e + 8 a g e c n t [ i ] ) − 1 (\sum_{i=0.5\times age+8}^{age}cnt[i])-1 (∑i=0.5×age+8agecnt[i])−1, One minus one is because users will not send friend requests to themselves .
To avoid traversing every time c n t cnt cnt Sum arrays , We can calculate c n t cnt cnt Array prefix and array p r e pre pre, p r e [ a g e ] pre[age] pre[age] It represents the age group located in 1 ~ age Number of users : p r e [ a g e ] = ∑ i = 1 a g e c n t [ i ] pre[age]=\sum_{i=1}^{age}cnt[i] pre[age]=∑i=1agecnt[i].
At this time, each age is a g e age age The number of friend requests that users can send out can be written as : p r e [ a g e ] − p r e [ 0.5 × a g e + 7 ] − 1 pre[age]-pre[0.5\times age+7]-1 pre[age]−pre[0.5×age+7]−1
At this point, we can O ( 1 ) O(1) O(1) Calculate an age of a g e age age The number of friend requests that users of can send out , Multiply this value by c n t [ a g e ] cnt[age] cnt[age] It is the total number of friend requests that all users in this age group can send out .
So the steps to solve the problem are as follows :
- Count and sort the number of users in each age group .
- Count out c n t cnt cnt Array prefix and array p r e pre pre.
- adopt ( p r e [ a g e ] − p r e [ 0.5 × a g e + 7 ] − 1 ) × c n t [ a g e ] (pre[age]-pre[0.5\times age+7]-1)\times cnt[age] (pre[age]−pre[0.5×age+7]−1)×cnt[age] The formula calculates the total number of friend requests that users of each age group can send out , Adding them up is the final result .
Four 、 Code implementation
1、 Violence solution

2、 Sort + Double pointer

3、 Count sorting + The prefix and

边栏推荐
- 响应式移动Web测试题
- Tar source code analysis Part 10
- Mysql 45讲学习笔记(七)行锁
- 2022 is probably the best year for the economy in the next 10 years. Did you graduate in 2022? What is the plan after graduation?
- Fundamentals of SQL database operation
- Another company raised the price of SAIC Roewe new energy products from March 1
- Background and current situation of domestic CDN acceleration
- Download kicad on Alibaba cloud image station
- Highly paid programmers & interview questions: how does redis of series 119 realize distributed locks?
- [GF (q) + LDPC] regular LDPC coding and decoding design and MATLAB simulation based on the GF (q) field of binary graph
猜你喜欢

Which water in the environment needs water quality monitoring

leetcode 310. Minimum Height Trees

Variables d'environnement personnalisées uniapp

notepad++如何统计单词数量

关于IDEA如何设置快捷键集

期末周,我裂开

树形dp

【网络数据传输】基于FPGA的百兆网/兆网千UDP数据包收发系统开发,PC到FPGA

响应式移动Web测试题

2022 where to find enterprise e-mail and which is the security of enterprise e-mail system?
随机推荐
The sorting in C language realizes the number sorting method from small to large
关于IDEA如何设置快捷键集
Redis interview question set
The solution of win11 taskbar right click without Task Manager - add win11 taskbar right click function
Analysis of tars source code 1
MySQL relearn 2- Alibaba cloud server CentOS installation mysql8.0
Design of test cases
Cochez une colonne d'affichage dans une colonne de tableau connue
【MySQL】数据库视图的介绍、作用、创建、查看、删除和修改(附练习题)
2022, peut - être la meilleure année économique de la prochaine décennie, avez - vous obtenu votre diplôme en 2022? Comment est - ce prévu après la remise des diplômes?
2022 is probably the best year for the economy in the next 10 years. Did you graduate in 2022? What is the plan after graduation?
【GF(q)+LDPC】基于二值图GF(q)域的规则LDPC编译码设计与matlab仿真
Tar source code analysis Part 3
Can the out of sequence message complete TCP three handshakes
Tar source code analysis 4
Selection (021) - what is the output of the following code?
颈椎、脚气
Mysql 45讲学习笔记(十一)字符串字段怎么加索引
R statistical mapping - random forest classification analysis and species abundance difference test combination diagram
Tar source code analysis 8