Skip to content

chore: update lc problems #1468

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

Merged
merged 1 commit into from
Aug 19, 2023
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
Original file line number Diff line number Diff line change
@@ -1,72 +1,75 @@
# [2814. Minimum Time Takes to Reach Destination Without Drowning](https://leetcode.cn/problems/minimum-time-takes-to-reach-destination-without-drowning)
# [2814. 避免淹死并到达目的地的最短时间](https://leetcode.cn/problems/minimum-time-takes-to-reach-destination-without-drowning)

[English Version](/solution/2800-2899/2814.Minimum%20Time%20Takes%20to%20Reach%20Destination%20Without%20Drowning/README_EN.md)

## 题目描述

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

<p>You are given an <code>n * m</code> <strong>0-indexed</strong> grid of string <code>land</code>. Right now, you are standing at the cell that contains <code>&quot;S&quot;</code>, and you want to get to the cell containing <code>&quot;D&quot;</code>. There are three other types of cells in this land:</p>
<p>现给定一个 <code>n * m</code> 的索引从 <strong>0</strong> 开始的二维字符串网格 <code>land</code>,目前你站在为&nbsp;<code>"S"</code> 的单元格上,你需要到达为&nbsp;<code>"D"</code> 的单元格。在这片区域上还有另外三种类型的单元格:</p>

<ul>
<li><code>&quot;.&quot;</code>: These cells are empty.</li>
<li><code>&quot;X&quot;</code>: These cells are stone.</li>
<li><code>&quot;*&quot;</code>: These cells are flooded.</li>
<li><code>"."</code>:这些单元格是空的。</li>
<li><code>"X"</code>:这些单元格是石头。</li>
<li><code>"*"</code>:这些单元格被淹没了。</li>
</ul>

<p>At each second, you can move to a cell that shares a side with your current cell (if it exists). Also, at each second, every <strong>empty cell</strong> that shares a side with a flooded cell becomes flooded as well.<br />
There are two problems ahead of your journey:</p>
<p>每秒钟,你可以移动到与当前单元格共享边的单元格(如果它存在)。此外,每秒钟,与被淹没的单元格共享边的每个 <strong>空单元格</strong> 也会被淹没。</p>

<p>在你的旅程中,有两个需要注意的问题:</p>

<ul>
<li>You can&#39;t step on stone cells.</li>
<li>You can&#39;t step on flooded cells since you will drown (also, you can&#39;t step on a cell that will be flooded at the same time as you step on it).</li>
<li>你不能踩在石头单元格上。</li>
<li>你不能踩在被淹没的单元格上,因为你会淹死(同时,你也不能踩在在你踩上时会被淹没的单元格上)。</li>
</ul>

<p>Return<em> the <strong>minimum</strong> time it takes you to reach the destination in seconds, or </em><code>-1</code><em> if it is impossible.</em></p>
<p>返回从起始位置到达目标位置所需的 <strong>最小</strong> 时间(以秒为单位),如果不可能达到目标位置,则返回 <code>-1</code></p>

<p><strong>Note</strong> that the destination will never be flooded.</p>
<p><strong>注意</strong>,目标位置永远不会被淹没。</p>

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

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

