LeetCode 597: Friend Requests I: Overall Acceptance Rate Solution

Master LeetCode problem 597 (Friend Requests I: Overall Acceptance Rate), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

597. Friend Requests I: Overall Acceptance Rate

Problem Explanation

Explanation

To solve this problem, we need to calculate the overall acceptance rate for friend requests. The acceptance rate for a person is defined as the number of accepted friend requests divided by the total number of sent friend requests. The overall acceptance rate is the average acceptance rate of all people.

We will iterate through the input arrays to calculate the acceptance rate for each person and then compute the average acceptance rate.

Algorithm:

  1. Initialize variables totalAccepted and totalRequests to store the total number of accepted friend requests and total number of sent friend requests respectively.
  2. Iterate through the input arrays:
    • For each person, calculate the acceptance rate by dividing the number of accepted requests by the number of sent requests.
    • Add the number of accepted requests to totalAccepted and the number of sent requests to totalRequests.
  3. Calculate the overall acceptance rate by dividing totalAccepted by totalRequests.

Time Complexity: O(n) where n is the total number of elements in the input arrays. Space Complexity: O(1)

Solution Code

class Solution {
    public double overallAcceptanceRate(int[] ids, int[] senders, int[] accepters) {
        int totalAccepted = 0;
        int totalRequests = 0;
        
        for (int i = 0; i < ids.length; i++) {
            if (senders[i] != accepters[i]) {
                totalRequests++;
            }
            if (ids[i] == accepters[i]) {
                totalAccepted++;
            }
        }
        
        return totalAccepted / (double) totalRequests;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 597 (Friend Requests I: Overall Acceptance Rate)?

This page provides optimized solutions for LeetCode problem 597 (Friend Requests I: Overall Acceptance Rate) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 597 (Friend Requests I: Overall Acceptance Rate)?

The time complexity for LeetCode 597 (Friend Requests I: Overall Acceptance Rate) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 597 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 597 in Java, C++, or Python.

Back to LeetCode Solutions