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:

Example 2:

Example 3:

Notes:

分析

方法1 年级120内,map存每个age的个数。然后2个for遍历所有数字,相乘(注意= 是*cnt(b)-1)

方法2 用array和和数学公式。a,b 之间范围的和相减,[a, b = a/2 +7]

Last updated

Was this helpful?