Skip to content

Commit febb5f7

Browse files
committed
feat: add solutions to lc problems: No.2357~2360
* No.2357.Make Array Zero by Subtracting Equal Amounts * No.2358.Maximum Number of Groups Entering a Competition * No.2359.Find Closest Node to Given Two Nodes * No.2360.Longest Cycle in a Graph
1 parent d4f5e82 commit febb5f7

File tree

34 files changed

+1795
-8
lines changed

34 files changed

+1795
-8
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# [2356. Number of Unique Subjects Taught by Each Teacher](https://leetcode.cn/problems/number-of-unique-subjects-taught-by-each-teacher)
2+
3+
[English Version](/solution/2300-2399/2356.Number%20of%20Unique%20Subjects%20Taught%20by%20Each%20Teacher/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>Table: <code>Teacher</code></p>
10+
11+
<pre>
12+
+-------------+------+
13+
| Column Name | Type |
14+
+-------------+------+
15+
| teacher_id | int |
16+
| subject_id | int |
17+
| dept_id | int |
18+
+-------------+------+
19+
(subject_id, dept_id) is the primary key for this table.
20+
Each row in this table indicates that the teacher with teacher_id teaches the subject subject_id in the department dept_id.
21+
</pre>
22+
23+
<p>&nbsp;</p>
24+
25+
<p>Write an SQL query to report the number of unique subjects each teacher teaches in the university.</p>
26+
27+
<p>Return the result table in <strong>any order</strong>.</p>
28+
29+
<p>The query result format is shown in the following example.</p>
30+
31+
<p>&nbsp;</p>
32+
<p><strong>Example 1:</strong></p>
33+
34+
<pre>
35+
<strong>Input:</strong>
36+
Teacher table:
37+
+------------+------------+---------+
38+
| teacher_id | subject_id | dept_id |
39+
+------------+------------+---------+
40+
| 1 | 2 | 3 |
41+
| 1 | 2 | 4 |
42+
| 1 | 3 | 3 |
43+
| 2 | 1 | 1 |
44+
| 2 | 2 | 1 |
45+
| 2 | 3 | 1 |
46+
| 2 | 4 | 1 |
47+
+------------+------------+---------+
48+
<strong>Output:</strong>
49+
+------------+-----+
50+
| teacher_id | cnt |
51+
+------------+-----+
52+
| 1 | 2 |
53+
| 2 | 4 |
54+
+------------+-----+
55+
<strong>Explanation:</strong>
56+
Teacher 1:
57+
- They teach subject 2 in departments 3 and 4.
58+
- They teach subject 3 in department 3.
59+
Teacher 2:
60+
- They teach subject 1 in department 1.
61+
- They teach subject 2 in department 1.
62+
- They teach subject 3 in department 1.
63+
- They teach subject 4 in department 1.
64+
</pre>
65+
66+
67+
## 解法
68+
69+
<!-- 这里可写通用的实现逻辑 -->
70+
71+
<!-- tabs:start -->
72+
73+
### **SQL**
74+
75+
<!-- 这里可写当前语言的特殊实现逻辑 -->
76+
77+
```sql
78+
79+
```
80+
81+
<!-- tabs:end -->
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# [2356. Number of Unique Subjects Taught by Each Teacher](https://leetcode.com/problems/number-of-unique-subjects-taught-by-each-teacher)
2+
3+
[中文文档](/solution/2300-2399/2356.Number%20of%20Unique%20Subjects%20Taught%20by%20Each%20Teacher/README.md)
4+
5+
## Description
6+
7+
<p>Table: <code>Teacher</code></p>
8+
9+
<pre>
10+
+-------------+------+
11+
| Column Name | Type |
12+
+-------------+------+
13+
| teacher_id | int |
14+
| subject_id | int |
15+
| dept_id | int |
16+
+-------------+------+
17+
(subject_id, dept_id) is the primary key for this table.
18+
Each row in this table indicates that the teacher with teacher_id teaches the subject subject_id in the department dept_id.
19+
</pre>
20+
21+
<p>&nbsp;</p>
22+
23+
<p>Write an SQL query to report the number of unique subjects each teacher teaches in the university.</p>
24+
25+
<p>Return the result table in <strong>any order</strong>.</p>
26+
27+
<p>The query result format is shown in the following example.</p>
28+
29+
<p>&nbsp;</p>
30+
<p><strong>Example 1:</strong></p>
31+
32+
<pre>
33+
<strong>Input:</strong>
34+
Teacher table:
35+
+------------+------------+---------+
36+
| teacher_id | subject_id | dept_id |
37+
+------------+------------+---------+
38+
| 1 | 2 | 3 |
39+
| 1 | 2 | 4 |
40+
| 1 | 3 | 3 |
41+
| 2 | 1 | 1 |
42+
| 2 | 2 | 1 |
43+
| 2 | 3 | 1 |
44+
| 2 | 4 | 1 |
45+
+------------+------------+---------+
46+
<strong>Output:</strong>
47+
+------------+-----+
48+
| teacher_id | cnt |
49+
+------------+-----+
50+
| 1 | 2 |
51+
| 2 | 4 |
52+
+------------+-----+
53+
<strong>Explanation:</strong>
54+
Teacher 1:
55+
- They teach subject 2 in departments 3 and 4.
56+
- They teach subject 3 in department 3.
57+
Teacher 2:
58+
- They teach subject 1 in department 1.
59+
- They teach subject 2 in department 1.
60+
- They teach subject 3 in department 1.
61+
- They teach subject 4 in department 1.
62+
</pre>
63+
64+
65+
## Solutions
66+
67+
<!-- tabs:start -->
68+
69+
### **SQL**
70+
71+
```sql
72+
73+
```
74+
75+
<!-- tabs:end -->
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
# [2357. 使数组中所有元素都等于零](https://leetcode.cn/problems/make-array-zero-by-subtracting-equal-amounts)
2+
3+
[English Version](/solution/2300-2399/2357.Make%20Array%20Zero%20by%20Subtracting%20Equal%20Amounts/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个非负整数数组 <code>nums</code> 。在一步操作中,你必须:</p>
10+
11+
<ul>
12+
<li>选出一个正整数 <code>x</code> ,<code>x</code> 需要小于或等于 <code>nums</code> 中 <strong>最小</strong> 的 <strong>非零</strong> 元素。</li>
13+
<li><code>nums</code> 中的每个正整数都减去 <code>x</code>。</li>
14+
</ul>
15+
16+
<p>返回使 <code>nums</code> 中所有元素都等于<em> </em><code>0</code> 需要的 <strong>最少</strong> 操作数。</p>
17+
18+
<p>&nbsp;</p>
19+
20+
<p><strong>示例 1:</strong></p>
21+
22+
<pre>
23+
<strong>输入:</strong>nums = [1,5,0,3,5]
24+
<strong>输出:</strong>3
25+
<strong>解释:</strong>
26+
第一步操作:选出 x = 1 ,之后 nums = [0,4,0,2,4] 。
27+
第二步操作:选出 x = 2 ,之后 nums = [0,2,0,0,2] 。
28+
第三步操作:选出 x = 2 ,之后 nums = [0,0,0,0,0] 。</pre>
29+
30+
<p><strong>示例 2:</strong></p>
31+
32+
<pre>
33+
<strong>输入:</strong>nums = [0]
34+
<strong>输出:</strong>0
35+
<strong>解释:</strong>nums 中的每个元素都已经是 0 ,所以不需要执行任何操作。
36+
</pre>
37+
38+
<p>&nbsp;</p>
39+
40+
<p><strong>提示:</strong></p>
41+
42+
<ul>
43+
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
44+
<li><code>0 &lt;= nums[i] &lt;= 100</code></li>
45+
</ul>
46+
47+
48+
## 解法
49+
50+
<!-- 这里可写通用的实现逻辑 -->
51+
52+
**方法一:哈希表去重**
53+
54+
求去重后的非零元素个数。
55+
56+
时间复杂度 $O(n)$。
57+
58+
<!-- tabs:start -->
59+
60+
### **Python3**
61+
62+
<!-- 这里可写当前语言的特殊实现逻辑 -->
63+
64+
```python
65+
class Solution:
66+
def minimumOperations(self, nums: List[int]) -> int:
67+
s = {v for v in nums if v}
68+
return len(s)
69+
```
70+
71+
### **Java**
72+
73+
<!-- 这里可写当前语言的特殊实现逻辑 -->
74+
75+
```java
76+
class Solution {
77+
public int minimumOperations(int[] nums) {
78+
Set<Integer> s = new HashSet<>();
79+
for (int v : nums) {
80+
if (v > 0) {
81+
s.add(v);
82+
}
83+
}
84+
return s.size();
85+
}
86+
}
87+
```
88+
89+
### **C++**
90+
91+
```cpp
92+
class Solution {
93+
public:
94+
int minimumOperations(vector<int>& nums) {
95+
unordered_set<int> s;
96+
for (int v : nums) if (v) s.insert(v);
97+
return s.size();
98+
}
99+
};
100+
```
101+
102+
### **Go**
103+
104+
```go
105+
func minimumOperations(nums []int) int {
106+
s := map[int]bool{}
107+
for _, v := range nums {
108+
if v > 0 {
109+
s[v] = true
110+
}
111+
}
112+
return len(s)
113+
}
114+
```
115+
116+
### **TypeScript**
117+
118+
```ts
119+
120+
```
121+
122+
### **...**
123+
124+
```
125+
126+
```
127+
128+
<!-- tabs:end -->

0 commit comments

Comments
 (0)