mirror of https://github.com/doocs/leetcode.git
feat: add biweekly contest 118 (#2015)
--------- Co-authored-by: Doocs Bot <doocs-bot@outlook.com>
This commit is contained in:
parent
69c47029ab
commit
cc5d541db4
|
|
@ -0,0 +1,92 @@
|
|||
# [2942. 查找包含给定字符的单词](https://leetcode.cn/problems/find-words-containing-character)
|
||||
|
||||
[English Version](/solution/2900-2999/2942.Find%20Words%20Containing%20Character/README_EN.md)
|
||||
|
||||
## 题目描述
|
||||
|
||||
<!-- 这里写题目描述 -->
|
||||
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的字符串数组 <code>words</code> 和一个字符 <code>x</code> 。</p>
|
||||
|
||||
<p>请你返回一个 <strong>下标数组</strong> ,表示下标在数组中对应的单词包含字符 <code>x</code> 。</p>
|
||||
|
||||
<p><b>注意</b> ,返回的数组可以是 <strong>任意</strong> 顺序。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>words = ["leet","code"], x = "e"
|
||||
<b>输出:</b>[0,1]
|
||||
<b>解释:</b>"e" 在两个单词中都出现了:"l<em><strong>ee</strong></em>t" 和 "cod<em><strong>e</strong></em>" 。所以我们返回下标 0 和 1 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>words = ["abc","bcd","aaaa","cbc"], x = "a"
|
||||
<b>输出:</b>[0,2]
|
||||
<b>解释:</b>"a" 在 "<em><strong>a</strong></em>bc" 和 "<em><strong>aaaa</strong></em>" 中出现了,所以我们返回下标 0 和 2 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>words = ["abc","bcd","aaaa","cbc"], x = "z"
|
||||
<b>输出:</b>[]
|
||||
<b>解释:</b>"z" 没有在任何单词中出现。所以我们返回空数组。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= words.length <= 50</code></li>
|
||||
<li><code>1 <= words[i].length <= 50</code></li>
|
||||
<li><code>x</code> 是一个小写英文字母。</li>
|
||||
<li><code>words[i]</code> 只包含小写英文字母。</li>
|
||||
</ul>
|
||||
|
||||
## 解法
|
||||
|
||||
<!-- 这里可写通用的实现逻辑 -->
|
||||
|
||||
<!-- tabs:start -->
|
||||
|
||||
### **Python3**
|
||||
|
||||
<!-- 这里可写当前语言的特殊实现逻辑 -->
|
||||
|
||||
```python
|
||||
|
||||
```
|
||||
|
||||
### **Java**
|
||||
|
||||
<!-- 这里可写当前语言的特殊实现逻辑 -->
|
||||
|
||||
```java
|
||||
|
||||
```
|
||||
|
||||
### **C++**
|
||||
|
||||
```cpp
|
||||
|
||||
```
|
||||
|
||||
### **Go**
|
||||
|
||||
```go
|
||||
|
||||
```
|
||||
|
||||
### **...**
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
|
||||
<!-- tabs:end -->
|
||||
|
|
@ -0,0 +1,82 @@
|
|||
# [2942. Find Words Containing Character](https://leetcode.com/problems/find-words-containing-character)
|
||||
|
||||
[中文文档](/solution/2900-2999/2942.Find%20Words%20Containing%20Character/README.md)
|
||||
|
||||
## Description
|
||||
|
||||
<p>You are given a <strong>0-indexed</strong> array of strings <code>words</code> and a character <code>x</code>.</p>
|
||||
|
||||
<p>Return <em>an <strong>array of indices</strong> representing the words that contain the character </em><code>x</code>.</p>
|
||||
|
||||
<p><strong>Note</strong> that the returned array may be in <strong>any</strong> order.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["leet","code"], x = "e"
|
||||
<strong>Output:</strong> [0,1]
|
||||
<strong>Explanation:</strong> "e" occurs in both words: "l<strong><u>ee</u></strong>t", and "cod<u><strong>e</strong></u>". Hence, we return indices 0 and 1.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["abc","bcd","aaaa","cbc"], x = "a"
|
||||
<strong>Output:</strong> [0,2]
|
||||
<strong>Explanation:</strong> "a" occurs in "<strong><u>a</u></strong>bc", and "<u><strong>aaaa</strong></u>". Hence, we return indices 0 and 2.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["abc","bcd","aaaa","cbc"], x = "z"
|
||||
<strong>Output:</strong> []
|
||||
<strong>Explanation:</strong> "z" does not occur in any of the words. Hence, we return an empty array.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= words.length <= 50</code></li>
|
||||
<li><code>1 <= words[i].length <= 50</code></li>
|
||||
<li><code>x</code> is a lowercase English letter.</li>
|
||||
<li><code>words[i]</code> consists only of lowercase English letters.</li>
|
||||
</ul>
|
||||
|
||||
## Solutions
|
||||
|
||||
<!-- tabs:start -->
|
||||
|
||||
### **Python3**
|
||||
|
||||
```python
|
||||
|
||||
```
|
||||
|
||||
### **Java**
|
||||
|
||||
```java
|
||||
|
||||
```
|
||||
|
||||
### **C++**
|
||||
|
||||
```cpp
|
||||
|
||||
```
|
||||
|
||||
### **Go**
|
||||
|
||||
```go
|
||||
|
||||
```
|
||||
|
||||
### **...**
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
|
||||
<!-- tabs:end -->
|
||||
|
|
@ -0,0 +1,139 @@
|
|||
# [2943. 最大化网格图中正方形空洞的面积](https://leetcode.cn/problems/maximize-area-of-square-hole-in-grid)
|
||||
|
||||
[English Version](/solution/2900-2999/2943.Maximize%20Area%20of%20Square%20Hole%20in%20Grid/README_EN.md)
|
||||
|
||||
## 题目描述
|
||||
|
||||
<!-- 这里写题目描述 -->
|
||||
|
||||
<p>给你一个网格图,由 <code>n + 2</code> 条 <strong>横线段</strong> 和 <code>m + 2</code> 条 <strong>竖线段</strong> 组成,一开始所有区域均为 <code>1 x 1</code> 的单元格。</p>
|
||||
|
||||
<p>所有线段的编号从 <strong>1</strong> 开始。</p>
|
||||
|
||||
<p>给你两个整数 <code>n</code> 和 <code>m</code> 。</p>
|
||||
|
||||
<p>同时给你两个整数数组 <code>hBars</code> 和 <code>vBars</code> 。</p>
|
||||
|
||||
<ul>
|
||||
<li><code>hBars</code> 包含区间 <code>[2, n + 1]</code> 内 <strong>互不相同</strong> 的横线段编号。</li>
|
||||
<li><code>vBars</code> 包含 <code>[2, m + 1]</code> 内 <strong>互不相同的</strong> 竖线段编号。</li>
|
||||
</ul>
|
||||
|
||||
<p>如果满足以下条件之一,你可以 <strong>移除</strong> 两个数组中的部分线段:</p>
|
||||
|
||||
<ul>
|
||||
<li>如果移除的是横线段,它必须是 <code>hBars</code> 中的值。</li>
|
||||
<li>如果移除的是竖线段,它必须是 <code>vBars</code> 中的值。</li>
|
||||
</ul>
|
||||
|
||||
<p>请你返回移除一些线段后(<strong>可能不移除任何线段)</strong>,剩余网格图中 <strong>最大正方形</strong> 空洞的面积,正方形空洞的意思是正方形 <strong>内部</strong> 不含有任何线段。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2900-2999/2943.Maximize%20Area%20of%20Square%20Hole%20in%20Grid/images/screenshot-from-2023-11-05-22-40-25.png" style="width: 411px; height: 220px;" /></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>n = 2, m = 1, hBars = [2,3], vBars = [2]
|
||||
<b>输出:</b>4
|
||||
<b>解释:</b>左边的图是一开始的网格图。
|
||||
横线编号的范围是区间 [1,4] ,竖线编号的范围是区间 [1,3] 。
|
||||
可以移除的横线段为 [2,3] ,竖线段为 [2] 。
|
||||
一种得到最大正方形面积的方法是移除横线段 2 和竖线段 2 。
|
||||
操作后得到的网格图如右图所示。
|
||||
正方形空洞面积为 4。
|
||||
无法得到面积大于 4 的正方形空洞。
|
||||
所以答案为 4 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2900-2999/2943.Maximize%20Area%20of%20Square%20Hole%20in%20Grid/images/screenshot-from-2023-11-04-17-01-02.png" style="width: 368px; height: 145px;" /></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>n = 1, m = 1, hBars = [2], vBars = [2]
|
||||
<b>输出:</b>4
|
||||
<b>解释:</b>左边的图是一开始的网格图。
|
||||
横线编号的范围是区间 [1,3] ,竖线编号的范围是区间 [1,3] 。
|
||||
可以移除的横线段为 [2] ,竖线段为 [2] 。
|
||||
一种得到最大正方形面积的方法是移除横线段 2 和竖线段 2 。
|
||||
操作后得到的网格图如右图所示。
|
||||
正方形空洞面积为 4。
|
||||
无法得到面积大于 4 的正方形空洞。
|
||||
所以答案为 4 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2900-2999/2943.Maximize%20Area%20of%20Square%20Hole%20in%20Grid/images/screenshot-from-2023-11-05-22-33-35.png" style="width: 648px; height: 218px;" /></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>n = 2, m = 3, hBars = [2,3], vBars = [2,3,4]
|
||||
<b>输出:</b>9
|
||||
<b>解释:</b>左边的图是一开始的网格图。
|
||||
横线编号的范围是区间 [1,4] ,竖线编号的范围是区间 [1,5] 。
|
||||
可以移除的横线段为 [2,3] ,竖线段为 [2,3,4] 。
|
||||
一种得到最大正方形面积的方法是移除横线段 2、3 和竖线段 3、4 。
|
||||
操作后得到的网格图如右图所示。
|
||||
正方形空洞面积为 9。
|
||||
无法得到面积大于 9 的正方形空洞。
|
||||
所以答案为 9 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= m <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= hBars.length <= 100</code></li>
|
||||
<li><code>2 <= hBars[i] <= n + 1</code></li>
|
||||
<li><code>1 <= vBars.length <= 100</code></li>
|
||||
<li><code>2 <= vBars[i] <= m + 1</code></li>
|
||||
<li><code>hBars</code> 中的值互不相同。</li>
|
||||
<li><code>vBars</code> 中的值互不相同。</li>
|
||||
</ul>
|
||||
|
||||
## 解法
|
||||
|
||||
<!-- 这里可写通用的实现逻辑 -->
|
||||
|
||||
<!-- tabs:start -->
|
||||
|
||||
### **Python3**
|
||||
|
||||
<!-- 这里可写当前语言的特殊实现逻辑 -->
|
||||
|
||||
```python
|
||||
|
||||
```
|
||||
|
||||
### **Java**
|
||||
|
||||
<!-- 这里可写当前语言的特殊实现逻辑 -->
|
||||
|
||||
```java
|
||||
|
||||
```
|
||||
|
||||
### **C++**
|
||||
|
||||
```cpp
|
||||
|
||||
```
|
||||
|
||||
### **Go**
|
||||
|
||||
```go
|
||||
|
||||
```
|
||||
|
||||
### **...**
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
|
||||
<!-- tabs:end -->
|
||||
|
|
@ -0,0 +1,128 @@
|
|||
# [2943. Maximize Area of Square Hole in Grid](https://leetcode.com/problems/maximize-area-of-square-hole-in-grid)
|
||||
|
||||
[中文文档](/solution/2900-2999/2943.Maximize%20Area%20of%20Square%20Hole%20in%20Grid/README.md)
|
||||
|
||||
## Description
|
||||
|
||||
<p>There is a grid with <code>n + 2</code> <strong>horizontal</strong> bars and <code>m + 2</code> <strong>vertical</strong> bars, and initially containing <code>1 x 1</code> unit cells.</p>
|
||||
|
||||
<p>The bars are <strong>1-indexed</strong>.</p>
|
||||
|
||||
<p>You are given the two integers, <code>n</code> and <code>m</code>.</p>
|
||||
|
||||
<p>You are also given two integer arrays: <code>hBars</code> and <code>vBars</code>.</p>
|
||||
|
||||
<ul>
|
||||
<li><code>hBars</code> contains <strong>distinct</strong> horizontal bars in the range <code>[2, n + 1]</code>.</li>
|
||||
<li><code>vBars</code> contains <strong>distinct</strong> vertical bars in the range <code>[2, m + 1]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>You are allowed to <strong>remove</strong> bars that satisfy any of the following conditions:</p>
|
||||
|
||||
<ul>
|
||||
<li>If it is a horizontal bar, it must correspond to a value in <code>hBars</code>.</li>
|
||||
<li>If it is a vertical bar, it must correspond to a value in <code>vBars</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>an integer denoting the <strong>maximum</strong> area of a <strong>square-shaped</strong> hole in the grid after removing some bars (<strong>possibly none</strong>).</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2900-2999/2943.Maximize%20Area%20of%20Square%20Hole%20in%20Grid/images/screenshot-from-2023-11-05-22-40-25.png" style="width: 411px; height: 220px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 2, m = 1, hBars = [2,3], vBars = [2]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> The left image shows the initial grid formed by the bars.
|
||||
The horizontal bars are in the range [1,4], and the vertical bars are in the range [1,3].
|
||||
It is allowed to remove horizontal bars [2,3] and the vertical bar [2].
|
||||
One way to get the maximum square-shaped hole is by removing horizontal bar 2 and vertical bar 2.
|
||||
The resulting grid is shown on the right.
|
||||
The hole has an area of 4.
|
||||
It can be shown that it is not possible to get a square hole with an area more than 4.
|
||||
Hence, the answer is 4.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2900-2999/2943.Maximize%20Area%20of%20Square%20Hole%20in%20Grid/images/screenshot-from-2023-11-04-17-01-02.png" style="width: 368px; height: 145px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 1, m = 1, hBars = [2], vBars = [2]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> The left image shows the initial grid formed by the bars.
|
||||
The horizontal bars are in the range [1,3], and the vertical bars are in the range [1,3].
|
||||
It is allowed to remove the horizontal bar [2] and the vertical bar [2].
|
||||
To get the maximum square-shaped hole, we remove horizontal bar 2 and vertical bar 2.
|
||||
The resulting grid is shown on the right.
|
||||
The hole has an area of 4.
|
||||
Hence, the answer is 4, and it is the maximum possible.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2900-2999/2943.Maximize%20Area%20of%20Square%20Hole%20in%20Grid/images/screenshot-from-2023-11-05-22-33-35.png" style="width: 648px; height: 218px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 2, m = 3, hBars = [2,3], vBars = [2,3,4]
|
||||
<strong>Output:</strong> 9
|
||||
<strong>Explanation:</strong> The left image shows the initial grid formed by the bars.
|
||||
The horizontal bars are in the range [1,4], and the vertical bars are in the range [1,5].
|
||||
It is allowed to remove horizontal bars [2,3] and vertical bars [2,3,4].
|
||||
One way to get the maximum square-shaped hole is by removing horizontal bars 2 and 3, and vertical bars 3 and 4.
|
||||
The resulting grid is shown on the right.
|
||||
The hole has an area of 9.
|
||||
It can be shown that it is not possible to get a square hole with an area more than 9.
|
||||
Hence, the answer is 9.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= m <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= hBars.length <= 100</code></li>
|
||||
<li><code>2 <= hBars[i] <= n + 1</code></li>
|
||||
<li><code>1 <= vBars.length <= 100</code></li>
|
||||
<li><code>2 <= vBars[i] <= m + 1</code></li>
|
||||
<li>All values in <code>hBars</code> are distinct.</li>
|
||||
<li>All values in <code>vBars</code> are distinct.</li>
|
||||
</ul>
|
||||
|
||||
## Solutions
|
||||
|
||||
<!-- tabs:start -->
|
||||
|
||||
### **Python3**
|
||||
|
||||
```python
|
||||
|
||||
```
|
||||
|
||||
### **Java**
|
||||
|
||||
```java
|
||||
|
||||
```
|
||||
|
||||
### **C++**
|
||||
|
||||
```cpp
|
||||
|
||||
```
|
||||
|
||||
### **Go**
|
||||
|
||||
```go
|
||||
|
||||
```
|
||||
|
||||
### **...**
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
|
||||
<!-- tabs:end -->
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 3.9 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 6.5 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 4.9 KiB |
|
|
@ -0,0 +1,100 @@
|
|||
# [2944. 购买水果需要的最少金币数](https://leetcode.cn/problems/minimum-number-of-coins-for-fruits)
|
||||
|
||||
[English Version](/solution/2900-2999/2944.Minimum%20Number%20of%20Coins%20for%20Fruits/README_EN.md)
|
||||
|
||||
## 题目描述
|
||||
|
||||
<!-- 这里写题目描述 -->
|
||||
|
||||
<p>你在一个水果超市里,货架上摆满了玲琅满目的奇珍异果。</p>
|
||||
|
||||
<p>给你一个下标从 <strong>1</strong> 开始的数组 <code>prices</code> ,其中 <code>prices[i]</code> 表示你购买第 <code>i</code> 个水果需要花费的金币数目。</p>
|
||||
|
||||
<p>水果超市有如下促销活动:</p>
|
||||
|
||||
<ul>
|
||||
<li>如果你花费 <code>price[i]</code> 购买了水果 <code>i</code> ,那么接下来的 <code>i</code> 个水果你都可以免费获得。</li>
|
||||
</ul>
|
||||
|
||||
<p><strong>注意</strong> ,即使你 <strong>可以</strong> 免费获得水果 <code>j</code> ,你仍然可以花费 <code>prices[j]</code> 个金币去购买它以便能免费获得接下来的 <code>j</code> 个水果。</p>
|
||||
|
||||
<p>请你返回获得所有水果所需要的 <strong>最少</strong> 金币数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>prices = [3,1,2]
|
||||
<b>输出:</b>4
|
||||
<b>解释</b><strong>:</strong>你可以按如下方法获得所有水果:
|
||||
- 花 3 个金币购买水果 1 ,然后免费获得水果 2 。
|
||||
- 花 1 个金币购买水果 2 ,然后免费获得水果 3 。
|
||||
- 免费获得水果 3 。
|
||||
注意,虽然你可以免费获得水果 2 ,但你还是花 1 个金币去购买它,因为这样的总花费最少。
|
||||
购买所有水果需要最少花费 4 个金币。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>prices = [1,10,1,1]
|
||||
<b>输出:</b>2
|
||||
<b>解释:</b>你可以按如下方法获得所有水果:
|
||||
- 花 1 个金币购买水果 1 ,然后免费获得水果 2 。
|
||||
- 免费获得水果 2 。
|
||||
- 花 1 个金币购买水果 3 ,然后免费获得水果 4 。
|
||||
- 免费获得水果 4 。
|
||||
购买所有水果需要最少花费 2 个金币。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= prices.length <= 1000</code></li>
|
||||
<li><code>1 <= prices[i] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
||||
|
||||
## 解法
|
||||
|
||||
<!-- 这里可写通用的实现逻辑 -->
|
||||
|
||||
<!-- tabs:start -->
|
||||
|
||||
### **Python3**
|
||||
|
||||
<!-- 这里可写当前语言的特殊实现逻辑 -->
|
||||
|
||||
```python
|
||||
|
||||
```
|
||||
|
||||
### **Java**
|
||||
|
||||
<!-- 这里可写当前语言的特殊实现逻辑 -->
|
||||
|
||||
```java
|
||||
|
||||
```
|
||||
|
||||
### **C++**
|
||||
|
||||
```cpp
|
||||
|
||||
```
|
||||
|
||||
### **Go**
|
||||
|
||||
```go
|
||||
|
||||
```
|
||||
|
||||
### **...**
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
|
||||
<!-- tabs:end -->
|
||||
|
|
@ -0,0 +1,90 @@
|
|||
# [2944. Minimum Number of Coins for Fruits](https://leetcode.com/problems/minimum-number-of-coins-for-fruits)
|
||||
|
||||
[中文文档](/solution/2900-2999/2944.Minimum%20Number%20of%20Coins%20for%20Fruits/README.md)
|
||||
|
||||
## Description
|
||||
|
||||
<p>You are at a fruit market with different types of exotic fruits on display.</p>
|
||||
|
||||
<p>You are given a <strong>1-indexed</strong> array <code>prices</code>, where <code>prices[i]</code> denotes the number of coins needed to purchase the <code>i<sup>th</sup></code> fruit.</p>
|
||||
|
||||
<p>The fruit market has the following offer:</p>
|
||||
|
||||
<ul>
|
||||
<li>If you purchase the <code>i<sup>th</sup></code> fruit at <code>prices[i]</code> coins, you can get the next <code>i</code> fruits for free.</li>
|
||||
</ul>
|
||||
|
||||
<p><strong>Note</strong> that even if you <strong>can</strong> take fruit <code>j</code> for free, you can still purchase it for <code>prices[j]</code> coins to receive a new offer.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> number of coins needed to acquire all the fruits</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> prices = [3,1,2]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> You can acquire the fruits as follows:
|
||||
- Purchase the 1<sup>st</sup> fruit with 3 coins, you are allowed to take the 2<sup>nd</sup> fruit for free.
|
||||
- Purchase the 2<sup>nd</sup> fruit with 1 coin, you are allowed to take the 3<sup>rd</sup> fruit for free.
|
||||
- Take the 3<sup>rd</sup> fruit for free.
|
||||
Note that even though you were allowed to take the 2<sup>nd</sup> fruit for free, you purchased it because it is more optimal.
|
||||
It can be proven that 4 is the minimum number of coins needed to acquire all the fruits.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> prices = [1,10,1,1]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> You can acquire the fruits as follows:
|
||||
- Purchase the 1<sup>st</sup> fruit with 1 coin, you are allowed to take the 2<sup>nd</sup> fruit for free.
|
||||
- Take the 2<sup>nd</sup> fruit for free.
|
||||
- Purchase the 3<sup>rd</sup> fruit for 1 coin, you are allowed to take the 4<sup>th</sup> fruit for free.
|
||||
- Take the 4<sup>t</sup><sup>h</sup> fruit for free.
|
||||
It can be proven that 2 is the minimum number of coins needed to acquire all the fruits.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= prices.length <= 1000</code></li>
|
||||
<li><code>1 <= prices[i] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
||||
|
||||
## Solutions
|
||||
|
||||
<!-- tabs:start -->
|
||||
|
||||
### **Python3**
|
||||
|
||||
```python
|
||||
|
||||
```
|
||||
|
||||
### **Java**
|
||||
|
||||
```java
|
||||
|
||||
```
|
||||
|
||||
### **C++**
|
||||
|
||||
```cpp
|
||||
|
||||
```
|
||||
|
||||
### **Go**
|
||||
|
||||
```go
|
||||
|
||||
```
|
||||
|
||||
### **...**
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
|
||||
<!-- tabs:end -->
|
||||
|
|
@ -0,0 +1,98 @@
|
|||
# [2945. 找到最大非递减数组的长度](https://leetcode.cn/problems/find-maximum-non-decreasing-array-length)
|
||||
|
||||
[English Version](/solution/2900-2999/2945.Find%20Maximum%20Non-decreasing%20Array%20Length/README_EN.md)
|
||||
|
||||
## 题目描述
|
||||
|
||||
<!-- 这里写题目描述 -->
|
||||
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> 。</p>
|
||||
|
||||
<p>你可以执行任意次操作。每次操作中,你需要选择一个 <strong>子数组</strong> ,并将这个子数组用它所包含元素的 <strong>和</strong> 替换。比方说,给定数组是 <code>[1,3,5,6]</code> ,你可以选择子数组 <code>[3,5]</code> ,用子数组的和 <code>8</code> 替换掉子数组,然后数组会变为 <code>[1,8,6]</code> 。</p>
|
||||
|
||||
<p>请你返回执行任意次操作以后,可以得到的 <strong>最长非递减</strong> 数组的长度。</p>
|
||||
|
||||
<p><strong>子数组</strong> 指的是一个数组中一段连续 <strong>非空</strong> 的元素序列。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [5,2,2]
|
||||
<b>输出:</b>1
|
||||
<strong>解释:</strong>这个长度为 3 的数组不是非递减的。
|
||||
我们有 2 种方案使数组长度为 2 。
|
||||
第一种,选择子数组 [2,2] ,对数组执行操作后得到 [5,4] 。
|
||||
第二种,选择子数组 [5,2] ,对数组执行操作后得到 [7,2] 。
|
||||
这两种方案中,数组最后都不是 <strong>非递减</strong> 的,所以不是可行的答案。
|
||||
如果我们选择子数组 [5,2,2] ,并将它替换为 [9] ,数组变成非递减的。
|
||||
所以答案为 1 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [1,2,3,4]
|
||||
<b>输出:</b>4
|
||||
<b>解释:</b>数组已经是非递减的。所以答案为 4 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [4,3,2,6]
|
||||
<b>输出:</b>3
|
||||
<b>解释:</b>将 [3,2] 替换为 [5] ,得到数组 [4,5,6] ,它是非递减的。
|
||||
最大可能的答案为 3 。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
||||
|
||||
## 解法
|
||||
|
||||
<!-- 这里可写通用的实现逻辑 -->
|
||||
|
||||
<!-- tabs:start -->
|
||||
|
||||
### **Python3**
|
||||
|
||||
<!-- 这里可写当前语言的特殊实现逻辑 -->
|
||||
|
||||
```python
|
||||
|
||||
```
|
||||
|
||||
### **Java**
|
||||
|
||||
<!-- 这里可写当前语言的特殊实现逻辑 -->
|
||||
|
||||
```java
|
||||
|
||||
```
|
||||
|
||||
### **C++**
|
||||
|
||||
```cpp
|
||||
|
||||
```
|
||||
|
||||
### **Go**
|
||||
|
||||
```go
|
||||
|
||||
```
|
||||
|
||||
### **...**
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
|
||||
<!-- tabs:end -->
|
||||
|
|
@ -0,0 +1,88 @@
|
|||
# [2945. Find Maximum Non-decreasing Array Length](https://leetcode.com/problems/find-maximum-non-decreasing-array-length)
|
||||
|
||||
[中文文档](/solution/2900-2999/2945.Find%20Maximum%20Non-decreasing%20Array%20Length/README.md)
|
||||
|
||||
## Description
|
||||
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p>
|
||||
|
||||
<p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,5,6]</code> and you select subarray <code>[3,5]</code> the array will convert to <code>[1,8,6]</code>.</p>
|
||||
|
||||
<p>Return <em>the </em><strong><em>maximum</em></strong><em> length of a </em><strong><em>non-decreasing</em></strong><em> array that can be made after applying operations.</em></p>
|
||||
|
||||
<p>A <strong>subarray</strong> is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [5,2,2]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> This array with length 3 is not non-decreasing.
|
||||
We have two ways to make the array length two.
|
||||
First, choosing subarray [2,2] converts the array to [5,4].
|
||||
Second, choosing subarray [5,2] converts the array to [7,2].
|
||||
In these two ways the array is not non-decreasing.
|
||||
And if we choose subarray [5,2,2] and replace it with [9] it becomes non-decreasing.
|
||||
So the answer is 1.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> The array is non-decreasing. So the answer is 4.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [4,3,2,6]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> Replacing [3,2] with [5] converts the given array to [4,5,6] that is non-decreasing.
|
||||
Because the given array is not non-decreasing, the maximum<!-- notionvc: 3447a505-d1ee-4411-8cae-e52162f53a55 --> possible answer is 3.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
||||
|
||||
## Solutions
|
||||
|
||||
<!-- tabs:start -->
|
||||
|
||||
### **Python3**
|
||||
|
||||
```python
|
||||
|
||||
```
|
||||
|
||||
### **Java**
|
||||
|
||||
```java
|
||||
|
||||
```
|
||||
|
||||
### **C++**
|
||||
|
||||
```cpp
|
||||
|
||||
```
|
||||
|
||||
### **Go**
|
||||
|
||||
```go
|
||||
|
||||
```
|
||||
|
||||
### **...**
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
|
||||
<!-- tabs:end -->
|
||||
|
|
@ -22,6 +22,13 @@
|
|||
|
||||
## 往期竞赛
|
||||
|
||||
#### 第 118 场双周赛(2023-11-25 22:30, 90 分钟) 参赛人数 2425
|
||||
|
||||
- [2942. 查找包含给定字符的单词](/solution/2900-2999/2942.Find%20Words%20Containing%20Character/README.md)
|
||||
- [2943. 最大化网格图中正方形空洞的面积](/solution/2900-2999/2943.Maximize%20Area%20of%20Square%20Hole%20in%20Grid/README.md)
|
||||
- [2944. 购买水果需要的最少金币数](/solution/2900-2999/2944.Minimum%20Number%20of%20Coins%20for%20Fruits/README.md)
|
||||
- [2945. 找到最大非递减数组的长度](/solution/2900-2999/2945.Find%20Maximum%20Non-decreasing%20Array%20Length/README.md)
|
||||
|
||||
#### 第 372 场周赛(2023-11-19 10:30, 90 分钟) 参赛人数 3920
|
||||
|
||||
- [2937. 使三个字符串相等](/solution/2900-2999/2937.Make%20Three%20Strings%20Equal/README.md)
|
||||
|
|
|
|||
|
|
@ -25,6 +25,13 @@ Get your rating changes right after the completion of LeetCode contests, https:/
|
|||
|
||||
## Past Contests
|
||||
|
||||
#### Biweekly Contest 118
|
||||
|
||||
- [2942. Find Words Containing Character](/solution/2900-2999/2942.Find%20Words%20Containing%20Character/README_EN.md)
|
||||
- [2943. Maximize Area of Square Hole in Grid](/solution/2900-2999/2943.Maximize%20Area%20of%20Square%20Hole%20in%20Grid/README_EN.md)
|
||||
- [2944. Minimum Number of Coins for Fruits](/solution/2900-2999/2944.Minimum%20Number%20of%20Coins%20for%20Fruits/README_EN.md)
|
||||
- [2945. Find Maximum Non-decreasing Array Length](/solution/2900-2999/2945.Find%20Maximum%20Non-decreasing%20Array%20Length/README_EN.md)
|
||||
|
||||
#### Weekly Contest 372
|
||||
|
||||
- [2937. Make Three Strings Equal](/solution/2900-2999/2937.Make%20Three%20Strings%20Equal/README_EN.md)
|
||||
|
|
|
|||
|
|
@ -2952,6 +2952,10 @@
|
|||
| 2939 | [最大异或乘积](/solution/2900-2999/2939.Maximum%20Xor%20Product/README.md) | `贪心`,`位运算`,`数学` | 中等 | 第 372 场周赛 |
|
||||
| 2940 | [找到 Alice 和 Bob 可以相遇的建筑](/solution/2900-2999/2940.Find%20Building%20Where%20Alice%20and%20Bob%20Can%20Meet/README.md) | `栈`,`树状数组`,`线段树`,`数组`,`二分查找`,`单调栈`,`堆(优先队列)` | 困难 | 第 372 场周赛 |
|
||||
| 2941 | [Maximum GCD-Sum of a Subarray](/solution/2900-2999/2941.Maximum%20GCD-Sum%20of%20a%20Subarray/README.md) | | 困难 | 🔒 |
|
||||
| 2942 | [查找包含给定字符的单词](/solution/2900-2999/2942.Find%20Words%20Containing%20Character/README.md) | | 简单 | 第 118 场双周赛 |
|
||||
| 2943 | [最大化网格图中正方形空洞的面积](/solution/2900-2999/2943.Maximize%20Area%20of%20Square%20Hole%20in%20Grid/README.md) | | 中等 | 第 118 场双周赛 |
|
||||
| 2944 | [购买水果需要的最少金币数](/solution/2900-2999/2944.Minimum%20Number%20of%20Coins%20for%20Fruits/README.md) | | 中等 | 第 118 场双周赛 |
|
||||
| 2945 | [找到最大非递减数组的长度](/solution/2900-2999/2945.Find%20Maximum%20Non-decreasing%20Array%20Length/README.md) | | 困难 | 第 118 场双周赛 |
|
||||
|
||||
## 版权
|
||||
|
||||
|
|
|
|||
|
|
@ -2950,6 +2950,10 @@ Press <kbd>Control</kbd> + <kbd>F</kbd>(or <kbd>Command</kbd> + <kbd>F</kbd> on
|
|||
| 2939 | [Maximum Xor Product](/solution/2900-2999/2939.Maximum%20Xor%20Product/README_EN.md) | `Greedy`,`Bit Manipulation`,`Math` | Medium | Weekly Contest 372 |
|
||||
| 2940 | [Find Building Where Alice and Bob Can Meet](/solution/2900-2999/2940.Find%20Building%20Where%20Alice%20and%20Bob%20Can%20Meet/README_EN.md) | `Stack`,`Binary Indexed Tree`,`Segment Tree`,`Array`,`Binary Search`,`Monotonic Stack`,`Heap (Priority Queue)` | Hard | Weekly Contest 372 |
|
||||
| 2941 | [Maximum GCD-Sum of a Subarray](/solution/2900-2999/2941.Maximum%20GCD-Sum%20of%20a%20Subarray/README_EN.md) | | Hard | 🔒 |
|
||||
| 2942 | [Find Words Containing Character](/solution/2900-2999/2942.Find%20Words%20Containing%20Character/README_EN.md) | | Easy | Biweekly Contest 118 |
|
||||
| 2943 | [Maximize Area of Square Hole in Grid](/solution/2900-2999/2943.Maximize%20Area%20of%20Square%20Hole%20in%20Grid/README_EN.md) | | Medium | Biweekly Contest 118 |
|
||||
| 2944 | [Minimum Number of Coins for Fruits](/solution/2900-2999/2944.Minimum%20Number%20of%20Coins%20for%20Fruits/README_EN.md) | | Medium | Biweekly Contest 118 |
|
||||
| 2945 | [Find Maximum Non-decreasing Array Length](/solution/2900-2999/2945.Find%20Maximum%20Non-decreasing%20Array%20Length/README_EN.md) | | Hard | Biweekly Contest 118 |
|
||||
|
||||
## Copyright
|
||||
|
||||
|
|
|
|||
|
|
@ -422,9 +422,8 @@ def run():
|
|||
# 刷新题目文件
|
||||
if refresh_all:
|
||||
refresh(ls)
|
||||
|
||||
# 格式化
|
||||
os.system('cd .. && npx prettier --write "**/*.{md,js,ts,php,sql}"')
|
||||
# 格式化
|
||||
os.system('cd .. && npx prettier --write "**/*.{md,js,ts,php,sql}"')
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
|
|||
|
|
@ -2999,3 +2999,7 @@
|
|||
- [2939.最大异或乘积](/solution/2900-2999/2939.Maximum%20Xor%20Product/README.md)
|
||||
- [2940.找到 Alice 和 Bob 可以相遇的建筑](/solution/2900-2999/2940.Find%20Building%20Where%20Alice%20and%20Bob%20Can%20Meet/README.md)
|
||||
- [2941.Maximum GCD-Sum of a Subarray](/solution/2900-2999/2941.Maximum%20GCD-Sum%20of%20a%20Subarray/README.md)
|
||||
- [2942.查找包含给定字符的单词](/solution/2900-2999/2942.Find%20Words%20Containing%20Character/README.md)
|
||||
- [2943.最大化网格图中正方形空洞的面积](/solution/2900-2999/2943.Maximize%20Area%20of%20Square%20Hole%20in%20Grid/README.md)
|
||||
- [2944.购买水果需要的最少金币数](/solution/2900-2999/2944.Minimum%20Number%20of%20Coins%20for%20Fruits/README.md)
|
||||
- [2945.找到最大非递减数组的长度](/solution/2900-2999/2945.Find%20Maximum%20Non-decreasing%20Array%20Length/README.md)
|
||||
|
|
|
|||
|
|
@ -2999,3 +2999,7 @@
|
|||
- [2939.Maximum Xor Product](/solution/2900-2999/2939.Maximum%20Xor%20Product/README_EN.md)
|
||||
- [2940.Find Building Where Alice and Bob Can Meet](/solution/2900-2999/2940.Find%20Building%20Where%20Alice%20and%20Bob%20Can%20Meet/README_EN.md)
|
||||
- [2941.Maximum GCD-Sum of a Subarray](/solution/2900-2999/2941.Maximum%20GCD-Sum%20of%20a%20Subarray/README_EN.md)
|
||||
- [2942.Find Words Containing Character](/solution/2900-2999/2942.Find%20Words%20Containing%20Character/README_EN.md)
|
||||
- [2943.Maximize Area of Square Hole in Grid](/solution/2900-2999/2943.Maximize%20Area%20of%20Square%20Hole%20in%20Grid/README_EN.md)
|
||||
- [2944.Minimum Number of Coins for Fruits](/solution/2900-2999/2944.Minimum%20Number%20of%20Coins%20for%20Fruits/README_EN.md)
|
||||
- [2945.Find Maximum Non-decreasing Array Length](/solution/2900-2999/2945.Find%20Maximum%20Non-decreasing%20Array%20Length/README_EN.md)
|
||||
|
|
|
|||
Loading…
Reference in New Issue