Skip to content

Commit 1be1dd7

Browse files
aQuaaQua
authored andcommitted
825 finish
1 parent dc143ce commit 1be1dd7

File tree

1 file changed

+30
-15
lines changed

1 file changed

+30
-15
lines changed
Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,39 @@
11
package problem0825
22

33
func numFriendRequests(ages []int) int {
4-
size := len(ages)
4+
count := [121]int{}
5+
for i := range ages {
6+
count[ages[i]]++
7+
}
8+
9+
sums := [121]int{}
10+
sum := 0
511
res := 0
6-
for i := 0; i < size; i++ {
7-
for j := 0; j < size; j++ {
8-
if i == j || isNotRequest(ages[i], ages[j]) {
9-
continue
10-
}
11-
res++
12+
for a := range count {
13+
sum += count[a]
14+
sums[a] = sum
15+
16+
if a <= 14 || count[a] == 0 {
17+
// 此时 a/2+7 >= a
18+
continue
1219
}
20+
21+
b := a/2 + 7
22+
23+
res += count[a] * (sums[a] - sums[b] - 1)
24+
// sums[a] - sums[b],年龄段 (b,a] 中的人数
25+
// sums[a] - sums[b] - 1,减一是因为不能和自己交朋友
26+
//
27+
// 题目中的 3 条交友规则,归纳总结一下,就是
28+
// 年龄为 a 的人
29+
// 只能和年龄段 (a/2+7, a] 中除了自己以外的人交朋友,
1330
}
31+
1432
return res
1533
}
1634

17-
func isNotRequest(a, b int) bool {
18-
if b <= a/2+7 ||
19-
b > a ||
20-
(b > 100 && a < 100) {
21-
return true
22-
}
23-
return false
24-
}
35+
/**
36+
* 当 age[B] <= 0.5 * age[A] + 7 和 age[B] > age[A] 成立时
37+
* age[B] > 100 && age[A] < 100 一定成立,所以这个限制是障眼法
38+
*
39+
**/

0 commit comments

Comments
 (0)