Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add new lc problems and solutions #2541

Merged
merged 1 commit into from
Apr 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,251 changes: 702 additions & 549 deletions package-lock.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,29 @@

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

<pre>
+---------------+---------+
<pre>+---------------+---------+
| Column Name | Type |
+---------------+---------+
| customer_id | int |
| customer_name | varchar |
| email | varchar |
+---------------+---------+
customer_id 是这张表具有唯一值的列
customer_id 是这张表的主键
此表的每一行包含了某在线商店顾客的姓名和电子邮件。
</pre>

<p>&nbsp;</p>

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

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

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

<pre>
+--------------+---------+
<pre>+--------------+---------+
| Column Name | Type |
+--------------+---------+
| invoice_id | int |
| price | int |
| user_id | int |
+--------------+---------+
invoice_id 是这张表具有唯一值的列
invoice_id 是这张表的主键
此表的每一行分别表示编号为 user_id 的顾客拥有有一张编号为 invoice_id、价格为 price 的发票。
</pre>

<p>&nbsp;</p>

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

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

<p>返回结果按照&nbsp;<code>invoice_id</code>&nbsp;<strong>排序</strong>。</p>

<p>结果的格式如下例所示。</p>

<p>&nbsp;</p>
<p>将查询的结果按照&nbsp;<code>invoice_id</code>&nbsp;排序。</p>

<p><strong>示例 1:</strong></p>
<p>查询结果的格式如下例所示:</p>

<pre>
<strong>输入:</strong>
<code>Customers</code> table:
<pre><code>Customers</code> table:
+-------------+---------------+--------------------+
| customer_id | customer_name | email |
+-------------+---------------+--------------------+
Expand Down Expand Up @@ -107,7 +98,7 @@ Invoices table:
| 55 | 500 | 13 |
| 44 | 60 | 6 |
+------------+-------+---------+
<strong>输出:</strong>
Result table:
+------------+---------------+-------+--------------+----------------------+
| invoice_id | customer_name | price | contacts_cnt | trusted_contacts_cnt |
+------------+---------------+-------+--------------+----------------------+
Expand All @@ -118,7 +109,6 @@ Invoices table:
| 88 | Alice | 200 | 3 | 2 |
| 99 | Bob | 300 | 2 | 0 |
+------------+---------------+-------+--------------+----------------------+
<strong>解释:</strong>
Alice 有三位联系人,其中两位(Bob 和 John)是可信联系人。
Bob 有两位联系人, 他们中的任何一位都不是可信联系人。
Alex 只有一位联系人(Alice),并是一位可信联系人。
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
| customer_name | varchar |
| email | varchar |
+---------------+---------+
customer_id is the column of unique values for this table.
customer_id is the primary key for this table.
Each row of this table contains the name and the email of a customer of an online shop.
</pre>

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

<p>&nbsp;</p>

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

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

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

<p>The result format is in the following example.</p>
<p>The query result format is in the following example.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

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

## 题目描述

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

<!-- tags:String,String Matching -->
<!-- tags:String,Dynamic Programming,String Matching -->

## Description

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

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

## 题目描述

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

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

## Description

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

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

## 题目描述

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

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

## Description

Expand Down
2 changes: 1 addition & 1 deletion solution/2600-2699/2681.Power of Heroes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

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

## 题目描述

Expand Down
2 changes: 1 addition & 1 deletion solution/2600-2699/2681.Power of Heroes/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

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

## Description

Expand Down
72 changes: 29 additions & 43 deletions solution/2800-2899/2826.Sorting Three Groups/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,61 +6,44 @@

## Description

<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>.<br />
<br />
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 />
<br />
You are allowed to perform this operation any number of times:</p>
<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>

<ul>
<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>
</ul>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

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

<ol>
<li>Sort the numbers in each group independently.</li>
<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>
</ol>
<p><strong>Output:</strong> <span class="example-io">3</span></p>

<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>
<p><strong>Explanation:</strong></p>

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

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

<pre>
<strong>Input:</strong> nums = [2,1,3,2,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> It&#39;s optimal to perform three operations:
1. change nums[0] to 1.
2. change nums[2] to 1.
3. change nums[3] to 1.
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.
It can be proven that there is no valid sequence of less than three operations.
</pre>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,3,2,1,3,3]</span></p>

<p><strong class="example">Example 2:</strong></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>

<p><strong>Explanation:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,3,2,1,3,3]
<strong>Output:</strong> 2
<strong>Explanation:</strong> It&#39;s optimal to perform two operations:
1. change nums[1] to 1.
2. change nums[2] to 1.
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.
It can be proven that there is no valid sequence of less than two operations.
</pre>
<p>One of the optimal solutions is to remove <code>nums[1]</code> and <code>nums[2]</code>.</p>
</div>

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

<pre>
<strong>Input:</strong> nums = [2,2,2,2,3,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> It&#39;s optimal to not perform operations.
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.
</pre>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,2,2,2,3,3]</span></p>

<p><strong>Output:</strong> <span class="example-io">0</span></p>

<p><strong>Explanation:</strong></p>

<p><code>nums</code> is already non-decreasing.</p>
</div>

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

<p>&nbsp;</p>
<strong>Follow-up:</strong> Can you come up with an algorithm that runs in <code>O(n)</code> time complexity?

## Solutions

### Solution 1: Dynamic Programming
Expand Down
Loading
Loading