Skip to content

Commit c1ee2b3

Browse files
committed
feat: add new lc problems
1 parent a2bd594 commit c1ee2b3

File tree

9 files changed

+397
-15
lines changed

9 files changed

+397
-15
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# [1988. Find Cutoff Score for Each School](https://leetcode-cn.com/problems/find-cutoff-score-for-each-school)
2+
3+
[English Version](/solution/1900-1999/1988.Find%20Cutoff%20Score%20for%20Each%20School/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>Table: <code>Schools</code></p>
10+
11+
<pre>
12+
+-------------+------+
13+
| Column Name | Type |
14+
+-------------+------+
15+
| school_id | int |
16+
| capacity | int |
17+
+-------------+------+
18+
school_id is the primary key for this table.
19+
This table contains information about the capacity of some schools. The capacity is the maximum number of students the school can accept.
20+
</pre>
21+
22+
<p>&nbsp;</p>
23+
24+
<p>Table: <code>Exam</code></p>
25+
26+
<pre>
27+
+---------------+------+
28+
| Column Name | Type |
29+
+---------------+------+
30+
| score | int |
31+
| student_count | int |
32+
+---------------+------+
33+
score is the primary key for this table.
34+
Each row in this table indicates that there are student_count students that got at least score points in the exam.
35+
The data in this table will be logically correct, meaning a row recording a higher score will have the same or smaller student_count compared to a row recording a lower score. More formally, for every two rows i and j in the table, if score<sub>i</sub> &gt; score<sub>j</sub> then student_count<sub>i</sub> &lt;= student_count<sub>j</sub>.
36+
</pre>
37+
38+
<p>&nbsp;</p>
39+
40+
<p>Every year, each school announces a <strong>minimum score requirement</strong> that a student needs to apply to it. The school chooses the minimum score requirement based on the exam results of all the students:</p>
41+
42+
<ol>
43+
<li>They want to ensure that even if <strong>every</strong> student meeting the requirement applies, the school can accept everyone.</li>
44+
<li>They also want to <strong>maximize</strong> the possible number of students that can apply.</li>
45+
<li>They <strong>must</strong> use a score that is in the <code>Exam</code> table.</li>
46+
</ol>
47+
48+
<p>Write an SQL query to report the <strong>minimum score requirement</strong> for each school. If there are multiple score values satisfying the above conditions, choose the <strong>smallest</strong> one. If the input data is not enough to determine the score, report <code>-1</code>.</p>
49+
50+
<p>Return the result table in <strong>any order</strong>.</p>
51+
52+
<p>The query result format is in the following example.</p>
53+
54+
<p>&nbsp;</p>
55+
<p><strong>Example 1:</strong></p>
56+
57+
<pre>
58+
<strong>Input:</strong>
59+
Schools table:
60+
+-----------+----------+
61+
| school_id | capacity |
62+
+-----------+----------+
63+
| 11 | 151 |
64+
| 5 | 48 |
65+
| 9 | 9 |
66+
| 10 | 99 |
67+
+-----------+----------+
68+
Exam table:
69+
+-------+---------------+
70+
| score | student_count |
71+
+-------+---------------+
72+
| 975 | 10 |
73+
| 966 | 60 |
74+
| 844 | 76 |
75+
| 749 | 76 |
76+
| 744 | 100 |
77+
+-------+---------------+
78+
<strong>Output:</strong>
79+
+-----------+-------+
80+
| school_id | score |
81+
+-----------+-------+
82+
| 5 | 975 |
83+
| 9 | -1 |
84+
| 10 | 749 |
85+
| 11 | 744 |
86+
+-----------+-------+
87+
<strong>Explanation:</strong>
88+
- School 5: The school&#39;s capacity is 48. Choosing 975 as the min score requirement, the school will get at most 10 applications, which is within capacity.
89+
- School 10: The school&#39;s capacity is 99. Choosing 844 or 749 as the min score requirement, the school will get at most 76 applications, which is within capacity. We choose the smallest of them, which is 749.
90+
- School 11: The school&#39;s capacity is 151. Choosing 744 as the min score requirement, the school will get at most 100 applications, which is within capacity.
91+
- School 9: The data given is not enough to determine the min score requirement. Choosing 975 as the min score, the school may get 10 requests while its capacity is 9. We do not have information about higher scores, hence we report -1.
92+
</pre>
93+
94+
95+
## 解法
96+
97+
<!-- 这里可写通用的实现逻辑 -->
98+
99+
<!-- tabs:start -->
100+
101+
### **SQL**
102+
103+
<!-- 这里可写当前语言的特殊实现逻辑 -->
104+
105+
```sql
106+
107+
```
108+
109+
<!-- tabs:end -->
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# [1988. Find Cutoff Score for Each School](https://leetcode.com/problems/find-cutoff-score-for-each-school)
2+
3+
[中文文档](/solution/1900-1999/1988.Find%20Cutoff%20Score%20for%20Each%20School/README.md)
4+
5+
## Description
6+
7+
<p>Table: <code>Schools</code></p>
8+
9+
<pre>
10+
+-------------+------+
11+
| Column Name | Type |
12+
+-------------+------+
13+
| school_id | int |
14+
| capacity | int |
15+
+-------------+------+
16+
school_id is the primary key for this table.
17+
This table contains information about the capacity of some schools. The capacity is the maximum number of students the school can accept.
18+
</pre>
19+
20+
<p>&nbsp;</p>
21+
22+
<p>Table: <code>Exam</code></p>
23+
24+
<pre>
25+
+---------------+------+
26+
| Column Name | Type |
27+
+---------------+------+
28+
| score | int |
29+
| student_count | int |
30+
+---------------+------+
31+
score is the primary key for this table.
32+
Each row in this table indicates that there are student_count students that got at least score points in the exam.
33+
The data in this table will be logically correct, meaning a row recording a higher score will have the same or smaller student_count compared to a row recording a lower score. More formally, for every two rows i and j in the table, if score<sub>i</sub> &gt; score<sub>j</sub> then student_count<sub>i</sub> &lt;= student_count<sub>j</sub>.
34+
</pre>
35+
36+
<p>&nbsp;</p>
37+
38+
<p>Every year, each school announces a <strong>minimum score requirement</strong> that a student needs to apply to it. The school chooses the minimum score requirement based on the exam results of all the students:</p>
39+
40+
<ol>
41+
<li>They want to ensure that even if <strong>every</strong> student meeting the requirement applies, the school can accept everyone.</li>
42+
<li>They also want to <strong>maximize</strong> the possible number of students that can apply.</li>
43+
<li>They <strong>must</strong> use a score that is in the <code>Exam</code> table.</li>
44+
</ol>
45+
46+
<p>Write an SQL query to report the <strong>minimum score requirement</strong> for each school. If there are multiple score values satisfying the above conditions, choose the <strong>smallest</strong> one. If the input data is not enough to determine the score, report <code>-1</code>.</p>
47+
48+
<p>Return the result table in <strong>any order</strong>.</p>
49+
50+
<p>The query result format is in the following example.</p>
51+
52+
<p>&nbsp;</p>
53+
<p><strong>Example 1:</strong></p>
54+
55+
<pre>
56+
<strong>Input:</strong>
57+
Schools table:
58+
+-----------+----------+
59+
| school_id | capacity |
60+
+-----------+----------+
61+
| 11 | 151 |
62+
| 5 | 48 |
63+
| 9 | 9 |
64+
| 10 | 99 |
65+
+-----------+----------+
66+
Exam table:
67+
+-------+---------------+
68+
| score | student_count |
69+
+-------+---------------+
70+
| 975 | 10 |
71+
| 966 | 60 |
72+
| 844 | 76 |
73+
| 749 | 76 |
74+
| 744 | 100 |
75+
+-------+---------------+
76+
<strong>Output:</strong>
77+
+-----------+-------+
78+
| school_id | score |
79+
+-----------+-------+
80+
| 5 | 975 |
81+
| 9 | -1 |
82+
| 10 | 749 |
83+
| 11 | 744 |
84+
+-----------+-------+
85+
<strong>Explanation:</strong>
86+
- School 5: The school&#39;s capacity is 48. Choosing 975 as the min score requirement, the school will get at most 10 applications, which is within capacity.
87+
- School 10: The school&#39;s capacity is 99. Choosing 844 or 749 as the min score requirement, the school will get at most 76 applications, which is within capacity. We choose the smallest of them, which is 749.
88+
- School 11: The school&#39;s capacity is 151. Choosing 744 as the min score requirement, the school will get at most 100 applications, which is within capacity.
89+
- School 9: The data given is not enough to determine the min score requirement. Choosing 975 as the min score, the school may get 10 requests while its capacity is 9. We do not have information about higher scores, hence we report -1.
90+
</pre>
91+
92+
93+
## Solutions
94+
95+
<!-- tabs:start -->
96+
97+
### **SQL**
98+
99+
```sql
100+
101+
```
102+
103+
<!-- tabs:end -->
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# [1989. Maximum Number of People That Can Be Caught in Tag](https://leetcode-cn.com/problems/maximum-number-of-people-that-can-be-caught-in-tag)
2+
3+
[English Version](/solution/1900-1999/1989.Maximum%20Number%20of%20People%20That%20Can%20Be%20Caught%20in%20Tag/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>You are playing a game of tag with your friends. In tag, people are divided into two teams: people who are &quot;it&quot;, and people who are not &quot;it&quot;. The people who are &quot;it&quot; want to catch as many people as possible who are not &quot;it&quot;.</p>
10+
11+
<p>You are given a <strong>0-indexed</strong> integer array <code>team</code> containing only zeros (denoting people who are <strong>not</strong> &quot;it&quot;) and ones (denoting people who are &quot;it&quot;), and an integer <code>dist</code>. A person who is &quot;it&quot; at index <code>i</code> can catch any <strong>one</strong> person whose index is in the range <code>[i - dist, i + dist]</code> (<strong>inclusive</strong>) and is <strong>not</strong> &quot;it&quot;.</p>
12+
13+
<p>Return <em>the <strong>maximum</strong> number of people that the people who are &quot;it&quot; can catch</em>.</p>
14+
15+
<p>&nbsp;</p>
16+
<p><strong>Example 1:</strong></p>
17+
18+
<pre>
19+
<strong>Input:</strong> team = [0,1,0,1,0], dist = 3
20+
<strong>Output:</strong> 2
21+
<strong>Explanation:</strong>
22+
The person who is &quot;it&quot; at index 1 can catch people in the range [i-dist, i+dist] = [1-3, 1+3] = [-2, 4].
23+
They can catch the person who is not &quot;it&quot; at index 2.
24+
The person who is &quot;it&quot; at index 3 can catch people in the range [i-dist, i+dist] = [3-3, 3+3] = [0, 6].
25+
They can catch the person who is not &quot;it&quot; at index 0.
26+
The person who is not &quot;it&quot; at index 4 will not be caught because the people at indices 1 and 3 are already catching one person.</pre>
27+
28+
<p><strong>Example 2:</strong></p>
29+
30+
<pre>
31+
<strong>Input:</strong> team = [1], dist = 1
32+
<strong>Output:</strong> 0
33+
<strong>Explanation:</strong>
34+
There are no people who are not &quot;it&quot; to catch.
35+
</pre>
36+
37+
<p><strong>Example 3:</strong></p>
38+
39+
<pre>
40+
<strong>Input:</strong> team = [0], dist = 1
41+
<strong>Output:</strong> 0
42+
<strong>Explanation:
43+
</strong>There are no people who are &quot;it&quot; to catch people.
44+
</pre>
45+
46+
<p>&nbsp;</p>
47+
<p><strong>Constraints:</strong></p>
48+
49+
<ul>
50+
<li><code>1 &lt;= team.length &lt;= 10<sup>5</sup></code></li>
51+
<li><code>0 &lt;= team[i] &lt;= 1</code></li>
52+
<li><code>1 &lt;= dist &lt;= team.length</code></li>
53+
</ul>
54+
55+
56+
## 解法
57+
58+
<!-- 这里可写通用的实现逻辑 -->
59+
60+
<!-- tabs:start -->
61+
62+
### **Python3**
63+
64+
<!-- 这里可写当前语言的特殊实现逻辑 -->
65+
66+
```python
67+
68+
```
69+
70+
### **Java**
71+
72+
<!-- 这里可写当前语言的特殊实现逻辑 -->
73+
74+
```java
75+
76+
```
77+
78+
### **...**
79+
80+
```
81+
82+
```
83+
84+
<!-- tabs:end -->
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# [1989. Maximum Number of People That Can Be Caught in Tag](https://leetcode.com/problems/maximum-number-of-people-that-can-be-caught-in-tag)
2+
3+
[中文文档](/solution/1900-1999/1989.Maximum%20Number%20of%20People%20That%20Can%20Be%20Caught%20in%20Tag/README.md)
4+
5+
## Description
6+
7+
<p>You are playing a game of tag with your friends. In tag, people are divided into two teams: people who are &quot;it&quot;, and people who are not &quot;it&quot;. The people who are &quot;it&quot; want to catch as many people as possible who are not &quot;it&quot;.</p>
8+
9+
<p>You are given a <strong>0-indexed</strong> integer array <code>team</code> containing only zeros (denoting people who are <strong>not</strong> &quot;it&quot;) and ones (denoting people who are &quot;it&quot;), and an integer <code>dist</code>. A person who is &quot;it&quot; at index <code>i</code> can catch any <strong>one</strong> person whose index is in the range <code>[i - dist, i + dist]</code> (<strong>inclusive</strong>) and is <strong>not</strong> &quot;it&quot;.</p>
10+
11+
<p>Return <em>the <strong>maximum</strong> number of people that the people who are &quot;it&quot; can catch</em>.</p>
12+
13+
<p>&nbsp;</p>
14+
<p><strong>Example 1:</strong></p>
15+
16+
<pre>
17+
<strong>Input:</strong> team = [0,1,0,1,0], dist = 3
18+
<strong>Output:</strong> 2
19+
<strong>Explanation:</strong>
20+
The person who is &quot;it&quot; at index 1 can catch people in the range [i-dist, i+dist] = [1-3, 1+3] = [-2, 4].
21+
They can catch the person who is not &quot;it&quot; at index 2.
22+
The person who is &quot;it&quot; at index 3 can catch people in the range [i-dist, i+dist] = [3-3, 3+3] = [0, 6].
23+
They can catch the person who is not &quot;it&quot; at index 0.
24+
The person who is not &quot;it&quot; at index 4 will not be caught because the people at indices 1 and 3 are already catching one person.</pre>
25+
26+
<p><strong>Example 2:</strong></p>
27+
28+
<pre>
29+
<strong>Input:</strong> team = [1], dist = 1
30+
<strong>Output:</strong> 0
31+
<strong>Explanation:</strong>
32+
There are no people who are not &quot;it&quot; to catch.
33+
</pre>
34+
35+
<p><strong>Example 3:</strong></p>
36+
37+
<pre>
38+
<strong>Input:</strong> team = [0], dist = 1
39+
<strong>Output:</strong> 0
40+
<strong>Explanation:
41+
</strong>There are no people who are &quot;it&quot; to catch people.
42+
</pre>
43+
44+
<p>&nbsp;</p>
45+
<p><strong>Constraints:</strong></p>
46+
47+
<ul>
48+
<li><code>1 &lt;= team.length &lt;= 10<sup>5</sup></code></li>
49+
<li><code>0 &lt;= team[i] &lt;= 1</code></li>
50+
<li><code>1 &lt;= dist &lt;= team.length</code></li>
51+
</ul>
52+
53+
54+
## Solutions
55+
56+
<!-- tabs:start -->
57+
58+
### **Python3**
59+
60+
```python
61+
62+
```
63+
64+
### **Java**
65+
66+
```java
67+
68+
```
69+
70+
### **...**
71+
72+
```
73+
74+
```
75+
76+
<!-- tabs:end -->

0 commit comments

Comments
 (0)