[Swift]LeetCode825. 适龄的朋友 | Friends Of Appropriate Ages
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
?微信公眾號:山青詠芝(shanqingyongzhi)
?博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
?GitHub地址:https://github.com/strengthen/LeetCode
?原文地址:?https://www.cnblogs.com/strengthen/p/10569342.html?
?如果鏈接不是山青詠芝的博客園地址,則可能是爬取作者的文章。
?原文已修改更新!強烈建議點擊原文地址閱讀!支持作者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
Some people will make friend requests. The?list of their ages is given and?ages[i]?is the age of the?ith person.?
Person A will NOT friend request person B (B != A) if any of the following conditions are true:
- age[B]?<= 0.5 * age[A]?+ 7
- age[B]?> age[A]
- age[B]?> 100 &&?age[A]?< 100
Otherwise, A will friend request B.
Note that if?A requests B, B does not necessarily request A.? Also, people will not friend request themselves.
How many total friend requests are made?
Example 1:
Input: [16,16] Output: 2 Explanation: 2 people friend request each other.Example 2:
Input: [16,17,18] Output: 2 Explanation: Friend requests are made 17 -> 16, 18 -> 17.Example 3:
Input: [20,30,100,110,120] Output: Explanation: Friend requests are made 110 -> 100, 120 -> 110, 120 -> 100.?Notes:
- 1 <= ages.length?<= 20000.
- 1 <= ages[i] <= 120.
人們會互相發送好友請求,現在給定一個包含有他們年齡的數組,ages[i]?表示第 i 個人的年齡。
當滿足以下條件時,A 不能給 B(A、B不為同一人)發送好友請求:
- age[B]?<= 0.5 * age[A]?+ 7
- age[B]?> age[A]
- age[B]?> 100 &&?age[A]?< 100
否則,A 可以給 B 發送好友請求。
注意如果 A 向 B 發出了請求,不等于 B 接受了 A 的請求。而且,人們不會給自己發送好友請求。?
求總共會發出多少份好友請求?
示例 1:
輸入: [16,16] 輸出: 2 解釋: 二人可以互發好友申請。示例 2:
輸入: [16,17,18] 輸出: 2 解釋: 好友請求可產生于 17 -> 16, 18 -> 17.示例 3:
輸入: [20,30,100,110,120] 輸出: 3 解釋: 好友請求可產生于 110 -> 100, 120 -> 110, 120 -> 100.說明:
- 1 <= ages.length?<= 20000.
- 1 <= ages[i] <= 120.
Runtime:?240 ms Memory Usage:?19.4 MB 1 class Solution { 2 func numFriendRequests(_ ages: [Int]) -> Int { 3 var res:Int = 0 4 var numInAge:[Int] = [Int](repeating:0,count:121) 5 var sumInAge:[Int] = [Int](repeating:0,count:121) 6 for age in ages 7 { 8 numInAge[age] += 1 9 } 10 for i in 1...120 11 { 12 sumInAge[i] = numInAge[i] + sumInAge[i - 1] 13 } 14 for i in 15...120 15 { 16 if numInAge[i] == 0 {continue} 17 var cnt:Int = sumInAge[i] - sumInAge[Int(Double(i) * 0.5 + 7)] 18 res += cnt * numInAge[i] - numInAge[i] 19 } 20 return res 21 } 22 }
268ms
1 class Solution { 2 func numFriendRequests(_ ages: [Int]) -> Int { 3 var count = [Int](repeating: 0, count: 121) 4 for age in ages { count[age] += 1 } 5 6 var ans = 0 7 for ageA in 0...120 { 8 let countA = count[ageA] 9 for ageB in 0...120 { 10 let countB = count[ageB] 11 if (ageA + 14) >= 2*ageB { continue } 12 if ageA < ageB { continue } 13 if ageA < 100 && 100 < ageB { continue } 14 ans += countA * countB 15 if ageA == ageB { ans -= countA} 16 } 17 } 18 return ans 19 } 20 }292ms
1 class Solution { 2 func numFriendRequests(_ ages: [Int]) -> Int { 3 var map = [Int: Int]() 4 for age in ages { 5 map[age] = (map[age] ?? 0) + 1 6 } 7 var result = 0 8 for i in 0...120 { 9 if let iCount = map[i] { 10 for j in 0...120 { 11 if let jCount = map[j] { 12 if j <= i / 2 + 7 || j > i || (j > 100 && i < 100) { 13 continue 14 } 15 if i == j { 16 result += (iCount - 1) * iCount 17 } else { 18 result += iCount * jCount 19 } 20 } 21 } 22 } 23 } 24 return result 25 } 26 }404ms
1 class Solution { 2 func numFriendRequests(_ ages: [Int]) -> Int { 3 var ageCounts = [Int](repeating: 0, count: 121) 4 for age in ages { 5 ageCounts[age] += 1 6 } 7 var res = 0 8 for i in 0..<ageCounts.count { 9 var ageA = i 10 for j in 0..<ageCounts.count { 11 var ageB = j 12 if ageB <= Int(floor(0.5 * Double(ageA) + 7)) { continue } 13 if ageB > ageA { continue } 14 res += ageCounts[i] * ageCounts[j] 15 if ageA == ageB { 16 res -= ageCounts[i] 17 } 18 } 19 } 20 return res 21 } 22 }?
轉載于:https://www.cnblogs.com/strengthen/p/10569342.html
總結
以上是生活随笔為你收集整理的[Swift]LeetCode825. 适龄的朋友 | Friends Of Appropriate Ages的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Web送货单打印管理系统毕业设计
- 下一篇: OSChina 周四乱弹 ——小小编辑教