Skip to content

Commit a6160a3

Browse files
committed
feat: add new lc problems
1 parent 845f1b9 commit a6160a3

File tree

23 files changed

+1446
-1
lines changed

23 files changed

+1446
-1
lines changed

solution/1200-1299/1240.Tiling a Rectangle with the Fewest Squares/README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,12 @@
5757

5858
**方法一:递归回溯 + 状态压缩**
5959

60-
我们可以按位置进行递归回溯。如果当前位置 $(i, j)$ 已经被填充,则直接递归到下一个位置 $(i, j + 1)$。否则,我们枚举当前位置 $(i, j)$ 可以填充的最大正方形的边长 $w$,并将当前位置 $(i, j)$ 到 $(i + w - 1, j + w - 1)$ 的位置全部填充,然后递归到下一个位置 $(i, j + w)$。在回溯时,我们需要将当前位置 $(i, j)$ 到 $(i + w - 1, j + w - 1)$ 的位置全部清空。
60+
我们可以按位置进行递归回溯,过程中我们用一个变量 $t$ 记录当前使用的瓷砖数。
61+
62+
- 如果 $j = m$,即第 $i$ 行已经被完全填充,则递归到下一行,即 $(i + 1, 0)$。
63+
- 如果 $i = n$,则表示所有位置都已经被填充,我们更新答案并返回。
64+
- 如果当前位置 $(i, j)$ 已经被填充,则直接递归到下一个位置 $(i, j + 1)$。
65+
- 否则,我们枚举当前位置 $(i, j)$ 可以填充的最大正方形的边长 $w$,并将当前位置 $(i, j)$ 到 $(i + w - 1, j + w - 1)$ 的位置全部填充,然后递归到下一个位置 $(i, j + w)$。在回溯时,我们需要将当前位置 $(i, j)$ 到 $(i + w - 1, j + w - 1)$ 的位置全部清空。
6166

