Skip to content

Commit

Permalink
feat: add weekly contest 392 (#2549)
Browse files Browse the repository at this point in the history
  • Loading branch information
yanglbme authored Apr 7, 2024
1 parent 51d9ecf commit d7593ed
Show file tree
Hide file tree
Showing 26 changed files with 855 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,31 @@

<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 @@ -41,33 +43,40 @@ 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> 编写一个SQL查询以查找以下内容:</p>
<p>为每张发票 <code>invoice_id</code> 编写一个查询方案以查找以下内容:</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>:可信联系人的数量:既是该顾客的联系人又是商店顾客的联系人数量(即:可信联系人的电子邮件存在于客户表中)。</li>
<li><code>contacts_cnt</code>:该顾客的联系人数量</li>
<li><code>trusted_contacts_cnt</code>:可信联系人的数量:既是该顾客的联系人又是商店顾客的联系人数量(即:可信联系人的电子邮件存在于 <meta charset="UTF-8" />&nbsp;<code>Customers</code>&nbsp;表中)。</li>
</ul>

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

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

<p>&nbsp;</p>

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

<pre><code>Customers</code> table:
<pre>
<strong>输入:</strong>
<code>Customers</code> table:
+-------------+---------------+--------------------+
| customer_id | customer_name | email |
+-------------+---------------+--------------------+
Expand Down Expand Up @@ -98,7 +107,7 @@ Invoices table:
| 55 | 500 | 13 |
| 44 | 60 | 6 |
+------------+-------+---------+
Result table:
<strong>输出:</strong>
+------------+---------------+-------+--------------+----------------------+
| invoice_id | customer_name | price | contacts_cnt | trusted_contacts_cnt |
+------------+---------------+-------+--------------+----------------------+
Expand All @@ -109,6 +118,7 @@ Result 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 primary key for this table.
customer_id is the column of unique values 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 for this table.
(user_id, contact_email) is the primary key (combination of columns with unique values) 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 primary key for this table.
invoice_id is the column of unique values 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 an SQL query to find the following for each <code>invoice_id</code>:</p>
<p>Write a solution 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 query result format is in the following example.</p>
<p>The 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
@@ -1,4 +1,4 @@
# [1918. 第 K 小的子数组和·](https://leetcode.cn/problems/kth-smallest-subarray-sum)
# [1918. 第 K 小的子数组和](https://leetcode.cn/problems/kth-smallest-subarray-sum)

[English Version](/solution/1900-1999/1918.Kth%20Smallest%20Subarray%20Sum/README_EN.md)

Expand Down
42 changes: 9 additions & 33 deletions solution/2800-2899/2826.Sorting Three Groups/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,7 @@

<!-- 这里写题目描述 -->

<p>给你一个下标从 <strong>0</strong>&nbsp;开始长度为 <code>n</code>&nbsp;的整数数组&nbsp;<code>nums</code>&nbsp;。<br />
<br />
&nbsp;<code>0</code>&nbsp;&nbsp;<code>n - 1</code>&nbsp;的数字被分为编号从&nbsp;<code>1</code>&nbsp;&nbsp;<code>3</code>&nbsp;的三个组,数字&nbsp;<code>i</code>&nbsp;属于组&nbsp;<code>nums[i]</code>&nbsp;。注意,有的组可能是&nbsp;<strong>空的</strong>&nbsp;。<br />
<br />
你可以执行以下操作任意次:</p>

<ul>
<li>选择数字&nbsp;<code>x</code>&nbsp;并改变它的组。更正式的,你可以将&nbsp;<code>nums[x]</code>&nbsp;改为数字&nbsp;<code>1</code>&nbsp;到&nbsp;<code>3</code>&nbsp;中的任意一个。</li>
</ul>

<p>你将按照以下过程构建一个新的数组&nbsp;<code>res</code>&nbsp;:</p>

<ol>
<li>将每个组中的数字分别排序。</li>
<li>将组&nbsp;<code>1</code>&nbsp;,<code>2</code>&nbsp;和&nbsp;<code>3</code>&nbsp;中的元素&nbsp;<strong>依次</strong>&nbsp;连接以得到&nbsp;<code>res</code>&nbsp;。</li>
</ol>

<p>如果得到的&nbsp;<code>res</code>&nbsp;是 <strong>非递减</strong>顺序的,那么我们称数组&nbsp;<code>nums</code>&nbsp;是 <strong>美丽数组</strong>&nbsp;。</p>

<p>请你返回将<em>&nbsp;</em><code>nums</code>&nbsp;变为&nbsp;<strong>美丽数组</strong>&nbsp;需要的最少步数。</p>
<p>给你一个整数数组&nbsp;<code>nums</code>&nbsp;。<code>nums</code>&nbsp;的每个元素是 1,2 或 3。在每次操作中,你可以删除&nbsp;<code>nums</code>&nbsp;中的一个元素。返回使 nums 成为 <strong>非递减</strong>&nbsp;顺序所需操作数的 <strong>最小值</strong>。</p>

<p>&nbsp;</p>

Expand All @@ -36,33 +17,26 @@
<pre>
<b>输入:</b>nums = [2,1,3,2,1]
<b>输出:</b>3
<b>解释:</b>以下三步操作是最优方案:
1. 将 nums[0] 变为 1 。
2. 将 nums[2] 变为 1 。
3. 将 nums[3] 变为 1 。
执行以上操作后,将每组中的数字排序,组 1 为 [0,1,2,3,4] ,组 2 和组 3 都为空。所以 res 等于 [0,1,2,3,4] ,它是非递减顺序的。
三步操作是最少需要的步数。
<b>解释:</b>
其中一个最优方案是删除 nums[0],nums[2] 和 nums[3]。
</pre>

<p><strong class="example">示例 2:</strong></p>

<pre>
<b>输入:</b>nums = [1,3,2,1,3,3]
<b>输出:</b>2
<b>解释:</b>以下两步操作是最优方案:
1. 将 nums[1] 变为 1 。
2. 将 nums[2] 变为 1 。
执行以上操作后,将每组中的数字排序,组 1 为 [0,1,2,3] ,组 2 为空,组 3 为 [4,5] 。所以 res 等于 [0,1,2,3,4,5] ,它是非递减顺序的。
两步操作是最少需要的步数。
<b>解释:</b>
其中一个最优方案是删除 nums[1] 和 nums[2]。
</pre>

<p><strong class="example">示例 3:</strong></p>

<pre>
<b>输入:</b>nums = [2,2,2,2,3,3]
<b>输出:</b>0
<b>解释:</b>不需要执行任何操作。
组 1 为空,组 2 为 [0,1,2,3] ,组 3 为 [4,5] 。所以 res 等于 [0,1,2,3,4,5] ,它是非递减顺序的
<b>解释:</b>
nums 已是非递减顺序的
</pre>

<p>&nbsp;</p>
Expand All @@ -74,6 +48,8 @@
<li><code>1 &lt;= nums[i] &lt;= 3</code></li>
</ul>

<p><strong>进阶:</strong>你可以使用&nbsp;<code>O(n)</code>&nbsp;时间复杂度以内的算法解决吗?</p>

## 解法

### 方法一:动态规划
Expand Down
2 changes: 1 addition & 1 deletion solution/3000-3099/3087.Find Trending Hashtags/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ tweet_id 是这张表的主键 (值互不相同的列)。
这张表的每一行都包含 user_id, tweet_id, tweet_date 和 tweet。
</pre>

<p>编写一个解决方案来找到&nbsp;<code>2024</code>&nbsp;年 <strong>二月&nbsp;</strong>的 <strong>前</strong>&nbsp;<code>3</code>&nbsp;热门话题 <strong>标签</strong>。</p>
<p>编写一个解决方案来找到&nbsp;<code>2024</code>&nbsp;年 <strong>二月&nbsp;</strong>的 <strong>前</strong>&nbsp;<code>3</code>&nbsp;热门话题 <strong>标签</strong>。每条推文只包含一个标签。</p>

<p>返回结果表,根据标签的数量和标签&nbsp;<strong>降序</strong> 排序。</p>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# [3094. Guess the Number Using Bitwise Questions II](https://leetcode.cn/problems/guess-the-number-using-bitwise-questions-ii)
# [3094. 使用按位查询猜测数字 II](https://leetcode.cn/problems/guess-the-number-using-bitwise-questions-ii)

[English Version](/solution/3000-3099/3094.Guess%20the%20Number%20Using%20Bitwise%20Questions%20II/README_EN.md)

Expand All @@ -8,50 +8,52 @@

<!-- 这里写题目描述 -->

<p>There is a number <code>n</code> between <code>0</code> and <code>2<sup>30</sup> - 1</code> (both inclusive) that you have to find.</p>
<p>你需要找到一个在 <code>0</code> &nbsp;<code>2<sup>30</sup> - 1</code>&nbsp;(均包含)之间的数字 <code>n</code>。</p>

<p>There is a pre-defined API <code>int commonBits(int num)</code> that helps you with your mission. But here is the challenge, every time you call this function, <code>n</code> changes in some way. But keep in mind, that you have to find the <strong>initial value of </strong><code>n</code>.</p>
<p>有一个预定义的 API <code>int commonBits(int num)</code>&nbsp;能帮助你完成任务。但挑战是每次你调用这个函数,<code>n</code>&nbsp;都会以某种方式改变。但是记住,你需要找到的是<strong>&nbsp;</strong><code>n</code>&nbsp;的 <strong>初始值</strong>。</p>

<p><code>commonBits(int num)</code> acts as follows:</p>
<p><code>commonBits(int num)</code> 的操作如下:</p>

<ul>
<li>Calculate <code>count</code> which is the number of bits where both <code>n</code> and <code>num</code> have the same value in that position of their binary representation.</li>
<li>计算&nbsp;<code>n</code>&nbsp;和&nbsp;<code>num</code>&nbsp;的二进制表示中值相同的二进制位的位的数量&nbsp;<code>count</code></li>
<li><code>n = n XOR num</code></li>
<li>Return <code>count</code>.</li>
<li>返回&nbsp;<code>count</code></li>
</ul>

<p>Return <em>the number</em> <code>n</code>.</p>
<p>返回数字&nbsp;<code>n</code></p>

<p><strong>Note:</strong> In this world, all numbers are between <code>0</code> and <code>2<sup>30</sup> - 1</code> (both inclusive), thus for counting common bits, we see only the first 30 bits of those numbers.</p>
<p><strong>注意:</strong>在这个世界中,所有数字都在&nbsp;<code>0</code>&nbsp;&nbsp;<code>2<sup>30</sup> - 1</code>&nbsp;之间(均包含),因此在计算公共二进制位时,我们只看那些数字的前 30 个二进制位。</p>

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

<p><strong class="example">示例 1:</strong></p>

<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
<p><strong>Input: </strong> <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;"> n = 31 </span></p>
<p><strong>输入:</strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">n = 31 </span></p>

<p><strong>Output: </strong> <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;"> 31 </span></p>
<p><strong>输出:</strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">31 </span></p>

<p><strong>Explanation: </strong> It can be proven that it&#39;s possible to find 31 using the provided API.</p>
<p><strong>解释:</strong>可以证明,使用提供的 API 可以找到 31。</p>
</div>

<p><strong class="example">Example 2: </strong></p>
<p><strong class="example">示例 2:</strong></p>

<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
<p><strong>Input: </strong> <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;"> n = 33 </span></p>
<p><strong>输入:</strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">n = 33 </span></p>

<p><strong>Output: </strong> <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;"> 33 </span></p>
<p><strong>输出:</strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">33 </span></p>

<p><strong>Explanation: </strong> It can be proven that it&#39;s possible to find 33 using the provided API.</p>
<p><strong>解释:</strong>可以证明,使用提供的 API 可以找到 33。</p>
</div>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<p><strong>提示:</strong></p>

<ul>
<li><code>0 &lt;= n &lt;= 2<sup>30</sup> - 1</code></li>
<li><code>0 &lt;= num &lt;= 2<sup>30</sup> - 1</code></li>
<li>If you ask for some <code>num</code> out of the given range, the output wouldn&#39;t be reliable.</li>
<li>如果你查询的&nbsp;<code>num</code>&nbsp;超出了给定的范围,输出将会是不可靠的。</li>
</ul>

## 解法
Expand Down
37 changes: 19 additions & 18 deletions solution/3100-3199/3103.Find Trending Hashtags II/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# [3103. Find Trending Hashtags II](https://leetcode.cn/problems/find-trending-hashtags-ii)
# [3103. 查找热门话题标签 II](https://leetcode.cn/problems/find-trending-hashtags-ii)

[English Version](/solution/3100-3199/3103.Find%20Trending%20Hashtags%20II/README_EN.md)

Expand All @@ -8,7 +8,7 @@

<!-- 这里写题目描述 -->

<p>Table: <code>Tweets</code></p>
<p>表:<code>Tweets</code></p>

<pre>
+-------------+---------+
Expand All @@ -19,23 +19,24 @@
| tweet_date | date |
| tweet | varchar |
+-------------+---------+
tweet_id is the primary key (column with unique values) for this table.
Each row of this table contains user_id, tweet_id, tweet_date and tweet.
tweet_id 是这张表的主键 (值互不相同的列)。
这张表的每一行都包含 user_id, tweet_id, tweet_date tweet
</pre>

<p>Write a solution to find the <strong>top</strong> <code>3</code> trending <strong>hashtags</strong> in <strong>February</strong> <code>2024</code>. Every tweet may contain <strong>several</strong> <strong>hashtags</strong>.</p>
<p>编写一个解决方案来找到&nbsp;<code>2024</code>&nbsp;<strong>二月&nbsp;</strong><strong></strong>&nbsp;<code>3</code>&nbsp;热门话题 <strong>标签</strong>。每条推文可能含有 <strong>几个标签</strong></p>

<p>Return <em>the result table orderd by count of hashtag, hashtag in </em><strong>descending</strong><em> order.</em></p>
<p>返回结果表,根据标签的数量和标签&nbsp;<strong>降序</strong> 排序。</p>

<p>The result format is in the following example.</p>
<p>结果格式如下所示。</p>

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

<p><strong class="example">示例 1:</strong></p>

<div class="example-block">
<p><strong>Input:</strong></p>
<p><strong>输入:</strong></p>

<p>Tweets table:</p>
<p>Tweets 表:</p>

<pre class="example-io">
+---------+----------+------------------------------------------------------------+------------+
Expand All @@ -45,13 +46,13 @@ Each row of this table contains user_id, tweet_id, tweet_date and tweet.
| 136 | 14 | Another #HappyDay with good vibes! #FeelGood | 2024-02-03 |
| 137 | 15 | Productivity peaks! #WorkLife #ProductiveDay | 2024-02-04 |
| 138 | 16 | Exploring new tech frontiers. #TechLife #Innovation | 2024-02-04 |
| 139 | 17 | Gratitude for today&#39;s moments. #HappyDay #Thankful | 2024-02-05 |
| 139 | 17 | Gratitude for today's moments. #HappyDay #Thankful | 2024-02-05 |
| 140 | 18 | Innovation drives us. #TechLife #FutureTech | 2024-02-07 |
| 141 | 19 | Connecting with nature&#39;s serenity. #Nature #Peaceful | 2024-02-09 |
| 141 | 19 | Connecting with nature's serenity. #Nature #Peaceful | 2024-02-09 |
+---------+----------+------------------------------------------------------------+------------+
</pre>

<p><strong>Output:</strong></p>
<p><strong>输出:</strong></p>

<pre class="example-io">
+-----------+-------+
Expand All @@ -64,15 +65,15 @@ Each row of this table contains user_id, tweet_id, tweet_date and tweet.

</pre>

<p><strong>Explanation:</strong></p>
<p><strong>解释:</strong></p>

<ul>
<li><strong>#HappyDay:</strong> Appeared in tweet IDs 13, 14, and 17, with a total count of 3 mentions.</li>
<li><strong>#TechLife:</strong> Appeared in tweet IDs 16 and 18, with a total count of 2 mentions.</li>
<li><strong>#WorkLife:</strong> Appeared in tweet ID 15, with a total count of 1 mention.</li>
<li><strong>#HappyDay</strong>在 ID 为 13,14,17 的推文中出现,总共提及&nbsp;3 次。</li>
<li><strong>#TechLife</strong>在 ID 为 16,18 的推文中出现,总共提及 2&nbsp;次。</li>
<li><strong>#WorkLife</strong>ID 为 15 的推文中出现,总共提及 1&nbsp;次。</li>
</ul>

<p><b>Note:</b> Output table is sorted in descending order by count and hashtag respectively.</p>
<p><b>注意:</b>输出表分别按 count hashtag 降序排序。</p>
</div>

## 解法
Expand Down
Loading

0 comments on commit d7593ed

Please sign in to comment.