LeetCode - 825 - school-age friends - Java - in the author's opinion, it's fine

Posted by niranjnn01 on Tue, 28 Dec 2021 07:25:44 +0100

subject

Topic analysis

Problem solving thinking 1:

Sorting the array ages (in ascending order) is actually sorting these people according to their age from high to low.
At this time, use two "left and right pointers" to lock x's dating range.
After confirmation, right - left is the number of people who meet the conditions for making friends with x, that is, x in the title will send a request to y.

code

class Solution {
    public int numFriendRequests(int[] ages) {
        Arrays.sort(ages);
        int n  = ages.length;
        int left = 0,right = 0,result=0;
        for(int age : ages){
            if( age < 15){
            // Regardless of those under the age of 15, this is the problem hiding condition. If you don't believe in evil, add equal to or delete this code block
                continue;// The following program is not executed, and the foreach loop continues to read data.
            }
            while(ages[left] <= 0.5*age +7){// Too small, inappropriate, left + +, narrow range
                left++;
            }
            while(right+1 < n && ages[right+1] <= age){// Appropriate age
                right++;
            }
            result += right -left;// Cumulative friend requests from everyone 
        }
        return result;// Returns the number of messages that all people send friend requests.
    }
}

Attached drawings

Problem solving thinking 2:

By creating an array cur with a capacity of 121, the array subscript is equivalent to the age, and the element size corresponding to the subscript is the number of people at that age.

Then create an array prev of the same size. Each element is equal to its previous element plus the element corresponding to the subscript of the element in cur.

Some friends may not understand this code. In short, it is cumulative assignment: the sum of people in age i and those younger than age i. Give the i subscript element of array prev and update its value to the sum of people in age group i and all people in age group younger than i.


The next step is to traverse the cur array. If there are people at the age of I, that is, cur [i] > 0. At this time, like the first method, it is necessary to calculate the value of 0.5 * i + 7, that is, the bound ary of the minimum age that can not be accepted by people at age I.
And please pay attention! Each element of our prev is the sum of the number of people corresponding to cur at a certain age and below.
At this time, prev[[i] corresponding to cur[i] is the sum of the number of people at this age and below, minus the calculated value of 0.5 * i + 7, that is, the bound ary of the minimum age that can not be accepted by people at age I.
The result of prev [i] - prev [bound]: that is, the sum of the number of people in [0.5 * i + 7 < age [y] < = I], that is, the subtraction result is larger than the result of 0.5 * i + 7, that is, all people are friends of people of I age.
Except for yourself, of course. (don't ask anything! The question is required)
Then, it is to add up the number of application information of people of each i age.
Finally, the result of this accumulation is returned.

Attach the code of method 2

class Solution {
    public int numFriendRequests(int[] ages) {
       // Count the number of people of each age and make it an array element
        int[] cur = new int[121];
        for(int age : ages){
            cur[age]++;
        }
        //Count the total number of people of each age and below and make it an array element
        int[] prev = new int[121];
        for(int i = 1; i < 121;i++){
            prev[i] = prev[i-1] + cur[i];
        }
        
        int result = 0;// Record final results
        // Don't consider under the age of 15. As the title says, traverse the array cur from subscript 15
        for(int i = 15; i < 121; i++){
            if(cur[i]>0){// If there are people of i age
                int bound = (int)(0.5*i +7);// Calculate the minimum age limit they can accept

                result += cur[i] * (prev[i] - prev[bound]-1);
                // According to the requirements of the topic, the minimum age that people of i age can accept is at least greater than the bound ary.
                // Subtract the total number of bound people from the total number of people at and below the current i age
                // The rest are people older than bound, and the maximum age is no more than i
                // Then, according to the requirements of the topic, subtract yourself, and the rest are friends.
                // Of course, there may be more than one person at the age of I, so it has to be multiplied by a cur[i].
                // That is, everyone at i age can apply for friends from people other than himself.
                // last. Add up the number of pieces of information applied for each i age.
            }
        }
        return result;// Finally return it.
    }
}

Topics: Java data structure leetcode