6267
由于每个位置只有两种状态:填充或者未填充,因此我们可以使用一个整数来表示当前位置的状态。我们使用一个长度为 $n$ 的整数数组 $filled$,其中 $filled[i]$ 表示第 $i$ 行的状态。如果 $filled[i]$ 的第 $j$ 位为 $1$,则表示第 $i$ 行第 $j$ 列已经被填充,否则表示未填充。
6368

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# [2720. Popularity Percentage](https://leetcode.cn/problems/popularity-percentage)
2+
3+
[English Version](/solution/2700-2799/2720.Popularity%20Percentage/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>Table: <code>Friends</code></p>
10+
11+
<pre>
12+
+-------------+------+
13+
| Column Name | Type |
14+
+-------------+------+
15+
| user1 | int |
16+
| user2 | int |
17+
+-------------+------+
18+
(user1, user2) is the primary key of this table.
19+
Each row contains information about friendship where user1 and user2 are friends.
20+
</pre>
21+
22+
<p>Write an SQL query to find the popularity percentage for each user on Meta/Facebook. The popularity percentage is defined as the total number of friends the user has divided by the total number of users on the platform, then converted into a percentage by multiplying by 100, <strong>rounded to 2 decimal places</strong>.</p>
23+
24+
<p>Return <em>the result table ordered by</em> <code>user1</code> <em>in <strong>ascending</strong> order.</em></p>
25+
26+
<p>The query result format is in the following example.</p>
27+
28+
<p>&nbsp;</p>
29+
<p><strong class="example">Example 1:</strong></p>
30+
31+
<pre>
32+
<strong>Input:</strong>&nbsp;
33+
Friends table:
34+
+-------+-------+
35+
| user1 | user2 |
36+
+-------+-------+
37+
| 2 &nbsp; &nbsp; | 1 &nbsp; &nbsp; |
38+
| 1 &nbsp; &nbsp; | 3 &nbsp; &nbsp; |
39+
| 4 &nbsp; &nbsp; | 1 &nbsp; &nbsp; |
40+
| 1 &nbsp; &nbsp; | 5 &nbsp; &nbsp; |
41+
| 1 &nbsp; &nbsp; | 6 &nbsp; &nbsp; |
42+
| 2 &nbsp; &nbsp; | 6 &nbsp; &nbsp; |
43+
| 7 &nbsp; &nbsp; | 2 &nbsp; &nbsp; |
44+
| 8 &nbsp; &nbsp; | 3&nbsp; &nbsp; &nbsp;|
45+
| 3 &nbsp; &nbsp; | 9 &nbsp; &nbsp; |
46+
+-------+-------+
47+
<strong>Output:</strong>&nbsp;
48+
+-------+-----------------------+
49+
| user1 | percentage_popularity |
50+
+-------+-----------------------+
51+
| 1 | 55.56 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|
52+
| 2 | 33.33 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|
53+
| 3 | 33.33 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |
54+
| 4 | 11.11 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |
55+
| 5 | 11.11 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |
56+
| 6 | 22.22 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |
57+
| 7 | 11.11 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |
58+
| 8 | 11.11 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |
59+
| 9 | 11.11 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |
60+
+-------+-----------------------+
61+
<strong>Explanation:</strong>&nbsp;
62+
There are total 9 users on the platform.
63+
- User &quot;1&quot; has friendships with 2, 3, 4, 5 and 6. Therefore, the percentage popularity for user 1 would be calculated as (5/9) * 100 = 55.56.
64+
- User &quot;2&quot; has friendships with 1, 6 and 7. Therefore, the percentage popularity for user 2 would be calculated as (3/9) * 100 = 33.33.
65+
- User &quot;3&quot; has friendships with 1, 8 and 9. Therefore, the percentage popularity for user 3 would be calculated as (3/9) * 100 = 33.33.
66+
- User &quot;4&quot; has friendships with 1. Therefore, the percentage popularity for user 4 would be calculated as (1/9) * 100 = 11.11.
67+
- User &quot;5&quot; has friendships with 1. Therefore, the percentage popularity for user 5 would be calculated as (1/9) * 100 = 11.11.
68+
- User &quot;6&quot; has friendships with 1 and 2. Therefore, the percentage popularity for user 6 would be calculated as (2/9) * 100 = 22.22.
69+
- User &quot;7&quot; has friendships with 2. Therefore, the percentage popularity for user 7 would be calculated as (1/9) * 100 = 11.11.
70+
- User &quot;8&quot; has friendships with 3. Therefore, the percentage popularity for user 8 would be calculated as (1/9) * 100 = 11.11.
71+
- User &quot;9&quot; has friendships with 3. Therefore, the percentage popularity for user 9 would be calculated as (1/9) * 100 = 11.11.
72+
user1 is sorted in ascending order.
73+
</pre>
74+
75+
## 解法
76+
77+
<!-- 这里可写通用的实现逻辑 -->
78+
79+
<!-- tabs:start -->
80+
81+
### **SQL**
82+
83+
<!-- 这里可写当前语言的特殊实现逻辑 -->
84+
85+
```sql
86+
87+
```
88+
89+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# [2720. Popularity Percentage](https://leetcode.com/problems/popularity-percentage)
2+
3+
[中文文档](/solution/2700-2799/2720.Popularity%20Percentage/README.md)
4+
5+
## Description
6+
7+
<p>Table: <code>Friends</code></p>
8+
9+
<pre>
10+
+-------------+------+
11+
| Column Name | Type |
12+
+-------------+------+
13+
| user1 | int |
14+
| user2 | int |
15+
+-------------+------+
16+
(user1, user2) is the primary key of this table.
17+
Each row contains information about friendship where user1 and user2 are friends.
18+
</pre>
19+
20+
<p>Write an SQL query to find the popularity percentage for each user on Meta/Facebook. The popularity percentage is defined as the total number of friends the user has divided by the total number of users on the platform, then converted into a percentage by multiplying by 100, <strong>rounded to 2 decimal places</strong>.</p>
21+
22+
<p>Return <em>the result table ordered by</em> <code>user1</code> <em>in <strong>ascending</strong> order.</em></p>
23+
24+
<p>The query result format is in the following example.</p>
25+
26+
<p>&nbsp;</p>
27+
<p><strong class="example">Example 1:</strong></p>
28+
29+
<pre>
30+
<strong>Input:</strong>&nbsp;
31+
Friends table:
32+
+-------+-------+
33+
| user1 | user2 |
34+
+-------+-------+
35+
| 2 &nbsp; &nbsp; | 1 &nbsp; &nbsp; |
36+
| 1 &nbsp; &nbsp; | 3 &nbsp; &nbsp; |
37+
| 4 &nbsp; &nbsp; | 1 &nbsp; &nbsp; |
38+
| 1 &nbsp; &nbsp; | 5 &nbsp; &nbsp; |
39+
| 1 &nbsp; &nbsp; | 6 &nbsp; &nbsp; |
40+
| 2 &nbsp; &nbsp; | 6 &nbsp; &nbsp; |
41+
| 7 &nbsp; &nbsp; | 2 &nbsp; &nbsp; |
42+
| 8 &nbsp; &nbsp; | 3&nbsp; &nbsp; &nbsp;|
43+
| 3 &nbsp; &nbsp; | 9 &nbsp; &nbsp; |
44+
+-------+-------+
45+
<strong>Output:</strong>&nbsp;
46+
+-------+-----------------------+
47+
| user1 | percentage_popularity |
48+
+-------+-----------------------+
49+
| 1 | 55.56 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|
50+
| 2 | 33.33 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|
51+
| 3 | 33.33 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |
52+
| 4 | 11.11 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |
53+
| 5 | 11.11 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |
54+
| 6 | 22.22 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |
55+
| 7 | 11.11 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |
56+
| 8 | 11.11 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |
57+
| 9 | 11.11 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |
58+
+-------+-----------------------+
59+
<strong>Explanation:</strong>&nbsp;
60+
There are total 9 users on the platform.
61+
- User &quot;1&quot; has friendships with 2, 3, 4, 5 and 6. Therefore, the percentage popularity for user 1 would be calculated as (5/9) * 100 = 55.56.
62+
- User &quot;2&quot; has friendships with 1, 6 and 7. Therefore, the percentage popularity for user 2 would be calculated as (3/9) * 100 = 33.33.
63+
- User &quot;3&quot; has friendships with 1, 8 and 9. Therefore, the percentage popularity for user 3 would be calculated as (3/9) * 100 = 33.33.
64+
- User &quot;4&quot; has friendships with 1. Therefore, the percentage popularity for user 4 would be calculated as (1/9) * 100 = 11.11.
65+
- User &quot;5&quot; has friendships with 1. Therefore, the percentage popularity for user 5 would be calculated as (1/9) * 100 = 11.11.
66+
- User &quot;6&quot; has friendships with 1 and 2. Therefore, the percentage popularity for user 6 would be calculated as (2/9) * 100 = 22.22.
67+
- User &quot;7&quot; has friendships with 2. Therefore, the percentage popularity for user 7 would be calculated as (1/9) * 100 = 11.11.
68+
- User &quot;8&quot; has friendships with 3. Therefore, the percentage popularity for user 8 would be calculated as (1/9) * 100 = 11.11.
69+
- User &quot;9&quot; has friendships with 3. Therefore, the percentage popularity for user 9 would be calculated as (1/9) * 100 = 11.11.
70+
user1 is sorted in ascending order.
71+
</pre>
72+
73+
## Solutions
74+
75+
<!-- tabs:start -->
76+
77+
### **SQL**
78+
79+
```sql
80+
81+
```
82+
83+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# [2721. Execute Asynchronous Functions in Parallel](https://leetcode.cn/problems/execute-asynchronous-functions-in-parallel)
2+
3+
[English Version](/solution/2700-2799/2721.Execute%20Asynchronous%20Functions%20in%20Parallel/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>Given an array of asynchronous&nbsp;functions&nbsp;<code>functions</code>, return a new promise <code>promise</code>. Each function in the array accepts no arguments&nbsp;and returns a promise.</p>
10+
11+
<p><code>promise</code> resolves:</p>
12+
13+
<ul>
14+
<li>When all the promises returned from&nbsp;<code>functions</code>&nbsp;were resolved successfully.&nbsp;The resolved&nbsp;value of&nbsp;<code>promise</code> should be an array of all the resolved values of promises in the same order as they were in the&nbsp;<code>functions.</code></li>
15+
</ul>
16+
17+
<p><code>promise</code> rejects:</p>
18+
19+
<ul>
20+
<li>When any&nbsp;of the promises&nbsp;returned from&nbsp;<code>functions</code>&nbsp;were rejected.&nbsp;<code>promise</code> should also&nbsp;reject&nbsp;with the reason of the first rejection.</li>
21+
</ul>
22+
23+
<p>Please solve it without using the built-in&nbsp;<code>Promise.all</code>&nbsp;function.</p>
24+
25+
<p>&nbsp;</p>
26+
<p><strong class="example">Example 1:</strong></p>
27+
28+
<pre>
29+
<strong>Input:</strong> functions = [
30+
&nbsp; () =&gt; new Promise(resolve =&gt; setTimeout(() =&gt; resolve(5), 200))
31+
]
32+
<strong>Output:</strong> {&quot;t&quot;: 200, &quot;resolved&quot;: [5]}
33+
<strong>Explanation:</strong>
34+
promiseAll(functions).then(console.log); // [5]
35+
36+
The single function was resolved at 200ms with a value of 5.
37+
</pre>
38+
39+
<p><strong class="example">Example 2:</strong></p>
40+
41+
<pre>
42+
<strong>Input:</strong> functions = [
43+
() =&gt; new Promise(resolve =&gt; setTimeout(() =&gt; resolve(1), 200)),
44+
() =&gt; new Promise((resolve, reject) =&gt; setTimeout(() =&gt; reject(&quot;Error&quot;), 100))
45+
]
46+
<strong>Output:</strong> {&quot;t&quot;: 100, &quot;rejected&quot;: &quot;Error&quot;}
47+
<strong>Explanation:</strong> Since one of the promises rejected, the returned promise also rejected with the same error at the same time.
48+
</pre>
49+
50+
<p><strong class="example">Example 3:</strong></p>
51+
52+
<pre>
53+
<strong>Input:</strong> functions = [
54+
() =&gt; new Promise(resolve =&gt; setTimeout(() =&gt; resolve(4), 50)),
55+
() =&gt; new Promise(resolve =&gt; setTimeout(() =&gt; resolve(10), 150)),
56+
() =&gt; new Promise(resolve =&gt; setTimeout(() =&gt; resolve(16), 100))
57+
]
58+
<strong>Output:</strong> {&quot;t&quot;: 150, &quot;resolved&quot;: [4, 10, 16]}
59+
<strong>Explanation:</strong> All the promises resolved with a value. The returned promise resolved when the last promise resolved.
60+
</pre>
61+
62+
<p>&nbsp;</p>
63+
<p><strong>Constraints:</strong></p>
64+
65+
<ul>
66+
<li><code>functions&nbsp;is an array of functions that returns promises</code></li>
67+
<li><code>1 &lt;= functions.length &lt;= 10</code></li>
68+
</ul>
69+
70+
## 解法
71+
72+
<!-- 这里可写通用的实现逻辑 -->
73+
74+
<!-- tabs:start -->
75+
76+
### **TypeScript**
77+
78+
<!-- 这里可写当前语言的特殊实现逻辑 -->
79+
80+
```ts
81+
82+
```
83+
84+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# [2721. Execute Asynchronous Functions in Parallel](https://leetcode.com/problems/execute-asynchronous-functions-in-parallel)
2+
3+
[中文文档](/solution/2700-2799/2721.Execute%20Asynchronous%20Functions%20in%20Parallel/README.md)
4+
5+
## Description
6+
7+
<p>Given an array of asynchronous&nbsp;functions&nbsp;<code>functions</code>, return a new promise <code>promise</code>. Each function in the array accepts no arguments&nbsp;and returns a promise.</p>
8+
9+
<p><code>promise</code> resolves:</p>
10+
11+
<ul>
12+
<li>When all the promises returned from&nbsp;<code>functions</code>&nbsp;were resolved successfully.&nbsp;The resolved&nbsp;value of&nbsp;<code>promise</code> should be an array of all the resolved values of promises in the same order as they were in the&nbsp;<code>functions.</code></li>
13+
</ul>
14+
15+
<p><code>promise</code> rejects:</p>
16+
17+
<ul>
18+
<li>When any&nbsp;of the promises&nbsp;returned from&nbsp;<code>functions</code>&nbsp;were rejected.&nbsp;<code>promise</code> should also&nbsp;reject&nbsp;with the reason of the first rejection.</li>
19+
</ul>
20+
21+
<p>Please solve it without using the built-in&nbsp;<code>Promise.all</code>&nbsp;function.</p>
22+
23+
<p>&nbsp;</p>
24+
<p><strong class="example">Example 1:</strong></p>
25+
26+
<pre>
27+
<strong>Input:</strong> functions = [
28+
&nbsp; () =&gt; new Promise(resolve =&gt; setTimeout(() =&gt; resolve(5), 200))
29+
]
30+
<strong>Output:</strong> {&quot;t&quot;: 200, &quot;resolved&quot;: [5]}
31+
<strong>Explanation:</strong>
32+
promiseAll(functions).then(console.log); // [5]
33+
34+
The single function was resolved at 200ms with a value of 5.
35+
</pre>
36+
37+
<p><strong class="example">Example 2:</strong></p>
38+
39+
<pre>
40+
<strong>Input:</strong> functions = [
41+
() =&gt; new Promise(resolve =&gt; setTimeout(() =&gt; resolve(1), 200)),
42+
() =&gt; new Promise((resolve, reject) =&gt; setTimeout(() =&gt; reject(&quot;Error&quot;), 100))
43+
]
44+
<strong>Output:</strong> {&quot;t&quot;: 100, &quot;rejected&quot;: &quot;Error&quot;}
45+
<strong>Explanation:</strong> Since one of the promises rejected, the returned promise also rejected with the same error at the same time.
46+
</pre>
47+
48+
<p><strong class="example">Example 3:</strong></p>
49+
50+
<pre>
51+
<strong>Input:</strong> functions = [
52+
() =&gt; new Promise(resolve =&gt; setTimeout(() =&gt; resolve(4), 50)),
53+
() =&gt; new Promise(resolve =&gt; setTimeout(() =&gt; resolve(10), 150)),
54+
() =&gt; new Promise(resolve =&gt; setTimeout(() =&gt; resolve(16), 100))
55+
]
56+
<strong>Output:</strong> {&quot;t&quot;: 150, &quot;resolved&quot;: [4, 10, 16]}
57+
<strong>Explanation:</strong> All the promises resolved with a value. The returned promise resolved when the last promise resolved.
58+
</pre>
59+
60+
<p>&nbsp;</p>
61+
<p><strong>Constraints:</strong></p>
62+
63+
<ul>
64+
<li><code>functions&nbsp;is an array of functions that returns promises</code></li>
65+
<li><code>1 &lt;= functions.length &lt;= 10</code></li>
66+
</ul>
67+
68+
## Solutions
69+
70+
<!-- tabs:start -->
71+
72+
### **TypeScript**
73+
74+
```ts
75+
76+
```
77+
78+
<!-- tabs:end -->

0 commit comments

Comments
 (0)