MediumNo. 1990LeetCode Premium
Count the Number of Experiments — LeetCode 1990 Python Solution
- Topics
- Database
- Reading time
- 6 min
- Source
- leetcode.com
Table schema
Table: Experiments +-----------------+------+ | Column Name | Type | +-----------------+------+ | experiment_id | int | | platform | enum | | experiment_name | enum | +-----------------+------+ experiment_id is the column with unique values for this table. platform is an enum (category) type of values ('Android', 'IOS', 'Web').Example
+-----------------+------+
| Column Name | Type |
+-----------------+------+
| experiment_id | int |
| platform | enum |
| experiment_name | enum |
+-----------------+------+
experiment_id is the column with unique values for this table.
platform is an enum (category) type of values ('Android', 'IOS', 'Web').
experiment_name is an enum (category) type of values ('Reading', 'Sports', 'Programming').
This table contains information about the ID of an experiment done with a random person, the platform used to do the experiment, and the name of the experiment.Python solution
import duckdb
import pandas as pd
def solution(experiments: pd.DataFrame) -> pd.DataFrame:
con = duckdb.connect()
con.register("Experiments", experiments)
return con.execute("""WITH
P AS (
SELECT 'Android' AS platform
UNION
SELECT 'IOS'
UNION
SELECT 'Web'
),
Exp AS (
SELECT 'Reading' AS experiment_name
UNION
SELECT 'Sports'
UNION
SELECT 'Programming'
),
T AS (
SELECT *
FROM
P,
Exp
)
SELECT platform, experiment_name, COUNT(experiment_id) AS num_experiments
FROM
T AS t
LEFT JOIN Experiments USING (platform, experiment_name)
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 1990. Count the Number of Experiments?
LeetCode 1990. Count the Number of Experiments is rated Medium on LeetCode.
- What topics does LeetCode 1990. Count the Number of Experiments cover?
LeetCode 1990. Count the Number of Experiments is tagged Database on LeetCode.
- Is LeetCode 1990. Count the Number of Experiments a premium problem?
Yes. LeetCode 1990. Count the Number of Experiments is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.