Skip to content

Commit cbaa988

Browse files
committed
feat: add solutions to lc problems: No.2347~2350
* No.2347.Best Poker Hand * No.2348.Number of Zero-Filled Subarrays * No.2349.Design a Number Container System * No.2350.Shortest Impossible Sequence of Rolls
1 parent 67a2062 commit cbaa988

32 files changed

+1860
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# [2346. Compute the Rank as a Percentage](https://leetcode.cn/problems/compute-the-rank-as-a-percentage)
2+
3+
[English Version](/solution/2300-2399/2346.Compute%20the%20Rank%20as%20a%20Percentage/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>Table: <code>Students</code></p>
10+
11+
<pre>
12+
+---------------+------+
13+
| Column Name | Type |
14+
+---------------+------+
15+
| student_id | int |
16+
| department_id | int |
17+
| mark | int |
18+
+---------------+------+
19+
student_id is the primary key of this table.
20+
Each row of this table indicates a student&#39;s ID, the ID of the department in which the student enrolled, and their mark in the exam.
21+
</pre>
22+
23+
<p>&nbsp;</p>
24+
25+
<p>Write an SQL query that reports the rank of each student in their department as a percentage, where the rank as a percentage is computed using the following formula: <code>(student_rank_in_the_department - 1) * 100 / (the_number_of_students_in_the_department - 1)</code>. The <code>percentage</code> should be <strong>rounded to 2 decimal places</strong>. <code>student_rank_in_the_department</code> is determined by <strong>descending</strong><b> </b><code>mark</code>, such that the student with the highest <code>mark</code> is <code>rank 1</code>. If two students get the same mark, they also get the same rank.</p>
26+
27+
<p>Return the result table in <strong>any order</strong>.</p>
28+
29+
<p>The query result format is 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+
Students table:
37+
+------------+---------------+------+
38+
| student_id | department_id | mark |
39+
+------------+---------------+------+
40+
| 2 | 2 | 650 |
41+
| 8 | 2 | 650 |
42+
| 7 | 1 | 920 |
43+
| 1 | 1 | 610 |
44+
| 3 | 1 | 530 |
45+
+------------+---------------+------+
46+
<strong>Output:</strong>
47+
+------------+---------------+------------+
48+
| student_id | department_id | percentage |
49+
+------------+---------------+------------+
50+
| 7 | 1 | 0.0 |
51+
| 1 | 1 | 50.0 |
52+
| 3 | 1 | 100.0 |
53+
| 2 | 2 | 0.0 |
54+
| 8 | 2 | 0.0 |
55+
+------------+---------------+------------+
56+
<strong>Explanation:</strong>
57+
For Department 1:
58+
- Student 7: percentage = (1 - 1) * 100 / (3 - 1) = 0.0
59+
- Student 1: percentage = (2 - 1) * 100 / (3 - 1) = 50.0
60+
- Student 3: percentage = (3 - 1) * 100 / (3 - 1) = 100.0
61+
For Department 2:
62+
- Student 2: percentage = (1 - 1) * 100 / (2 - 1) = 0.0
63+
- Student 8: percentage = (1 - 1) * 100 / (2 - 1) = 0.0
64+
</pre>
65+
66+
## 解法
67+
68+
<!-- 这里可写通用的实现逻辑 -->
69+
70+
<!-- tabs:start -->
71+
72+
### **SQL**
73+
74+
<!-- 这里可写当前语言的特殊实现逻辑 -->
75+
76+
```sql
77+
78+
```
79+
80+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# [2346. Compute the Rank as a Percentage](https://leetcode.com/problems/compute-the-rank-as-a-percentage)
2+
3+
[中文文档](/solution/2300-2399/2346.Compute%20the%20Rank%20as%20a%20Percentage/README.md)
4+
5+
## Description
6+
7+
<p>Table: <code>Students</code></p>
8+
9+
<pre>
10+
+---------------+------+
11+
| Column Name | Type |
12+
+---------------+------+
13+
| student_id | int |
14+
| department_id | int |
15+
| mark | int |
16+
+---------------+------+
17+
student_id is the primary key of this table.
18+
Each row of this table indicates a student&#39;s ID, the ID of the department in which the student enrolled, and their mark in the exam.
19+
</pre>
20+
21+
<p>&nbsp;</p>
22+
23+
<p>Write an SQL query that reports the rank of each student in their department as a percentage, where the rank as a percentage is computed using the following formula: <code>(student_rank_in_the_department - 1) * 100 / (the_number_of_students_in_the_department - 1)</code>. The <code>percentage</code> should be <strong>rounded to 2 decimal places</strong>. <code>student_rank_in_the_department</code> is determined by <strong>descending</strong><b> </b><code>mark</code>, such that the student with the highest <code>mark</code> is <code>rank 1</code>. If two students get the same mark, they also get the same rank.</p>
24+
25+
<p>Return the result table in <strong>any order</strong>.</p>
26+
27+
<p>The query result format is 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+
Students table:
35+
+------------+---------------+------+
36+
| student_id | department_id | mark |
37+
+------------+---------------+------+
38+
| 2 | 2 | 650 |
39+
| 8 | 2 | 650 |
40+
| 7 | 1 | 920 |
41+
| 1 | 1 | 610 |
42+
| 3 | 1 | 530 |
43+
+------------+---------------+------+
44+
<strong>Output:</strong>
45+
+------------+---------------+------------+
46+
| student_id | department_id | percentage |
47+
+------------+---------------+------------+
48+
| 7 | 1 | 0.0 |
49+
| 1 | 1 | 50.0 |
50+
| 3 | 1 | 100.0 |
51+
| 2 | 2 | 0.0 |
52+
| 8 | 2 | 0.0 |
53+
+------------+---------------+------------+
54+
<strong>Explanation:</strong>
55+
For Department 1:
56+
- Student 7: percentage = (1 - 1) * 100 / (3 - 1) = 0.0
57+
- Student 1: percentage = (2 - 1) * 100 / (3 - 1) = 50.0
58+
- Student 3: percentage = (3 - 1) * 100 / (3 - 1) = 100.0
59+
For Department 2:
60+
- Student 2: percentage = (1 - 1) * 100 / (2 - 1) = 0.0
61+
- Student 8: percentage = (1 - 1) * 100 / (2 - 1) = 0.0
62+
</pre>
63+
64+
## Solutions
65+
66+
<!-- tabs:start -->
67+
68+
### **SQL**
69+
70+
```sql
71+
72+
```
73+
74+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
# [2347. 最好的扑克手牌](https://leetcode.cn/problems/best-poker-hand)
2+
3+
[English Version](/solution/2300-2399/2347.Best%20Poker%20Hand/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个整数数组&nbsp;<code>ranks</code>&nbsp;和一个字符数组&nbsp;<code>suit</code>&nbsp;。你有&nbsp;<code>5</code>&nbsp;张扑克牌,第&nbsp;<code>i</code>&nbsp;张牌大小为&nbsp;<code>ranks[i]</code>&nbsp;,花色为&nbsp;<code>suits[i]</code>&nbsp;。</p>
10+
11+
<p>下述是从好到坏你可能持有的 <strong>手牌类型&nbsp;</strong>:</p>
12+
13+
<ol>
14+
<li><code>"Flush"</code>:同花,五张相同花色的扑克牌。</li>
15+
<li><code>"Three of a Kind"</code>:三条,有 3 张大小相同的扑克牌。</li>
16+
<li><code>"Pair"</code>:对子,两张大小一样的扑克牌。</li>
17+
<li><code>"High Card"</code>:高牌,五张大小互不相同的扑克牌。</li>
18+
</ol>
19+
20+
<p>请你返回一个字符串,表示给定的 5 张牌中,你能组成的 <strong>最好手牌类型</strong>&nbsp;。</p>
21+
22+
<p><strong>注意:</strong>返回的字符串&nbsp;<strong>大小写</strong>&nbsp;需与题目描述相同。</p>
23+
24+
<p>&nbsp;</p>
25+
26+
<p><strong>示例 1:</strong></p>
27+
28+
<pre><b>输入:</b>ranks = [13,2,3,1,9], suits = ["a","a","a","a","a"]
29+
<b>输出:</b>"Flush"
30+
<b>解释:</b>5 张扑克牌的花色相同,所以返回 "Flush" 。
31+
</pre>
32+
33+
<p><strong>示例 2:</strong></p>
34+
35+
<pre><b>输入:</b>ranks = [4,4,2,4,4], suits = ["d","a","a","b","c"]
36+
<b>输出:</b>"Three of a Kind"
37+
<b>解释:</b>第一、二和四张牌组成三张相同大小的扑克牌,所以得到 "Three of a Kind" 。
38+
注意我们也可以得到 "Pair" ,但是 "Three of a Kind" 是更好的手牌类型。
39+
有其他的 3 张牌也可以组成 "Three of a Kind" 手牌类型。</pre>
40+
41+
<p><strong>示例 3:</strong></p>
42+
43+
<pre><b>输入:</b>ranks = [10,10,2,12,9], suits = ["a","b","c","a","d"]
44+
<b>输出:</b>"Pair"
45+
<b>解释:</b>第一和第二张牌大小相同,所以得到 "Pair" 。
46+
我们无法得到 "Flush" 或者 "Three of a Kind" 。
47+
</pre>
48+
49+
<p>&nbsp;</p>
50+
51+
<p><strong>提示:</strong></p>
52+
53+
<ul>
54+
<li><code>ranks.length == suits.length == 5</code></li>
55+
<li><code>1 &lt;= ranks[i] &lt;= 13</code></li>
56+
<li><code>'a' &lt;= suits[i] &lt;= 'd'</code></li>
57+
<li>任意两张扑克牌不会同时有相同的大小和花色。</li>
58+
</ul>
59+
60+
## 解法
61+
62+
<!-- 这里可写通用的实现逻辑 -->
63+
64+
<!-- tabs:start -->
65+
66+
### **Python3**
67+
68+
<!-- 这里可写当前语言的特殊实现逻辑 -->
69+
70+
```python
71+
class Solution:
72+
def bestHand(self, ranks: List[int], suits: List[str]) -> str:
73+
if len(set(suits)) == 1:
74+
return 'Flush'
75+
cnt = Counter(ranks)
76+
if any(v >= 3 for v in cnt.values()):
77+
return 'Three of a Kind'
78+
if any(v == 2 for v in cnt.values()):
79+
return 'Pair'
80+
return 'High Card'
81+
```
82+
83+
### **Java**
84+
85+
<!-- 这里可写当前语言的特殊实现逻辑 -->
86+
87+
```java
88+
class Solution {
89+
public String bestHand(int[] ranks, char[] suits) {
90+
Set<Character> s = new HashSet<>();
91+
for (char c : suits) {
92+
s.add(c);
93+
}
94+
if (s.size() == 1) {
95+
return "Flush";
96+
}
97+
int[] cnt = new int[20];
98+
for (int v : ranks) {
99+
++cnt[v];
100+
if (cnt[v] >= 3) {
101+
return "Three of a Kind";
102+
}
103+
}
104+
for (int v : cnt) {
105+
if (v == 2) {
106+
return "Pair";
107+
}
108+
}
109+
return "High Card";
110+
}
111+
}
112+
```
113+
114+
### **C++**
115+
116+
```cpp
117+
class Solution {
118+
public:
119+
string bestHand(vector<int>& ranks, vector<char>& suits) {
120+
unordered_set<char> s(suits.begin(), suits.end());
121+
if (s.size() == 1) return "Flush";
122+
vector<int> cnt(20);
123+
for (int v : ranks) if (++cnt[v] >= 3) return "Three of a Kind";
124+
for (int v : cnt) if (v == 2) return "Pair";
125+
return "High Card";
126+
}
127+
};
128+
```
129+
130+
### **Go**
131+
132+
```go
133+
func bestHand(ranks []int, suits []byte) string {
134+
s := map[byte]bool{}
135+
for _, v := range suits {
136+
s[v] = true
137+
}
138+
if len(s) == 1 {
139+
return "Flush"
140+
}
141+
cnt := make([]int, 20)
142+
for _, v := range ranks {
143+
cnt[v]++
144+
if cnt[v] >= 3 {
145+
return "Three of a Kind"
146+
}
147+
}
148+
for _, v := range cnt {
149+
if v == 2 {
150+
return "Pair"
151+
}
152+
}
153+
return "High Card"
154+
}
155+
```
156+
157+
### **TypeScript**
158+
159+
```ts
160+
161+
```
162+
163+
### **...**
164+
165+
```
166+
167+
```
168+
169+
<!-- tabs:end -->

0 commit comments

Comments
 (0)