zoukankan      html  css  js  c++  java
  • 825. Friends Of Appropriate Ages

    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.

    Solution1:(TLE)

    class Solution:
        def numFriendRequests(self, ages):
            """
            :type ages: List[int]
            :rtype: int
            """
            def friend(a,b):
                return not (b<=0.5*a+7 or b>a or (b>100 and a<100))
            res = 0
            for i in range(len(ages)):
                for j in range(i+1,len(ages)):
                    if friend(ages[i],ages[j]):
                        res += 1
                    if friend(ages[j],ages[i]):
                        res += 1
            return res
    

    73 / 83 test cases passed.

    Solution2:(TLE)

    class Solution:
        def numFriendRequests(self, ages):
            """
            :type ages: List[int]
            :rtype: int
            """
            def friend(a,b):
                return not (b<=0.5*a+7)
            ages.sort()
            res = 0
            dic = {}
            for i in range(len(ages)):
                if ages[i] not in dic:
                    temp = 0
                    for j in range(i+1,len(ages)):
                        if friend(ages[j],ages[i]):
                            temp += 1
                    dic[ages[i]] = temp
                    res += temp
                else:
                    res += dic[ages[i]]
            return res
    

    78 / 83 test cases passed.

    Solution3:
    注意到age是有范围的

    class Solution:
        def numFriendRequests(self, ages):
            """
            :type ages: List[int]
            :rtype: int
            """
            count = [0 for i in range(121)]
            for i in ages:
                count[i] += 1
            res = 0
            for i in range(1,121):
                if count[i]==0:
                    continue
                temp = i//2 + 8
                res += count[i] * sum(count[temp:i])
                if i>14:
                    res += count[i]*(count[i]-1)
            return res
    
  • 相关阅读:
    300+值得收藏的设计师免费资源站
    Apache 隐藏入口文件 index.php
    Nginx 虚拟主机下支持Pathinfo并隐藏入口文件的完整配置
    Java多线程
    Java注解
    Java异常机制
    面向对象
    数组
    Java方法(函数)
    Java流程控制(Scanner)
  • 原文地址:https://www.cnblogs.com/bernieloveslife/p/10235656.html
Copyright © 2011-2022 走看看