★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-oxmxedbo-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Some people will make friend requests. The list of their ages is given and ages[i]
is the age of the ith person. git
Person A will NOT friend request person B (B != A) if any of the following conditions are true:github
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?spa
Example 1:code
Input: [16,16] Output: 2 Explanation: 2 people friend request each other.
Example 2:htm
Input: [16,17,18] Output: 2 Explanation: Friend requests are made 17 -> 16, 18 -> 17.
Example 3:blog
Input: [20,30,100,110,120] Output: Explanation: Friend requests are made 110 -> 100, 120 -> 110, 120 -> 100.
Notes:get
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
.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 }