Skip to content

Commit 4a94b85

Browse files
author
Partho Biswas
committed
825_Friends_Of_Appropriate_Ages
1 parent b4bd5f1 commit 4a94b85

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ I have solved quite a number of problems from several topics. See the below tabl
189189
|75| **[311. Sparse Matrix Multiplication](https://tinyurl.com/y9lapcjx)** | [Python](https://tinyurl.com/wu6rdaw/311_Sparse_Matrix_Multiplication.py), [Swift](https://tinyurl.com/wuja3c4/311_Sparse_Matrix_Multiplication.swift) | [Art 1](https://tinyurl.com/ycv24vc4), [Art 2](https://tinyurl.com/ycmqcfw9), [Art 3](https://tinyurl.com/y9az7nef), **[Art 4](https://tinyurl.com/y84lwkya)** | Medium | Very tricky |
190190
|76| **[896. Monotonic Array](https://tinyurl.com/y8a95fb6)** | [Python](https://tinyurl.com/wu6rdaw/896_Monotonic_Array.py), [Swift](https://tinyurl.com/wuja3c4/896_Monotonic_Array.swift) | --- | Eassy | |
191191
|77| **[670. Maximum Swap](https://tinyurl.com/y2zhdd33)** | [Python](https://tinyurl.com/wu6rdaw/670_Maximum_Swap.py), [Swift](https://tinyurl.com/wuja3c4/670_Maximum_Swap.swift) | [Art 1](https://tinyurl.com/y8vqklj3) | Medium | |
192+
|78| **[825. Friends Of Appropriate Ages](https://tinyurl.com/ycgnqxb8)** | [Python](https://tinyurl.com/wu6rdaw/825_Friends_Of_Appropriate_Ages.py), [Swift](https://tinyurl.com/wuja3c4/825_Friends_Of_Appropriate_Ages.swift) | [Art 1](https://tinyurl.com/yd4oal3l) | Medium | Think differently from a different angle. Loved this problem |
192193

193194

194195
</p>
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
import Foundation
21

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
2+
// Brute force approach. Time: O(n^2)
3+
// Time Limit Exceeded
4+
import Foundation
5+
class Solution {
6+
func numFriendRequests(_ ages: [Int]) -> Int {
7+
let contertedAges = ages.map {Double($0)}
8+
var existingRequestSet = Set<String>()
9+
for A in 0..<contertedAges.count {
10+
for B in 0..<contertedAges.count {
11+
if A == B {
12+
continue
13+
}
14+
if contertedAges[B] <= (0.5 * contertedAges[A] + 7) {
15+
continue
16+
}
17+
if contertedAges[B] > contertedAges[A] {
18+
continue
19+
}
20+
if contertedAges[B] > 100 && contertedAges[A] < 100 {
21+
continue
22+
}
23+
let requestKey = "\(A)-\(B)"
24+
if !existingRequestSet.contains(requestKey) {
25+
existingRequestSet.insert(requestKey)
26+
}
27+
}
28+
}
29+
return existingRequestSet.count
30+
}
31+
}
32+
33+
34+
// Brute force approach. Time: O(1)
35+
import Foundation
36+
class Solution {
37+
func numFriendRequests(_ ages: [Int]) -> Int {
38+
let convertedAges = ages.map {Double($0)}
39+
var ageCount = [Double: Int]()
40+
for (index, item) in convertedAges.enumerated() {
41+
ageCount[item, default: 0] += 1
42+
}
43+
44+
var requestSent = 0
45+
for (ageA, countA) in ageCount {
46+
for (ageB, countB) in ageCount {
47+
if ageB <= (0.5 * ageA + 7) {
48+
continue
49+
}
50+
if ageB > ageA {
51+
continue
52+
}
53+
if ageB > 100 && ageA < 100 {
54+
continue
55+
}
56+
if ageA == ageB {
57+
requestSent -= countA
58+
}
59+
requestSent += (countA * countB)
60+
}
61+
}
62+
return requestSent
63+
}
64+
}

0 commit comments

Comments
 (0)