欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

LeetCode 825 friends-of-appropriate-ages

发布时间:2023/12/20 编程问答 47 豆豆
生活随笔 收集整理的这篇文章主要介绍了 LeetCode 825 friends-of-appropriate-ages 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

LeetCode 825

仔细分析一下条件:
age[B] <= 0.5 * age[A] + 7
age[B] > age[A]
age[B] > 100 && age[A] < 100
第三个条件其实是无效的,因为第二个已经涵盖了,组合一下条件,其实就是要求 0.5*age【A】+7 < age【B】<agent[A]

那么就可以转换为查找这个区间,
如果有重复的age,我们只要算一次,然后把结果重复加上。

def numFriendRequests(self, ages: List[int]) -> int:if ages == None: raise Exception("invalid input: ages")ages.sort()count = 0index = len(ages) -1while index >= 0:age = ages[index]index -=1ageb = age * 0.5 + 7index2 = bisect.bisect_right(ages, ageb)if index >= index2:#get friend request countsscount= index-index2 + 1count += scount #same age, just need add the counts again, no need recaculatewhile index >= 0 and ages[index] == age:count += scountindex -=1return count

总结

以上是生活随笔为你收集整理的LeetCode 825 friends-of-appropriate-ages的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。