Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 57e97a3

Browse files
Update readme.md
1 parent a8cb430 commit 57e97a3

File tree

1 file changed

+128
-0
lines changed
  • LeetCode SQL 50 Solution/2356. Number of Unique Subjects Taught by Each Teacher

1 file changed

+128
-0
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
Below is a well-structured `README.md` for **LeetCode 2356 - Number of Unique Subjects Taught by Each Teacher**, which includes both the SQL solution and a Python (Pandas) solution.
2+
3+
```md
4+
# 📚 Number of Unique Subjects Taught by Each Teacher - LeetCode 2356
5+
6+
## 📌 Problem Statement
7+
You are given a table **Teacher** that provides information about the subjects taught by teachers in various departments of a university.
8+
9+
Your task is to calculate the **number of unique subjects** each teacher teaches.
10+
Note that the table can have multiple rows for the same subject taught in different departments, but you should count each subject only once per teacher.
11+
12+
Return the result table in **any order**.
13+
14+
---
15+
16+
## 📊 Table Structure
17+
18+
### **Teacher Table**
19+
| Column Name | Type |
20+
| ----------- | ---- |
21+
| teacher_id | int |
22+
| subject_id | int |
23+
| dept_id | int |
24+
25+
- `(subject_id, dept_id)` is the **primary key**.
26+
- Each row indicates that the teacher with `teacher_id` teaches the subject `subject_id` in the department `dept_id`.
27+
28+
---
29+
30+
## 📊 Example 1:
31+
32+
### **Input:**
33+
#### **Teacher Table**
34+
| teacher_id | subject_id | dept_id |
35+
| ---------- | ---------- | ------- |
36+
| 1 | 2 | 3 |
37+
| 1 | 2 | 4 |
38+
| 1 | 3 | 3 |
39+
| 2 | 1 | 1 |
40+
| 2 | 2 | 1 |
41+
| 2 | 3 | 1 |
42+
| 2 | 4 | 1 |
43+
44+
### **Output:**
45+
| teacher_id | cnt |
46+
| ---------- | --- |
47+
| 1 | 2 |
48+
| 2 | 4 |
49+
50+
### **Explanation:**
51+
- **Teacher 1:**
52+
- Teaches subject **2** (in departments 3 and 4) and subject **3** (in department 3).
53+
- Unique subjects = {2, 3} → **2 subjects**.
54+
- **Teacher 2:**
55+
- Teaches subjects **1**, **2**, **3**, and **4** (all in department 1).
56+
- Unique subjects = {1, 2, 3, 4} → **4 subjects**.
57+
58+
---
59+
60+
## 🖥 SQL Solution
61+
62+
### ✅ **Approach:**
63+
- Use `COUNT(DISTINCT subject_id)` to count the number of unique subjects taught by each teacher.
64+
- Group the results by `teacher_id`.
65+
66+
```sql
67+
SELECT teacher_id, COUNT(DISTINCT subject_id) AS cnt
68+
FROM Teacher
69+
GROUP BY teacher_id;
70+
```
71+
72+
---
73+
74+
## 🐍 Python (Pandas) Solution
75+
76+
### **Approach:**
77+
1. **Group by `teacher_id`:**
78+
- Group the DataFrame by `teacher_id`.
79+
2. **Count Unique Subjects:**
80+
- Use the `nunique()` function on the `subject_id` column within each group to count unique subjects.
81+
3. **Reset Index and Rename:**
82+
- Reset the index and rename the column appropriately.
83+
84+
```python
85+
import pandas as pd
86+
87+
def count_unique_subjects(teacher: pd.DataFrame) -> pd.DataFrame:
88+
# Group by teacher_id and count unique subject_id values
89+
result = teacher.groupby('teacher_id')['subject_id'].nunique().reset_index()
90+
result = result.rename(columns={'subject_id': 'cnt'})
91+
return result
92+
93+
# Example usage:
94+
# teacher_df = pd.DataFrame({
95+
# 'teacher_id': [1, 1, 1, 2, 2, 2, 2],
96+
# 'subject_id': [2, 2, 3, 1, 2, 3, 4],
97+
# 'dept_id': [3, 4, 3, 1, 1, 1, 1]
98+
# })
99+
# print(count_unique_subjects(teacher_df))
100+
```
101+
102+
---
103+
104+
## 📁 File Structure
105+
```
106+
📂 Unique-Subjects-Per-Teacher
107+
│── README.md
108+
│── solution.sql
109+
│── solution_pandas.py
110+
│── test_cases.sql
111+
│── sample_data.csv
112+
```
113+
114+
---
115+
116+
## 🔗 Useful Links
117+
- 📖 [LeetCode Problem](https://leetcode.com/problems/number-of-unique-subjects-taught-by-each-teacher/)
118+
- 🔍 [MySQL COUNT(DISTINCT) Documentation](https://www.w3schools.com/sql/sql_count_distinct.asp)
119+
- 🐍 [Pandas GroupBy Documentation](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.groupby.html)
120+
```
121+
122+
This `README.md` provides:
123+
- A **clear problem statement** along with the table structure.
124+
- A **detailed example** with explanation.
125+
- Both **SQL and Python (Pandas) solutions** with step-by-step approaches.
126+
- An **organized file structure** and helpful external links.
127+
128+
Let me know if you need any modifications!

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /