每日一题 leetcode 825. 适龄的朋友 java题解

这题目就sb

题目

https://leetcode-cn.com/problems/friends-of-appropriate-ages/

代码

class Solution {
    public int numFriendRequests(int[] ages) {
        int[] count=new int[121];//count[i]年龄的个数
        for(int age:ages)
            count[age]++;
        int[] pre=new int[121];
        for(int i=1;i<=120;i++)
            pre[i]=pre[i-1]+count[i];
        int res=0;
        for(int i=15;i<=120;i++){
            int low=(int)(i*0.5+8);
            res+=count[i]*(pre[i]-pre[low-1]-1);
        }
        return res;
    }
}

你可能感兴趣的:(LeetCode,java,leetcode,算法)