当前位置:网站首页>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
边栏推荐
- 【FPGA教程案例7】基于verilog的计数器设计与实现
- Which water in the environment needs water quality monitoring
- Bicolor case
- 雲原生——上雲必讀之SSH篇(常用於遠程登錄雲服務器)
- Google Chrome Portable Google Chrome browser portable version official website download method
- Tar source code analysis Part 10
- The solution of win11 taskbar right click without Task Manager - add win11 taskbar right click function
- 同一个job有两个source就报其中一个数据库找不到,有大佬回答下吗
- Download kicad on Alibaba cloud image station
- C语言中的排序,实现从小到大的数字排序法
猜你喜欢
notepad++如何统计单词数量
响应式移动Web测试题
【MySQL】数据库视图的介绍、作用、创建、查看、删除和修改(附练习题)
17-18. Dependency scope and life cycle plug-ins
2022 Xinjiang's latest eight members (Safety Officer) simulated examination questions and answers
图的底部问题
[GF (q) + LDPC] regular LDPC coding and decoding design and MATLAB simulation based on the GF (q) field of binary graph
Overview of convolutional neural network structure optimization
MySQL relearn 2- Alibaba cloud server CentOS installation mysql8.0
【GF(q)+LDPC】基于二值图GF(q)域的规则LDPC编译码设计与matlab仿真
随机推荐
1、 Relevant theories and tools of network security penetration testing
Matlab remainder
【FPGA教程案例8】基于verilog的分频器设计与实现
How does the recv of TCP socket receive messages of specified length?
Cervical vertebra, beriberi
STM32 单片机ADC 电压计算
Software keywords and process information intercepted by Golden Shield video player
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?
R统计绘图-随机森林分类分析及物种丰度差异检验组合图
Boast about Devops
what the fuck! If you can't grab it, write it yourself. Use code to realize a Bing Dwen Dwen. It's so beautiful ~!
Displaying currency in Indian numbering format
[FPGA tutorial case 8] design and implementation of frequency divider based on Verilog
tars源码分析之1
2022年,或许是未来10年经济最好的一年,2022年你毕业了吗?毕业后是怎么计划的?
[GF (q) + LDPC] regular LDPC coding and decoding design and MATLAB simulation based on the GF (q) field of binary graph
Appium基础 — APPium安装(二)
24 magicaccessorimpl can access the debugging of all methods
校园网络问题
Summary of June 2022