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.
person1
to person2
is considered the same as a call from person2
to person1
.
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.
Let's break down the steps for solving this problem:
person1
and person2
(in any order), count this call.
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.
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
.
The final answer is 3, since there are three calls between person 1 and person 2, regardless of direction.
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.
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