LeetCode 825 适龄的朋友
LeetCode 825 适龄的朋友
看完条件一晃眼...好像叫德摩根定律? (似乎是离散数学里的)
反正展开就对了
为了表达方便修改了运算符
!( b<=0.5*a+7 || b>a || (b>100 && a<100))
= !(b<=0.5*a+7) && !(b>a) || !(b>100 && a<100)
= b>0.5*a+7 && b<=a && (b<=100 || a>=100)
= b>0.5*a+7 && b<=a
故
b ∈ (14, +∞)
a ∈ [b, 2b-14)
此时直接套二分搜索就差不多了。至于还能用计数优化什么的就不搞了(懒
Python3
from typing import List
import bisect
class Solution:
def numFriendRequests(self, ages: List[int]) -> int:
ages = sorted([x for x in filter(lambda x: x>14, ages)])
opt = 0
lastV, lastS = -1, -1
for i, v in enumerate(ages):
if v == lastV:
opt += lastS
continue
if i+1 == len(ages):
continue
lastV, lastS = v, 0
ed = bisect.bisect_left(ages, 2*v - 14, i)
if ed:
lastS = ed - i - 1
opt += lastS
return opt