MediumNo. 1699LeetCode Premium
Number of Calls Between Two Persons — LeetCode 1699 Python Solution
- Topics
- Database
- Reading time
- 3 min
- Source
- leetcode.com
Table schema
Table: Calls +-------------+---------+ | Column Name | Type | +-------------+---------+ | from_id | int | | to_id | int | | duration | int | +-------------+---------+ This table does not have a primary key (column with unique values), it may contain duplicates. This table contains the duration of a phone call between from_id and to_id.Example
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| from_id | int |
| to_id | int |
| duration | int |
+-------------+---------+
This table does not have a primary key (column with unique values), it may contain duplicates.
This table contains the duration of a phone call between from_id and to_id.
from_id != to_idPython solution
import duckdb
import pandas as pd
def solution(calls: pd.DataFrame) -> pd.DataFrame:
con = duckdb.connect()
con.register("Calls", calls)
return con.execute("""SELECT
IF(from_id < to_id, from_id, to_id) AS person1,
IF(from_id < to_id, to_id, from_id) AS person2,
COUNT(1) AS call_count,
SUM(duration) AS total_duration
FROM Calls
GROUP BY 1, 2;""").df()Complexity
- Time
- O(n log n)(typical)
- Space · auxiliary
- O(n)
Related problems
| No. | Problem | Difficulty |
|---|---|---|
| 0175 | Combine Two Tables | Easy |
| 0176 | Second Highest Salary | Medium |
| 0177 | Nth Highest Salary | Medium |
| 0178 | Rank Scores | Medium |
| 0180 | Consecutive Numbers | Medium |
| 0181 | Employees Earning More Than Their Managers | Easy |
Frequently asked questions
- How hard is LeetCode 1699. Number of Calls Between Two Persons?
LeetCode 1699. Number of Calls Between Two Persons is rated Medium on LeetCode.
- What topics does LeetCode 1699. Number of Calls Between Two Persons cover?
LeetCode 1699. Number of Calls Between Two Persons is tagged Database on LeetCode.
- Is LeetCode 1699. Number of Calls Between Two Persons a premium problem?
Yes. LeetCode 1699. Number of Calls Between Two Persons is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.