Skip to content

Commit 156bd37

Browse files
authored
Merge pull request #625 from LetMeFly666/825
添加问题“825.适龄的朋友”的代码和题解
2 parents c1c3056 + 017b500 commit 156bd37

File tree

7 files changed

+321
-4
lines changed

7 files changed

+321
-4
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-11-17 17:39:44
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2024-11-17 18:11:18
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
/*
12+
对于x:x要加:
13+
0.5x+7 < y <= x
14+
x + 7 < 2y
15+
也就是说
16+
0.5x+7 < x 可得 x>14 才能有朋友
17+
*/
18+
#ifdef FirstTry // WA: 同龄人会相互申请
19+
class Solution {
20+
public:
21+
int numFriendRequests(vector<int>& ages) {
22+
int ans = 0;
23+
sort(ages.begin(), ages.end());
24+
int l = 0;
25+
while (l < ages.size() && ages[l] <= 14) {
26+
l++;
27+
}
28+
for (int r = l + 1; r < ages.size(); r++) {
29+
while (ages[l] * 2 <= ages[r] + 7) {
30+
l++;
31+
}
32+
ans += r - l;
33+
}
34+
return ans;
35+
}
36+
};
37+
#else // FirstTry
38+
#ifdef SecondTry // WA
39+
class Solution {
40+
public:
41+
int numFriendRequests(vector<int>& ages) {
42+
int ans = 0;
43+
sort(ages.begin(), ages.end());
44+
int y_l = 0;
45+
while (y_l < ages.size() && ages[y_l] <= 14) {
46+
y_l++;
47+
}
48+
for (int y_r = y_l, x = y_l; x < ages.size(); x++, y_r = max(y_r, x)) {
49+
while (ages[y_l] * 2 <= ages[x] + 7) {
50+
y_l++;
51+
}
52+
while (y_r + 1 < ages.size() && ages[y_r] == ages[y_r + 1]) {
53+
y_r++;
54+
}
55+
printf("y: [%d, %d], x: %d\n", y_l, y_r, x); //*************
56+
ans += y_r - y_l;
57+
}
58+
return ans;
59+
}
60+
};
61+
#else // SecondTry
62+
// ThirdTry
63+
class Solution {
64+
public:
65+
int numFriendRequests(vector<int>& ages) {
66+
int ans = 0;
67+
sort(ages.begin(), ages.end());
68+
int y_l = 0;
69+
while (y_l < ages.size() && ages[y_l] <= 14) {
70+
y_l++;
71+
}
72+
for (int y_r = y_l, x = y_l; x < ages.size(); x++) {
73+
while (ages[y_l] * 2 <= ages[x] + 14) {
74+
y_l++;
75+
}
76+
while (y_r + 1 < ages.size() && ages[y_r + 1] <= ages[x]) {
77+
y_r++;
78+
}
79+
ans += y_r - y_l;
80+
}
81+
return ans;
82+
}
83+
};
84+
#endif // SecondTry
85+
#endif // FirstTry

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@
273273
|0816.模糊坐标|中等|<a href="https://leetcode.cn/problems/ambiguous-coordinates/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/11/07/LeetCode%200816.%E6%A8%A1%E7%B3%8A%E5%9D%90%E6%A0%87/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/127727007" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/ambiguous-coordinates/solutions/1954035/letmefly-816mo-hu-zuo-biao-by-tisfy-s794/" target="_blank">LeetCode题解</a>|
274274
|0817.链表组件|中等|<a href="https://leetcode.cn/problems/linked-list-components/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/10/12/LeetCode%200817.%E9%93%BE%E8%A1%A8%E7%BB%84%E4%BB%B6/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/127278261" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/linked-list-components/solution/letmefly-817lian-biao-zu-jian-by-tisfy-fw6y/" target="_blank">LeetCode题解</a>|
275275
|0822.翻转卡片游戏|中等|<a href="https://leetcode.cn/problems/card-flipping-game/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/08/02/LeetCode%200822.%E7%BF%BB%E8%BD%AC%E5%8D%A1%E7%89%87%E6%B8%B8%E6%88%8F/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/132053927" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/card-flipping-game/solutions/2368854/letmefly-822fan-zhuan-qia-pian-you-xi-sh-h1at/" target="_blank">LeetCode题解</a>|
276+
|0825.适龄的朋友|中等|<a href="https://leetcode.cn/problems/friends-of-appropriate-ages/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/11/17/LeetCode%200825.%E9%80%82%E9%BE%84%E7%9A%84%E6%9C%8B%E5%8F%8B/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/143836115" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/friends-of-appropriate-ages/solutions/2991587/letmefly-825gua-ling-de-peng-you-shuang-l83kn/" target="_blank">LeetCode题解</a>|
276277
|0826.安排工作以达到最大收益|中等|<a href="https://leetcode.cn/problems/most-profit-assigning-work/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/05/17/LeetCode%200826.%E5%AE%89%E6%8E%92%E5%B7%A5%E4%BD%9C%E4%BB%A5%E8%BE%BE%E5%88%B0%E6%9C%80%E5%A4%A7%E6%94%B6%E7%9B%8A/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/139009669" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/most-profit-assigning-work/solutions/2781072/letmefly-826an-pai-gong-zuo-yi-da-dao-zu-b31d/" target="_blank">LeetCode题解</a>|
277278
|0827.最大人工岛|困难|<a href="https://leetcode.cn/problems/making-a-large-island/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/09/18/LeetCode%200827.%E6%9C%80%E5%A4%A7%E4%BA%BA%E5%B7%A5%E5%B2%9B/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/126913736" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/making-a-large-island/solution/letmefly-827zui-da-ren-gong-dao-by-tisfy-r8r1/" target="_blank">LeetCode题解</a>|
278279
|0828.统计子串中的唯一字符|困难|<a href="https://leetcode.cn/problems/count-unique-characters-of-all-substrings-of-a-given-string/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/09/06/LeetCode%200828.%E7%BB%9F%E8%AE%A1%E5%AD%90%E4%B8%B2%E4%B8%AD%E7%9A%84%E5%94%AF%E4%B8%80%E5%AD%97%E7%AC%A6/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/126722643" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-unique-characters-of-all-substrings-of-a-given-string/solution/letmefly-828tong-ji-zi-chuan-zhong-de-we-y8fj/" target="_blank">LeetCode题解</a>|
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
---
2+
title: 825.适龄的朋友
3+
date: 2024-11-17 18:11:16
4+
tags: [题解, LeetCode, 中等, 数组, 双指针, 二分查找, 排序]
5+
---
6+
7+
# 【LetMeFly】825.适龄的朋友:双指针(排序nlog n) 或 桶排序(n + C^2)
8+
9+
力扣题目链接:[https://leetcode.cn/problems/friends-of-appropriate-ages/](https://leetcode.cn/problems/friends-of-appropriate-ages/)
10+
11+
<p>在社交媒体网站上有 <code>n</code> 个用户。给你一个整数数组 <code>ages</code> ,其中 <code>ages[i]</code> 是第 <code>i</code> 个用户的年龄。</p>
12+
13+
<p>如果下述任意一个条件为真,那么用户 <code>x</code> 将不会向用户 <code>y</code>(<code>x != y</code>)发送好友请求:</p>
14+
15+
<ul>
16+
<li><code>ages[y] &lt;= 0.5 * ages[x] + 7</code></li>
17+
<li><code>ages[y] &gt; ages[x]</code></li>
18+
<li><code>ages[y] &gt; 100 &amp;&amp; ages[x] &lt; 100</code></li>
19+
</ul>
20+
21+
<p>否则,<code>x</code> 将会向 <code>y</code> 发送一条好友请求。</p>
22+
23+
<p>注意,如果 <code>x</code> 向 <code>y</code> 发送一条好友请求,<code>y</code> 不必也向 <code>x</code> 发送一条好友请求。另外,用户不会向自己发送好友请求。</p>
24+
25+
<p>返回在该社交媒体网站上产生的好友请求总数。</p>
26+
27+
<p>&nbsp;</p>
28+
29+
<p><strong>示例 1:</strong></p>
30+
31+
<pre>
32+
<strong>输入:</strong>ages = [16,16]
33+
<strong>输出:</strong>2
34+
<strong>解释:</strong>2 人互发好友请求。
35+
</pre>
36+
37+
<p><strong>示例 2:</strong></p>
38+
39+
<pre>
40+
<strong>输入:</strong>ages = [16,17,18]
41+
<strong>输出:</strong>2
42+
<strong>解释:</strong>产生的好友请求为 17 -&gt; 16 ,18 -&gt; 17 。
43+
</pre>
44+
45+
<p><strong>示例 3:</strong></p>
46+
47+
<pre>
48+
<strong>输入:</strong>ages = [20,30,100,110,120]
49+
<strong>输出:</strong>3
50+
<strong>解释:</strong>产生的好友请求为 110 -&gt; 100 ,120 -&gt; 110 ,120 -&gt; 100 。
51+
</pre>
52+
53+
<p>&nbsp;</p>
54+
55+
<p><strong>提示:</strong></p>
56+
57+
<ul>
58+
<li><code>n == ages.length</code></li>
59+
<li><code>1 &lt;= n &lt;= 2 * 10<sup>4</sup></code></li>
60+
<li><code>1 &lt;= ages[i] &lt;= 120</code></li>
61+
</ul>
62+
63+
64+
65+
## 方法一:双指针
66+
67+
如果满足第三条`ages[y] > 100 && ages[x] < 100`,那么一定满足第二条`ages[y] > ages[x]`,因此第三条可以忽略(只要满足第二条,不需要看是否满足第三条就一定不会受到邀请)。
68+
69+
由第一条和第二条可知,当`y`满足`0.5x+7 < y <= x``y`才会收到来自`x`的邀请。由`0.5x+7 < x`可得只有`>= 15``x`才会有可能发送邀请。
70+
71+
对于邀请发送者`x``y`的最小值需要满足`y > 0.5x+7``y`的最大值需要满足`y <= x`
72+
73+
假设`y_l`是第一个满足`> 0.5x+7``y`下标,`y_r`是最后一个满足`<= x``y`下标,那么对于区间`[y_l, y_r]`,只有`x`自身不会收到`x`的邀请,其他用户都会收到`x`的邀请。因此`x`的邀请发送数量为`区间长度 - 1 = y_r - y_l`
74+
75+
不难发现随着`x`的非递减,`y`区间的左右端点`y_l``y_r`也是非递减的,因此就可以使用双指针来实现每个元素只被遍历数次。
76+
77+
+ 时间复杂度$O(n\log n)$:排序时间复杂度$O(n\log n)$,双指针时间复杂度$O(n)$
78+
+ 空间复杂度$O(\log n)$
79+
80+
### AC代码
81+
82+
#### C++
83+
84+
```cpp
85+
/*
86+
* @Author: LetMeFly
87+
* @Date: 2024-11-17 17:39:44
88+
* @LastEditors: LetMeFly.xyz
89+
* @LastEditTime: 2024-11-17 18:11:18
90+
*/
91+
/*
92+
对于x:x要加:
93+
0.5x+7 < y <= x
94+
x + 7 < 2y
95+
也就是说
96+
0.5x+7 < x 可得 x>14 才能有朋友
97+
*/
98+
class Solution {
99+
public:
100+
int numFriendRequests(vector<int>& ages) {
101+
int ans = 0;
102+
sort(ages.begin(), ages.end());
103+
int y_l = 0;
104+
while (y_l < ages.size() && ages[y_l] <= 14) {
105+
y_l++;
106+
}
107+
for (int y_r = y_l, x = y_l; x < ages.size(); x++) {
108+
while (ages[y_l] * 2 <= ages[x] + 14) {
109+
y_l++;
110+
}
111+
while (y_r + 1 < ages.size() && ages[y_r + 1] <= ages[x]) {
112+
y_r++;
113+
}
114+
ans += y_r - y_l;
115+
}
116+
return ans;
117+
}
118+
};
119+
```
120+
121+
## 方法二:桶排序
122+
123+
有没有一种办法避免方法一的时间瓶颈——排序呢?当然有。
124+
125+
不难发现每个人的年龄范围是`1`到`120`,因此我们只需要统计一下每个年龄段分别有多少人,再枚举`x`和`y`的年龄判定是否符合第一第二两个条件就好了。
126+
127+
+ 时间复杂度$O(n + C^2)$:其中$C=120$
128+
+ 空间复杂度$O(C)$
129+
130+
### AC代码
131+
132+
#### C++
133+
134+
```cpp
135+
/*
136+
* @Author: LetMeFly
137+
* @Date: 2021-12-27 09:00:07
138+
* @LastEditors: LetMeFly
139+
* @LastEditTime: 2021-12-27 09:07:32
140+
*/
141+
int a[121];
142+
class Solution {
143+
public:
144+
// age[y] * 2 <= age[x] + 14
145+
int numFriendRequests(vector<int>& ages) {
146+
for (int i = 0; i < 121; i++)
147+
a[i] = 0;
148+
for (int& t : ages)
149+
a[t]++;
150+
int ans = 0;
151+
for (int y = 1; y <= 120; y++) {
152+
for (int x = y; x <= 120; x++) {
153+
if (y * 2 <= x + 14 || (y > 100 && x < 100))
154+
continue;
155+
ans += x == y ? a[x] * (a[x] - 1) : a[x] * a[y];
156+
}
157+
}
158+
return ans;
159+
}
160+
};
161+
```
162+
163+
> 同步发文于CSDN和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2024/11/17/LeetCode%200825.%E9%80%82%E9%BE%84%E7%9A%84%E6%9C%8B%E5%8F%8B/)哦~
164+
>
165+
> Tisfy:[https://letmefly.blog.csdn.net/article/details/143836115](https://letmefly.blog.csdn.net/article/details/143836115)

Solutions/Other-Accumulation-SomeTips.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,45 @@ git clone --branch 要clone的分支 --single-branch 仓库地址
2222
git clone --branch paper --single-branch [email protected]:LetMeFly666/SecFFT.git
2323
```
2424

25+
### 一些命令
26+
27+
#### 版本回退
28+
29+
+ 当前版本:`HEAD`
30+
+ 上个版本:`HEAD^`
31+
+ 上上个版本:`HEAD^^`
32+
+ 上100个版本:`HEAD~100`
33+
34+
```bash
35+
git reset --hard HEAD^
36+
```
37+
38+
其中`--hard`会回退到上个版本的已提交状态,`--soft`会回退到上个版本的未提交状态,`--mixed`会回退到上个版本已添加但未提交的状态。
39+
40+
#### git reflog
41+
42+
引用日志。
43+
44+
假如我使用了`git reset`回到了历史版本,我如何回来呢?可以`git reflog`查看回退前的commit id,然后`git reset --hard COMMIT_ID`
45+
46+
#### 丢弃工作区/暂存区更改
47+
48+
**丢弃工作区更改:**
49+
50+
```bash
51+
git checkout -- filename
52+
```
53+
54+
丢弃filename在工作区的更改。如果暂存区有此文件的版本则回到暂存区的版本,否则回到版本库的版本。
55+
56+
**丢弃暂存区更改:**
57+
58+
```bash
59+
git reset HEAD filename
60+
```
61+
62+
会把filename文件的更改从暂存区回退到工作区。
63+
2564
## About HTML
2665

2766
### 空白字符

Solutions/Other-English-LearningNotes-SomeWords.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,11 @@ tags: [其他, 知识, 英语, Notes]
800800
|||
801801
|shrine|n. 神庙,神龛,圣地|
802802
|engrave|v. 在...上雕刻(文字/图案)|
803+
|||
804+
|stink|v. 有臭味,有难闻的气味,让人觉得很糟糕,令人厌恶<br/>n. 恶臭,难闻气味,吵闹,争吵|
805+
|plateau|n. 高原,稳定期,停滞期<br/>v. 达到平稳阶段,处于停滞状态|
806+
|ferryboat|n. 渡船|
807+
|mince|v. 用绞肉机绞(事物,尤指肉),装模作样地小步快走<br/>n. 肉末(优质牛肉)|
803808

804809
<p class="wordCounts">单词收录总数</p>
805810

temp-PythonTeaching.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,22 @@
22
* @Author: LetMeFly
33
* @Date: 2024-11-11 22:43:08
44
* @LastEditors: LetMeFly.xyz
5-
* @LastEditTime: 2024-11-16 13:26:55
5+
* @LastEditTime: 2024-11-17 14:50:55
66
-->
77
俺也先列个题纲,周一找杨老师准备:
88

99
1. 开题 联邦学习还是流量监测(没有相关小论文支撑)
1010
2. UIC论文PPT、演讲 询问一下一般怎么个情况
1111
3. UIC论文订机票吗,几号到几号,都谁去
1212
4. JS项目陆续后续会议快结了,学弟们不怎么懂
13-
5. 进度缓慢的焦虑、怕干不好项目的焦虑
13+
5. JS要对接成电
14+
6. 进度缓慢的焦虑、怕干不好项目的焦虑
1415

1516

1617
ToYou:
1718

1819
1. 豆包MarsCodes发布会
1920
2. RUC跆拳道er
2021
3. 北邮VPN+手机热点 为何不能访问校内资源(之前解释有误)
22+
4. jk专家咨询费
23+

tryGo/JSFUZZ.bash

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1-
尽量写写吧[叹气] (我只是一个传话的[捂脸]
1+
甲方那边又有通知了,说成电的来了,想让明天去拷个测试用例
22

3-
真遇到什么困难了也可以找我帮忙
3+
不过应该挺快的,明天下午去一个就行
4+
5+
6+
7+
其实倒是只需要拷个数据,先不用考虑这些数据怎么使用就行
8+
9+
不知道会以光盘还是什么形式交付
10+
11+
12+
13+
14+
大概就是传个文件,如果甲方给光盘的话带回来就行
15+
甲方给U盘的话复制到机房那台电脑上就行
16+
17+
拷的是什么和怎么用倒是暂时不用关心
18+
19+
20+
21+
22+
这次还正好在周末[叹气]

0 commit comments

Comments
 (0)