<pre>
<strong>Input:</strong> land = [[&quot;D&quot;,&quot;.&quot;,&quot;*&quot;],[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;S&quot;,&quot;.&quot;]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>The picture below shows the simulation of the land second by second. The blue cells are flooded, and the gray cells are stone.
Picture (0) shows the initial state and picture (3) shows the final state when we reach destination. As you see, it takes us 3 second to reach destination and the answer would be 3.
It can be shown that 3 is the minimum time needed to reach from S to D.
<b>输入:</b>land = [["D",".","*"],[".",".","."],[".","S","."]]
<b>输出:</b>3
<strong>解释:</strong>下面的图片逐秒模拟了土地的变化。蓝色的单元格被淹没,灰色的单元格是石头。
图片(0)显示了初始状态,图片(3)显示了当我们到达目标时的最终状态。正如你所看到的,我们需要 3 秒才能到达目标位置,答案是 3。
可以证明 3 是从 S 到 D 所需的最小时间。
</pre>

<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2800-2899/2814.Minimum%20Time%20Takes%20to%20Reach%20Destination%20Without%20Drowning/images/ex1.png" style="padding: 5px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 600px; height: 111px;" /></p>

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

<pre>
<strong>Input:</strong> land = [[&quot;D&quot;,&quot;X&quot;,&quot;*&quot;],[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;.&quot;,&quot;S&quot;]]
<strong>Output:</strong> -1
<strong>Explanation:</strong> The picture below shows the simulation of the land second by second. The blue cells are flooded, and the gray cells are stone.
Picture (0) shows the initial state. As you see, no matter which paths we choose, we will drown at the 3<sup>rd</sup>&nbsp;second. Also the minimum path takes us 4 seconds to reach from S to D.
So the answer would be -1.
<b>输入:</b>land = [["D","X","*"],[".",".","."],[".",".","S"]]
<b>输出:</b>-1
<b>解释:</b>下面的图片逐秒模拟了土地的变化。蓝色的单元格被淹没,灰色的单元格是石头。
图片(0)显示了初始状态。正如你所看到的,无论我们选择哪条路径,我们都会在第三秒淹没。并且从 S 到 D 的最小路径需要 4 秒。
所以答案是 -1。
</pre>

<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2800-2899/2814.Minimum%20Time%20Takes%20to%20Reach%20Destination%20Without%20Drowning/images/ex2-2.png" style="padding: 7px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 600px; height: 107px;" /></p>

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

<pre>
<strong>Input:</strong> land = [[&quot;D&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;*&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;X&quot;,&quot;.&quot;,&quot;X&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;S&quot;,&quot;.&quot;]]
<strong>Output:</strong> 6
<strong>Explanation:</strong> It can be shown that we can reach destination in 6 seconds. Also it can be shown that 6 is the minimum seconds one need to reach from S to D.
<b>输入:</b>land = [["D",".",".",".","*","."],[".","X",".","X",".","."],[".",".",".",".","S","."]]
<b>输出:</b>6
<b>解释:</b>可以证明我们可以在 6 秒内到达目标位置。同时也可以证明 6 是从 S 到 D 所需的最小秒数。
</pre>

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

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

<ul>
<li><code>2 &lt;= n, m &lt;= 100</code></li>
<li><code>land</code>&nbsp;consists only of&nbsp;<code>&quot;S&quot;</code>, <code>&quot;D&quot;</code>, <code>&quot;.&quot;</code>, <code>&quot;*&quot;</code> and&nbsp;<code>&quot;X&quot;</code>.</li>
<li><strong>Exactly</strong> one of the cells is equal to <code>&quot;S&quot;</code>.</li>
<li><strong>Exactly</strong> one of the cells is equal to <code>&quot;D&quot;</code>.</li>
<li><code>land</code>&nbsp;只由&nbsp;<code>"S"</code>, <code>"D"</code>, <code>"."</code>, <code>"*"</code> &nbsp;<code>"X"</code>&nbsp;组成。</li>
<li><strong>恰好</strong>有一个单元格等于&nbsp;<code>"S"</code></li>
<li><strong>恰好</strong>有一个单元格等于 <code>"D"</code></li>
</ul>

## 解法
Expand Down
Original file line number Diff line number Diff line change
@@ -1,61 +1,63 @@
# [2819. Minimum Relative Loss After Buying Chocolates](https://leetcode.cn/problems/minimum-relative-loss-after-buying-chocolates)
# [2819. 购买巧克力后的最小相对损失](https://leetcode.cn/problems/minimum-relative-loss-after-buying-chocolates)

[English Version](/solution/2800-2899/2819.Minimum%20Relative%20Loss%20After%20Buying%20Chocolates/README_EN.md)

## 题目描述

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

<p>You are given an integer array <code>prices</code>, which shows the chocolate prices and a 2D integer array <code>queries</code>, where <code>queries[i] = [k<sub>i</sub>, m<sub>i</sub>]</code>.</p>
<p>现给定一个整数数组 <code>prices</code>,表示巧克力的价格;以及一个二维整数数组 <code>queries</code>,其中 <code>queries[i] = [ki, mi]</code></p>

<p>Alice and Bob went to buy some chocolates, and Alice suggested a way to pay for them, and Bob agreed.</p>
<p>Alice Bob 去买巧克力,Alice 提出了一种付款方式,而 Bob 同意了。</p>

<p>The terms for each query are as follows:</p>
<p>对于每个 <code>queries[i]</code> ,它的条件如下:</p>

<ul>
<li>If the price of a chocolate is <strong>less than or equal to</strong> <code>k<sub>i</sub></code>, Bob pays for it.</li>
<li>Otherwise, Bob pays <code>k<sub>i</sub></code> of it, and Alice pays the <strong>rest</strong>.</li>
<li>如果一块巧克力的价格 <strong>小于等于</strong> <code>ki</code>,那么 Bob 为它付款。</li>
<li>否则,Bob 为其中 <code>ki</code> 部分付款,而 Alice <strong>剩余</strong> 部分付款。</li>
</ul>

<p>Bob wants to select <strong>exactly</strong> <code>m<sub>i</sub></code> chocolates such that his <strong>relative loss</strong> is <strong>minimized</strong>, more formally, if, in total, Alice has paid <code>a<sub>i</sub></code> and Bob has paid <code>b<sub>i</sub></code>, Bob wants to minimize <code>b<sub>i</sub> - a<sub>i</sub></code>.</p>
<p>Bob 想要选择 <strong>恰好</strong> <code>mi</code> 块巧克力,使得他的 <strong>相对损失最小</strong> 。更具体地说,如果总共 Alice 付款了 <code>ai</code>Bob 付款了 <code>bi</code>,那么 Bob 希望最小化 <code>bi - ai</code></p>

<p>Return <em>an integer array</em> <code>ans</code> <em>where</em> <code>ans[i]</code> <em>is Bob&#39;s <strong>minimum relative loss </strong>possible for</em> <code>queries[i]</code>.</p>
<p>返回一个整数数组 <code>ans</code>,其中 <code>ans[i]</code> Bob 在&nbsp;<code>queries[i]</code> 中可能的 <strong>最小相对损失</strong> 。</p>

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

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

<pre>
<strong>Input:</strong> prices = [1,9,22,10,19], queries = [[18,4],[5,2]]
<strong>Output:</strong> [34,-21]
<strong>Explanation:</strong> For the 1<sup>st</sup> query Bob selects the chocolates with prices [1,9,10,22]. He pays 1 + 9 + 10 + 18 = 38 and Alice pays 0 + 0 + 0 + 4 = 4. So Bob&#39;s relative loss is 38 - 4 = 34.
For the 2<sup>nd</sup> query Bob selects the chocolates with prices [19,22]. He pays 5 + 5 = 10 and Alice pays 14 + 17 = 31. So Bob&#39;s relative loss is 10 - 31 = -21.
It can be shown that these are the minimum possible relative losses.</pre>
<b>输入:</b>prices = [1,9,22,10,19], queries = [[18,4],[5,2]]
<b>输出:</b>[34,-21]
<b>解释:</b>对于第一个 queryBob 选择价格为 [1,9,10,22] 的巧克力。他付了 1 + 9 + 10 + 18 = 38Alice 付了 0 + 0 + 0 + 4 = 4。因此,Bob 的相对损失是 38 - 4 = 34
对于第二个 queryBob 选择价格为 [19,22] 的巧克力。他付了 5 + 5 = 10Alice 付了 14 + 17 = 31。因此,Bob 的相对损失是 10 - 31 = -21
可以证明这些是可能的最小相对损失。</pre>

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

<pre>
<strong>Input:</strong> prices = [1,5,4,3,7,11,9], queries = [[5,4],[5,7],[7,3],[4,5]]
<strong>Output:</strong> [4,16,7,1]
<strong>Explanation:</strong> For the 1<sup>st</sup> query Bob selects the chocolates with prices [1,3,9,11]. He pays 1 + 3 + 5 + 5 = 14 and Alice pays 0 + 0 + 4 + 6 = 10. So Bob&#39;s relative loss is 14 - 10 = 4.
For the 2<sup>nd</sup> query Bob has to select all the chocolates. He pays 1 + 5 + 4 + 3 + 5 + 5 + 5 = 28 and Alice pays 0 + 0 + 0 + 0 + 2 + 6 + 4 = 12. So Bob&#39;s relative loss is 28 - 12 = 16.
For the 3<sup>rd</sup> query Bob selects the chocolates with prices [1,3,11] and he pays 1 + 3 + 7 = 11 and Alice pays 0 + 0 + 4 = 4. So Bob&#39;s relative loss is 11 - 4 = 7.
For the 4<sup>th</sup> query Bob selects the chocolates with prices [1,3,7,9,11] and he pays 1 + 3 + 4 + 4 + 4 = 16 and Alice pays 0 + 0 + 3 + 5 + 7 = 15. So Bob&#39;s relative loss is 16 - 15 = 1.
It can be shown that these are the minimum possible relative losses.
<b>输入:</b>prices = [1,5,4,3,7,11,9], queries = [[5,4],[5,7],[7,3],[4,5]]
<b>输出:</b>[4,16,7,1]
<b>解释:</b>对于第一个 queryBob 选择价格为 [1,3,9,11] 的巧克力。他付了 1 + 3 + 5 + 5 = 14Alice 付了 0 + 0 + 4 + 6 = 10。因此,Bob 的相对损失是 14 - 10 = 4
对于第二个 queryBob 必须选择所有的巧克力。他付了 1 + 5 + 4 + 3 + 5 + 5 + 5 = 28Alice 付了 0 + 0 + 0 + 0 + 2 + 6 + 4 = 12。因此,Bob 的相对损失是 28 - 12 = 16
对于第三个 queryBob 选择价格为 [1,3,11] 的巧克力。他付了 1 + 3 + 7 = 11Alice 付了 0 + 0 + 4 = 4。因此,Bob 的相对损失是 11 - 4 = 7
对于第四个 queryBob 选择价格为 [1,3,7,9,11] 的巧克力。他付了 1 + 3 + 4 + 4 + 4 = 16Alice 付了 0 + 0 + 3 + 5 + 7 = 15。因此,Bob 的相对损失是 16 - 15 = 1
可以证明这些是可能的最小相对损失。
</pre>

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

<pre>
<strong>Input:</strong> prices = [5,6,7], queries = [[10,1],[5,3],[3,3]]
<strong>Output:</strong> [5,12,0]
<strong>Explanation:</strong> For the 1<sup>st</sup> query Bob selects the chocolate with price 5 and he pays 5 and Alice pays 0. So Bob&#39;s relative loss is 5 - 0 = 5.
For the 2<sup>nd</sup> query Bob has to select all the chocolates. He pays 5 + 5 + 5 = 15 and Alice pays 0 + 1 + 2 = 3. So Bob&#39;s relative loss is 15 - 3 = 12.
For the 3<sup>rd</sup> query Bob has to select all the chocolates. He pays 3 + 3 + 3 = 9 and Alice pays 2 + 3 + 4 = 9. So Bob&#39;s relative loss is 9 - 9 = 0.
It can be shown that these are the minimum possible relative losses.
<b>输入:</b>prices = [5,6,7], queries = [[10,1],[5,3],[3,3]]
<b>输出:</b>[5,12,0]
<b>解释:</b>对于第一个 queryBob 选择价格为 5 的巧克力。他付了 5,Alice 付了 0。因此,Bob 的相对损失是 5 - 0 = 5
对于第二个 queryBob 必须选择所有的巧克力。他付了 5 + 5 + 5 = 15Alice 付了 0 + 1 + 2 = 3。因此,Bob 的相对损失是 15 - 3 = 12
对于第三个 queryBob 必须选择所有的巧克力。他付了 3 + 3 + 3 = 9Alice 付了 2 + 3 + 4 = 9。因此,Bob 的相对损失是 9 - 9 = 0
可以证明这些是可能的最小相对损失。
</pre>

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

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

<ul>
<li><code>1 &lt;= prices.length == n &lt;= 10<sup>5</sup></code></li>
Expand Down
31 changes: 16 additions & 15 deletions solution/2800-2899/2820.Election Results/README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# [2820. Election Results](https://leetcode.cn/problems/election-results)
# [2820. 选举结果](https://leetcode.cn/problems/election-results)

[English Version](/solution/2800-2899/2820.Election%20Results/README_EN.md)

## 题目描述

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

<p>Table: <code><font face="monospace">Votes</font></code></p>
<p>表:<code><font face="monospace">Votes</font></code></p>

<pre>
+-------------+---------+
Expand All @@ -15,23 +15,24 @@
| voter | varchar |
| candidate | varchar |
+-------------+---------+
(voter, candidate) is the primary key for this table.
Each row of this table contains name of the voter and their candidate.
(voter, candidate) 是该表的主键。
该表的每一行都包含选民及其候选人的姓名。
</pre>

<p>The election is conducted in a city where everyone can vote for <strong>one or more</strong> candidates or choose <strong>not</strong> to vote. Each person has <code>1</code><strong> vote</strong> so if they vote for multiple candidates, their vote gets equally split across them. For example, if a person votes for <code>2</code> candidates, these candidates receive an equivalent of <code>0.5</code>&nbsp;votes each.</p>
<p>选举在一个城市进行,每个人都可以投票给 <strong>一个或多个</strong> 候选人,也可以选择 <strong></strong> 投票。每个人都有 <code>1</code> 票,所以如果他们投票给多个候选人,他们的选票会被平均分配。例如,如果一个人投票给 <code>2</code> 个候选人,这些候选人每人获得&nbsp;<code>0.5</code> 张选票。</p>

<p>Write an SQL query to find <code>candidate</code> who got the most votes and won the election. Output the name of the <strong>candidate</strong> or If multiple candidates have an <strong>equal number</strong> of votes, display the names of all of them.</p>
<p>编写一个 SQL 查询来查找获得最多选票并赢得选举的候选人&nbsp;<code>candidate</code> 。输出 <strong>候选人</strong> 的姓名,或者如果多个候选人的票数 <strong>相等</strong> ,则输出所有候选人的姓名。</p>

<p>Return<em> the result table ordered</em> <em>by</em> <code>candidate</code> <em>in <strong>ascending</strong> order.</em></p>
<p>返回按 <code>candidate</code>&nbsp;<strong>升序排序&nbsp;</strong>的结果表。</p>

<p>The query 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>

<pre>
<strong>Input:</strong>
<strong>输入:</strong>
Votes table:
+----------+-----------+
| voter | candidate |
Expand All @@ -47,17 +48,17 @@ Votes table:
| Evelyn | Kathy |
| Arthur | Christine |
+----------+-----------+
<strong>Output:</strong>
<b>输出:</b>
+-----------+
| candidate |
+-----------+
| Christine |
| Ryan |
+-----------+
<strong>Explanation:</strong>
- Kathy and Terry opted not to participate in voting, resulting in their votes being recorded as 0. Charles distributed his vote among three candidates, equating to 0.33 for each candidate. On the other hand, Benjamin, Arthur, Anthony, Edward, and Evelyn each cast their votes for a single candidate.
- Collectively, Candidate Ryan and Christine amassed a total of 2.33 votes, while Kathy received a combined total of 1.33 votes.
Since Ryan and Christine received an equal number of votes, we will display their names in ascending order.</pre>
<b>解释:</b>
- Kathy Terry 选择不投票,导致他们的投票被记录为 0。 Charles 将他的选票分配给了三位候选人,相当于每位候选人得到 0.33 票。另一方面,Benjamin, Arthur, Anthony, Edward, 和 Evely 各自把票投给了一位候选人。
- Ryan Christine 总共获得了2.33票,而 Kathy 总共获得了 1.33 票。
由于 Ryan Christine 获得的票数相等,我们将按升序显示他们的名字。</pre>

## 解法

Expand Down
Loading