Want Help Cracking FAANG?

(Then click this)

×
Back to Question Bank

1699. Number of Calls Between Two Persons - Leetcode Solution

Problem Description

The "Number of Calls Between Two Persons" problem provides you with a list of phone call records, where each record contains information about a call between two people. Each record typically includes the caller's ID, the callee's ID, and possibly other details such as the timestamp or duration. The primary goal is: Given a list of such call records and two specific person IDs (person1 and person2), determine the total number of calls that occurred between these two persons, regardless of who was the caller and who was the callee for each call.

  • Each call is represented as a pair or object indicating the two participants.
  • The order of caller and callee does not matter: a call from person1 to person2 is considered the same as a call from person2 to person1.
  • You need to count all such calls in the records.
  • Constraints may ensure IDs are integers, and the list of calls can be large.

Thought Process

To solve this problem, the first idea that comes to mind is to iterate through all call records and check if each record involves both person1 and person2. Since the order does not matter, we need to check both possible directions (i.e., caller == person1 and callee == person2, or vice versa).

A brute-force solution would simply scan every record and increment a counter whenever such a match is found. Given the simplicity of the matching condition and the lack of further constraints (like avoiding duplicates or time windows), this approach is both correct and straightforward.

If the dataset were extremely large, we might consider indexing or pre-processing, but for most practical inputs, a single pass suffices. The main optimization is to avoid unnecessary checks and ensure that the comparison is symmetric.

Solution Approach

Let's break down the steps for solving this problem:

  1. Iterate through the call records: For each call record, extract the two involved person IDs.
  2. Check for a match: If the two IDs in the record are exactly person1 and person2 (in any order), count this call.
  3. Count the matches: Maintain a counter to keep track of the number of matches found.
  4. Return the result: After processing all records, return the final count.

We use a simple loop because each check is O(1), and there are no requirements to avoid double-counting or deduplication unless the input specifically allows repeated calls between the same two people.

Example Walkthrough

Suppose the call records are represented as a list of pairs: calls = [[1,2], [2,1], [1,3], [2,3], [1,2]], and we are asked for the number of calls between person1 = 1 and person2 = 2.

  • First record: [1,2] → matches (1 and 2) → count = 1
  • Second record: [2,1] → matches (2 and 1) → count = 2
  • Third record: [1,3] → does not match → count = 2
  • Fourth record: [2,3] → does not match → count = 2
  • Fifth record: [1,2] → matches → count = 3

The final answer is 3, since there are three calls between person 1 and person 2, regardless of direction.

Time and Space Complexity

  • Brute-force approach:
    • Time Complexity: O(N), where N is the number of call records. Each record is checked once.
    • Space Complexity: O(1), as we only maintain a counter and do not use extra data structures.
  • Optimized approach:
    • Since the brute-force approach is already O(N) and uses minimal space, there is little room for further optimization unless the data structure changes (e.g., if we need to answer multiple queries efficiently).

Summary

The key to solving the "Number of Calls Between Two Persons" problem is recognizing that each call record should be checked for the presence of both target persons, regardless of order. By iterating through the records and counting matches, we achieve an efficient and direct solution. The approach is both simple and optimal for single-query scenarios, making it easy to implement and understand.

Code Implementation

def numberOfCallsBetweenTwoPersons(calls, person1, person2):
    count = 0
    for call in calls:
        # call[0] and call[1] are the two persons in the call
        if (call[0] == person1 and call[1] == person2) or (call[0] == person2 and call[1] == person1):
            count += 1
    return count

# Example usage:
# calls = [[1,2], [2,1], [1,3], [2,3], [1,2]]
# print(numberOfCallsBetweenTwoPersons(calls, 1, 2))  # Output: 3
      
#include <vector>
using namespace std;

int numberOfCallsBetweenTwoPersons(vector<vector<int>>& calls, int person1, int person2) {
    int count = 0;
    for (const auto& call : calls) {
        if ((call[0] == person1 && call[1] == person2) ||
            (call[0] == person2 && call[1] == person1)) {
            count++;
        }
    }
    return count;
}

// Example usage:
// vector<vector<int>> calls = {{1,2}, {2,1}, {1,3}, {2,3}, {1,2}};
// cout << numberOfCallsBetweenTwoPersons(calls, 1, 2); // Output: 3
      
import java.util.List;

public class Solution {
    public int numberOfCallsBetweenTwoPersons(List<int[]> calls, int person1, int person2) {
        int count = 0;
        for (int[] call : calls) {
            if ((call[0] == person1 && call[1] == person2) ||
                (call[0] == person2 && call[1] == person1)) {
                count++;
            }
        }
        return count;
    }

    // Example usage:
    // List<int[]> calls = Arrays.asList(new int[]{1,2}, new int[]{2,1}, new int[]{1,3}, new int[]{2,3}, new int[]{1,2});
    // System.out.println(new Solution().numberOfCallsBetweenTwoPersons(calls, 1, 2)); // Output: 3
}
      
function numberOfCallsBetweenTwoPersons(calls, person1, person2) {
    let count = 0;
    for (const call of calls) {
        if ((call[0] === person1 && call[1] === person2) ||
            (call[0] === person2 && call[1] === person1)) {
            count++;
        }
    }
    return count;
}

// Example usage:
// const calls = [[1,2], [2,1], [1,3], [2,3], [1,2]];
// console.log(numberOfCallsBetweenTwoPersons(calls, 1, 2)); // Output: 3