Skip to content

Commit dc143ce

Browse files
aQuaaQua
authored andcommitted
825 Limit Time Exceeded
1 parent e219b22 commit dc143ce

File tree

5 files changed

+137
-7
lines changed

5 files changed

+137
-7
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# [825. Friends Of Appropriate Ages](https://leetcode.com/problems/friends-of-appropriate-ages/)
2+
3+
## 题目
4+
5+
Some people will make friend requests. Thelist of their ages is given andages[i]is the age of theith person.
6+
7+
Person A will NOT friend request person B (B != A) if any of the following conditions are true:
8+
9+
- age[B]<= 0.5 * age[A]+ 7
10+
- age[B]> age[A]
11+
- age[B]> 100 &&age[A]< 100
12+
13+
Otherwise, A will friend request B.
14+
15+
Note that ifA requests B, B does not necessarily request A. Also, people will not friend request themselves.
16+
17+
How many total friend requests are made?
18+
19+
Example 1:
20+
21+
```text
22+
Input: [16,16]
23+
Output: 2
24+
Explanation: 2 people friend request each other.
25+
```
26+
27+
Example 2:
28+
29+
```text
30+
Input: [16,17,18]
31+
Output: 2
32+
Explanation: Friend requests are made 17 -> 16, 18 -> 17.
33+
```
34+
35+
Example 3:
36+
37+
```text
38+
Input: [20,30,100,110,120]
39+
Output:
40+
Explanation: Friend requests are made 110 -> 100, 120 -> 110, 120 -> 100.
41+
```
42+
43+
Notes:
44+
45+
1. 1 <= ages.length<= 20000.
46+
1. 1 <= ages[i] <= 120.
47+
48+
## 解题思路
49+
50+
见程序注释
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package problem0825
2+
3+
func numFriendRequests(ages []int) int {
4+
size := len(ages)
5+
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+
}
13+
}
14+
return res
15+
}
16+
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+
}

Algorithms/0825.friends-of-appropriate-ages/friends-of-appropriate-ages_test.go

Lines changed: 54 additions & 0 deletions
Large diffs are not rendered by default.

Helper/problemReadme.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ func replaceCharacters(s string) string {
5353
"&gt;": ">",
5454
"&nbsp;": "",
5555
"&#39;": "'",
56+
"&amp;": "&",
5657
"\n\n\n": "\n\n",
58+
" \n": "\n",
5759
}
5860
for old, new := range changeMap {
5961
s = strings.Replace(s, old, new, -1)

leetcode.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"Username": "aQuaYi",
33
"Ranking": 1089,
4-
"Updated": "2018-05-21T10:15:29.898306051+08:00",
4+
"Updated": "2018-05-21T16:20:26.257356889+08:00",
55
"Record": {
66
"Easy": {
77
"Solved": 169,
@@ -7117,7 +7117,7 @@
71177117
"ID": 591,
71187118
"Title": "Tag Validator",
71197119
"TitleSlug": "tag-validator",
7120-
"PassRate": "31%",
7120+
"PassRate": "30%",
71217121
"Difficulty": "Hard",
71227122
"IsAccepted": true,
71237123
"IsPaid": false,
@@ -9097,7 +9097,7 @@
90979097
"ID": 756,
90989098
"Title": "Pyramid Transition Matrix",
90999099
"TitleSlug": "pyramid-transition-matrix",
9100-
"PassRate": "46%",
9100+
"PassRate": "45%",
91019101
"Difficulty": "Medium",
91029102
"IsAccepted": true,
91039103
"IsPaid": false,
@@ -9457,7 +9457,7 @@
94579457
"ID": 786,
94589458
"Title": "K-th Smallest Prime Fraction",
94599459
"TitleSlug": "k-th-smallest-prime-fraction",
9460-
"PassRate": "32%",
9460+
"PassRate": "31%",
94619461
"Difficulty": "Hard",
94629462
"IsAccepted": true,
94639463
"IsPaid": false,
@@ -9877,7 +9877,7 @@
98779877
"ID": 821,
98789878
"Title": "Shortest Distance to a Character",
98799879
"TitleSlug": "shortest-distance-to-a-character",
9880-
"PassRate": "62%",
9880+
"PassRate": "63%",
98819881
"Difficulty": "Easy",
98829882
"IsAccepted": true,
98839883
"IsPaid": false,
@@ -10069,7 +10069,7 @@
1006910069
"ID": 837,
1007010070
"Title": "New 21 Game",
1007110071
"TitleSlug": "new-21-game",
10072-
"PassRate": "20%",
10072+
"PassRate": "21%",
1007310073
"Difficulty": "Medium",
1007410074
"IsAccepted": false,
1007510075
"IsPaid": false,
@@ -10081,7 +10081,7 @@
1008110081
"ID": 838,
1008210082
"Title": "Push Dominoes",
1008310083
"TitleSlug": "push-dominoes",
10084-
"PassRate": "36%",
10084+
"PassRate": "37%",
1008510085
"Difficulty": "Medium",
1008610086
"IsAccepted": false,
1008710087
"IsPaid": false,

0 commit comments

Comments
 (0)