Skip to content

Commit 89db25c

Browse files
authored
feat: add new lc problems and solutions (doocs#2541)
1 parent e347d8d commit 89db25c

File tree

31 files changed

+1521
-765
lines changed

31 files changed

+1521
-765
lines changed

package-lock.json

+702-549
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

solution/1300-1399/1364.Number of Trusted Contacts of a Customer/README.md

+13-23
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,29 @@
1010

1111
<p>顾客表:<code>Customers</code></p>
1212

13-
<pre>
14-
+---------------+---------+
13+
<pre>+---------------+---------+
1514
| Column Name | Type |
1615
+---------------+---------+
1716
| customer_id | int |
1817
| customer_name | varchar |
1918
| email | varchar |
2019
+---------------+---------+
21-
customer_id 是这张表具有唯一值的列
20+
customer_id 是这张表的主键
2221
此表的每一行包含了某在线商店顾客的姓名和电子邮件。
2322
</pre>
2423

2524
<p>&nbsp;</p>
2625

2726
<p>联系方式表:<code>Contacts</code></p>
2827

29-
<pre>
30-
+---------------+---------+
28+
<pre>+---------------+---------+
3129
| Column Name | Type |
3230
+---------------+---------+
3331
| user_id | id |
3432
| contact_name | varchar |
3533
| contact_email | varchar |
3634
+---------------+---------+
37-
(user_id, contact_email) 是这张表的主键(具有唯一值的列的组合)
35+
(user_id, contact_email) 是这张表的主键。
3836
此表的每一行表示编号为 user_id 的顾客的某位联系人的姓名和电子邮件。
3937
此表包含每位顾客的联系人信息,但顾客的联系人不一定存在于顾客表中。
4038
</pre>
@@ -43,40 +41,33 @@ customer_id 是这张表具有唯一值的列。
4341

4442
<p>发票表:<code>Invoices</code></p>
4543

46-
<pre>
47-
+--------------+---------+
44+
<pre>+--------------+---------+
4845
| Column Name | Type |
4946
+--------------+---------+
5047
| invoice_id | int |
5148
| price | int |
5249
| user_id | int |
5350
+--------------+---------+
54-
invoice_id 是这张表具有唯一值的列
51+
invoice_id 是这张表的主键
5552
此表的每一行分别表示编号为 user_id 的顾客拥有有一张编号为 invoice_id、价格为 price 的发票。
5653
</pre>
5754

5855
<p>&nbsp;</p>
5956

60-
<p>为每张发票 <code>invoice_id</code> 编写一个查询方案以查找以下内容:</p>
57+
<p>为每张发票 <code>invoice_id</code> 编写一个SQL查询以查找以下内容:</p>
6158

6259
<ul>
6360
<li><code>customer_name</code>:与发票相关的顾客名称。</li>
6461
<li><code>price</code>:发票的价格。</li>
65-
<li><code>contacts_cnt</code>:该顾客的联系人数量</li>
66-
<li><code>trusted_contacts_cnt</code>:可信联系人的数量:既是该顾客的联系人又是商店顾客的联系人数量(即:可信联系人的电子邮件存在于 <meta charset="UTF-8" />&nbsp;<code>Customers</code>&nbsp;表中)。</li>
62+
<li><code>contacts_cnt</code>:该顾客的联系人数量</li>
63+
<li><code>trusted_contacts_cnt</code>:可信联系人的数量:既是该顾客的联系人又是商店顾客的联系人数量(即:可信联系人的电子邮件存在于客户表中)。</li>
6764
</ul>
6865

69-
<p>返回结果按照&nbsp;<code>invoice_id</code>&nbsp;<strong>排序</strong>。</p>
70-
71-
<p>结果的格式如下例所示。</p>
72-
73-
<p>&nbsp;</p>
66+
<p>将查询的结果按照&nbsp;<code>invoice_id</code>&nbsp;排序。</p>
7467

75-
<p><strong>示例 1:</strong></p>
68+
<p>查询结果的格式如下例所示:</p>
7669

77-
<pre>
78-
<strong>输入:</strong>
79-
<code>Customers</code> table:
70+
<pre><code>Customers</code> table:
8071
+-------------+---------------+--------------------+
8172
| customer_id | customer_name | email |
8273
+-------------+---------------+--------------------+
@@ -107,7 +98,7 @@ Invoices table:
10798
| 55 | 500 | 13 |
10899
| 44 | 60 | 6 |
109100
+------------+-------+---------+
110-
<strong>输出:</strong>
101+
Result table:
111102
+------------+---------------+-------+--------------+----------------------+
112103
| invoice_id | customer_name | price | contacts_cnt | trusted_contacts_cnt |
113104
+------------+---------------+-------+--------------+----------------------+
@@ -118,7 +109,6 @@ Invoices table:
118109
| 88 | Alice | 200 | 3 | 2 |
119110
| 99 | Bob | 300 | 2 | 0 |
120111
+------------+---------------+-------+--------------+----------------------+
121-
<strong>解释:</strong>
122112
Alice 有三位联系人,其中两位(Bob 和 John)是可信联系人。
123113
Bob 有两位联系人, 他们中的任何一位都不是可信联系人。
124114
Alex 只有一位联系人(Alice),并是一位可信联系人。

solution/1300-1399/1364.Number of Trusted Contacts of a Customer/README_EN.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
| customer_name | varchar |
1717
| email | varchar |
1818
+---------------+---------+
19-
customer_id is the column of unique values for this table.
19+
customer_id is the primary key for this table.
2020
Each row of this table contains the name and the email of a customer of an online shop.
2121
</pre>
2222

@@ -32,7 +32,7 @@ Each row of this table contains the name and the email of a customer of an onlin
3232
| contact_name | varchar |
3333
| contact_email | varchar |
3434
+---------------+---------+
35-
(user_id, contact_email) is the primary key (combination of columns with unique values) for this table.
35+
(user_id, contact_email) is the primary key for this table.
3636
Each row of this table contains the name and email of one contact of customer with user_id.
3737
This table contains information about people each customer trust. The contact may or may not exist in the Customers table.
3838
</pre>
@@ -49,13 +49,13 @@ This table contains information about people each customer trust. The contact ma
4949
| price | int |
5050
| user_id | int |
5151
+--------------+---------+
52-
invoice_id is the column of unique values for this table.
52+
invoice_id is the primary key for this table.
5353
Each row of this table indicates that user_id has an invoice with invoice_id and a price.
5454
</pre>
5555

5656
<p>&nbsp;</p>
5757

58-
<p>Write a solution to find the following for each <code>invoice_id</code>:</p>
58+
<p>Write an SQL query to find the following for each <code>invoice_id</code>:</p>
5959

6060
<ul>
6161
<li><code>customer_name</code>: The name of the customer the invoice is related to.</li>
@@ -66,7 +66,7 @@ Each row of this table indicates that user_id has an invoice with invoice_id and
6666

6767
<p>Return the result table <strong>ordered</strong> by <code>invoice_id</code>.</p>
6868

69-
<p>The result format is in the following example.</p>
69+
<p>The query result format is in the following example.</p>
7070

7171
<p>&nbsp;</p>
7272
<p><strong class="example">Example 1:</strong></p>

solution/1600-1699/1668.Maximum Repeating Substring/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[English Version](/solution/1600-1699/1668.Maximum%20Repeating%20Substring/README_EN.md)
44

5-
<!-- tags:字符串,字符串匹配 -->
5+
<!-- tags:字符串,动态规划,字符串匹配 -->
66

77
## 题目描述
88

solution/1600-1699/1668.Maximum Repeating Substring/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[中文文档](/solution/1600-1699/1668.Maximum%20Repeating%20Substring/README.md)
44

5-
<!-- tags:String,String Matching -->
5+
<!-- tags:String,Dynamic Programming,String Matching -->
66

77
## Description
88

solution/1700-1799/1738.Find Kth Largest XOR Coordinate Value/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[English Version](/solution/1700-1799/1738.Find%20Kth%20Largest%20XOR%20Coordinate%20Value/README_EN.md)
44

5-
<!-- tags:位运算,数组,分治,矩阵,前缀和,快速选择,堆(优先队列) -->
5+
<!-- tags:位运算,数组,分治,矩阵,前缀和,快速选择,排序,堆(优先队列) -->
66

77
## 题目描述
88

solution/1700-1799/1738.Find Kth Largest XOR Coordinate Value/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[中文文档](/solution/1700-1799/1738.Find%20Kth%20Largest%20XOR%20Coordinate%20Value/README.md)
44

5-
<!-- tags:Bit Manipulation,Array,Divide and Conquer,Matrix,Prefix Sum,Quickselect,Heap (Priority Queue) -->
5+
<!-- tags:Bit Manipulation,Array,Divide and Conquer,Matrix,Prefix Sum,Quickselect,Sorting,Heap (Priority Queue) -->
66

77
## Description
88

solution/2000-2099/2009.Minimum Number of Operations to Make Array Continuous/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[English Version](/solution/2000-2099/2009.Minimum%20Number%20of%20Operations%20to%20Make%20Array%20Continuous/README_EN.md)
44

5-
<!-- tags:数组,二分查找 -->
5+
<!-- tags:数组,哈希表,二分查找,滑动窗口 -->
66

77
## 题目描述
88

solution/2000-2099/2009.Minimum Number of Operations to Make Array Continuous/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[中文文档](/solution/2000-2099/2009.Minimum%20Number%20of%20Operations%20to%20Make%20Array%20Continuous/README.md)
44

5-
<!-- tags:Array,Binary Search -->
5+
<!-- tags:Array,Hash Table,Binary Search,Sliding Window -->
66

77
## Description
88

solution/2600-2699/2681.Power of Heroes/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[English Version](/solution/2600-2699/2681.Power%20of%20Heroes/README_EN.md)
44

5-
<!-- tags:数组,数学,前缀和,排序 -->
5+
<!-- tags:数组,数学,动态规划,前缀和,排序 -->
66

77
## 题目描述
88

solution/2600-2699/2681.Power of Heroes/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[中文文档](/solution/2600-2699/2681.Power%20of%20Heroes/README.md)
44

5-
<!-- tags:Array,Math,Prefix Sum,Sorting -->
5+
<!-- tags:Array,Math,Dynamic Programming,Prefix Sum,Sorting -->
66

77
## Description
88

solution/2800-2899/2826.Sorting Three Groups/README_EN.md

+29-43
Original file line numberDiff line numberDiff line change
@@ -6,61 +6,44 @@
66

77
## Description
88

9-
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>.<br />
10-
<br />
11-
The numbers from <code>0</code> to <code>n - 1</code> are divided into three groups numbered from <code>1</code> to <code>3</code>, where number <code>i</code> belongs to group <code>nums[i]</code>. Notice that some groups may be <strong>empty</strong>.<br />
12-
<br />
13-
You are allowed to perform this operation any number of times:</p>
9+
<p>You are given an integer array <code>nums</code>. Each element in <code>nums</code> is 1, 2 or 3. In each operation, you can remove an element from&nbsp;<code>nums</code>. Return the <strong>minimum</strong> number of operations to make <code>nums</code> <strong>non-decreasing</strong>.</p>
1410

15-
<ul>
16-
<li>Pick number <code>x</code> and change its group. More formally, change <code>nums[x]</code> to any number from <code>1</code> to <code>3</code>.</li>
17-
</ul>
11+
<p>&nbsp;</p>
12+
<p><strong class="example">Example 1:</strong></p>
1813

19-
<p>A new array <code>res</code> is constructed using the following procedure:</p>
14+
<div class="example-block">
15+
<p><strong>Input:</strong> <span class="example-io">nums = [2,1,3,2,1]</span></p>
2016

21-
<ol>
22-
<li>Sort the numbers in each group independently.</li>
23-
<li>Append the elements of groups <code>1</code>, <code>2</code>, and <code>3</code> to <code>res</code> <strong>in this order</strong>.</li>
24-
</ol>
17+
<p><strong>Output:</strong> <span class="example-io">3</span></p>
2518

26-
<p>Array <code>nums</code> is called a <strong>beautiful array</strong> if the constructed array <code>res</code> is sorted in <strong>non-decreasing</strong> order.</p>
19+
<p><strong>Explanation:</strong></p>
2720

28-
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> a <strong>beautiful array</strong></em>.</p>
21+
<p>One of the optimal solutions is to remove <code>nums[0]</code>, <code>nums[2]</code> and <code>nums[3]</code>.</p>
22+
</div>
2923

30-
<p>&nbsp;</p>
31-
<p><strong class="example">Example 1:</strong></p>
24+
<p><strong class="example">Example 2:</strong></p>
3225

33-
<pre>
34-
<strong>Input:</strong> nums = [2,1,3,2,1]
35-
<strong>Output:</strong> 3
36-
<strong>Explanation:</strong> It&#39;s optimal to perform three operations:
37-
1. change nums[0] to 1.
38-
2. change nums[2] to 1.
39-
3. change nums[3] to 1.
40-
After performing the operations and sorting the numbers in each group, group 1 becomes equal to [0,1,2,3,4] and group 2 and group 3 become empty. Hence, res is equal to [0,1,2,3,4] which is sorted in non-decreasing order.
41-
It can be proven that there is no valid sequence of less than three operations.
42-
</pre>
26+
<div class="example-block">
27+
<p><strong>Input:</strong> <span class="example-io">nums = [1,3,2,1,3,3]</span></p>
4328

44-
<p><strong class="example">Example 2:</strong></p>
29+
<p><strong>Output:</strong> <span class="example-io">2</span></p>
30+
31+
<p><strong>Explanation:</strong></p>
4532

46-
<pre>
47-
<strong>Input:</strong> nums = [1,3,2,1,3,3]
48-
<strong>Output:</strong> 2
49-
<strong>Explanation:</strong> It&#39;s optimal to perform two operations:
50-
1. change nums[1] to 1.
51-
2. change nums[2] to 1.
52-
After performing the operations and sorting the numbers in each group, group 1 becomes equal to [0,1,2,3], group 2 becomes empty, and group 3 becomes equal to [4,5]. Hence, res is equal to [0,1,2,3,4,5] which is sorted in non-decreasing order.
53-
It can be proven that there is no valid sequence of less than two operations.
54-
</pre>
33+
<p>One of the optimal solutions is to remove <code>nums[1]</code> and <code>nums[2]</code>.</p>
34+
</div>
5535

5636
<p><strong class="example">Example 3:</strong></p>
5737

58-
<pre>
59-
<strong>Input:</strong> nums = [2,2,2,2,3,3]
60-
<strong>Output:</strong> 0
61-
<strong>Explanation:</strong> It&#39;s optimal to not perform operations.
62-
After sorting the numbers in each group, group 1 becomes empty, group 2 becomes equal to [0,1,2,3] and group 3 becomes equal to [4,5]. Hence, res is equal to [0,1,2,3,4,5] which is sorted in non-decreasing order.
63-
</pre>
38+
<div class="example-block">
39+
<p><strong>Input:</strong> <span class="example-io">nums = [2,2,2,2,3,3]</span></p>
40+
41+
<p><strong>Output:</strong> <span class="example-io">0</span></p>
42+
43+
<p><strong>Explanation:</strong></p>
44+
45+
<p><code>nums</code> is already non-decreasing.</p>
46+
</div>
6447

6548
<p>&nbsp;</p>
6649
<p><strong>Constraints:</strong></p>
@@ -70,6 +53,9 @@ After sorting the numbers in each group, group 1 becomes empty, group 2 becomes
7053
<li><code>1 &lt;= nums[i] &lt;= 3</code></li>
7154
</ul>
7255

56+
<p>&nbsp;</p>
57+
<strong>Follow-up:</strong> Can you come up with an algorithm that runs in <code>O(n)</code> time complexity?
58+
7359
## Solutions
7460

7561
### Solution 1: Dynamic Programming

0 commit comments

Comments
 (0)