Skip to content

Commit 7c6fb30

Browse files
authored
feat: add weekly contest 440 (#4138)
1 parent 7a255e2 commit 7c6fb30

File tree

15 files changed

+919
-9
lines changed

15 files changed

+919
-9
lines changed

solution/2200-2299/2227.Encrypt and Decrypt Strings/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ tags:
6161

6262
<strong>解释:</strong>
6363
Encrypter encrypter = new Encrypter([['a', 'b', 'c', 'd'], ["ei", "zf", "ei", "am"], ["abcd", "acbd", "adbc", "badc", "dacb", "cadb", "cbda", "abad"]);
64-
encrypter.encrypt("abcd"); // 返回 "eizfeiam"。
64+
encrypter.encrypt("abcd"); // 返回 "eizfeiam"。
6565
&nbsp; // 'a' 映射为 "ei",'b' 映射为 "zf",'c' 映射为 "ei",'d' 映射为 "am"。
66-
encrypter.decrypt("eizfeiam"); // return 2.
67-
// "ei" 可以映射为 'a' 或 'c',"zf" 映射为 'b',"am" 映射为 'd'。
68-
// 因此,解密后可以得到的字符串是 "abad","cbad","abcd" 和 "cbcd"。
66+
encrypter.decrypt("eizfeiam"); // return 2.
67+
// "ei" 可以映射为 'a' 或 'c',"zf" 映射为 'b',"am" 映射为 'd'。
68+
// 因此,解密后可以得到的字符串是 "abad","cbad","abcd" 和 "cbcd"。
6969
// 其中 2 个字符串,"abad" 和 "abcd",在 dictionary 中出现,所以答案是 2 。
7070
</pre>
7171

solution/2200-2299/2227.Encrypt and Decrypt Strings/README_EN.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ tags:
6060

6161
<strong>Explanation</strong>
6262
Encrypter encrypter = new Encrypter([[&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;], [&quot;ei&quot;, &quot;zf&quot;, &quot;ei&quot;, &quot;am&quot;], [&quot;abcd&quot;, &quot;acbd&quot;, &quot;adbc&quot;, &quot;badc&quot;, &quot;dacb&quot;, &quot;cadb&quot;, &quot;cbda&quot;, &quot;abad&quot;]);
63-
encrypter.encrypt(&quot;abcd&quot;); // return &quot;eizfeiam&quot;.
63+
encrypter.encrypt(&quot;abcd&quot;); // return &quot;eizfeiam&quot;.
6464
&nbsp; // &#39;a&#39; maps to &quot;ei&quot;, &#39;b&#39; maps to &quot;zf&quot;, &#39;c&#39; maps to &quot;ei&quot;, and &#39;d&#39; maps to &quot;am&quot;.
65-
encrypter.decrypt(&quot;eizfeiam&quot;); // return 2.
66-
// &quot;ei&quot; can map to &#39;a&#39; or &#39;c&#39;, &quot;zf&quot; maps to &#39;b&#39;, and &quot;am&quot; maps to &#39;d&#39;.
67-
// Thus, the possible strings after decryption are &quot;abad&quot;, &quot;cbad&quot;, &quot;abcd&quot;, and &quot;cbcd&quot;.
65+
encrypter.decrypt(&quot;eizfeiam&quot;); // return 2.
66+
// &quot;ei&quot; can map to &#39;a&#39; or &#39;c&#39;, &quot;zf&quot; maps to &#39;b&#39;, and &quot;am&quot; maps to &#39;d&#39;.
67+
// Thus, the possible strings after decryption are &quot;abad&quot;, &quot;cbad&quot;, &quot;abcd&quot;, and &quot;cbcd&quot;.
6868
// 2 of those strings, &quot;abad&quot; and &quot;abcd&quot;, appear in dictionary, so the answer is 2.
6969
</pre>
7070

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
---
2+
comments: true
3+
difficulty: 简单
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3477.Fruits%20Into%20Baskets%20II/README.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3477. 将水果放入篮子 II](https://leetcode.cn/problems/fruits-into-baskets-ii)
10+
11+
[English Version](/solution/3400-3499/3477.Fruits%20Into%20Baskets%20II/README_EN.md)
12+
13+
## 题目描述
14+
15+
<!-- description:start -->
16+
17+
<p>给你两个长度为 <code>n</code>&nbsp;的整数数组,<code>fruits</code> 和 <code>baskets</code>,其中 <code>fruits[i]</code> 表示第 <code>i</code>&nbsp;种水果的 <strong>数量</strong>,<code>baskets[j]</code> 表示第 <code>j</code>&nbsp;个篮子的 <strong>容量</strong>。</p>
18+
19+
<p>你需要对 <code>fruits</code> 数组从左到右按照以下规则放置水果:</p>
20+
21+
<ul>
22+
<li>每种水果必须放入第一个 <strong>容量大于等于</strong> 该水果数量的 <strong>最左侧可用篮子</strong> 中。</li>
23+
<li>每个篮子只能装 <b>一种</b> 水果。</li>
24+
<li>如果一种水果 <b>无法放入</b> 任何篮子,它将保持 <b>未放置</b>。</li>
25+
</ul>
26+
27+
<p>返回所有可能分配完成后,剩余未放置的水果种类的数量。</p>
28+
29+
<p>&nbsp;</p>
30+
31+
<p><strong class="example">示例 1</strong></p>
32+
33+
<div class="example-block">
34+
<p><strong>输入:</strong> <span class="example-io">fruits = [4,2,5], baskets = [3,5,4]</span></p>
35+
36+
<p><strong>输出:</strong> <span class="example-io">1</span></p>
37+
38+
<p><strong>解释:</strong></p>
39+
40+
<ul>
41+
<li><code>fruits[0] = 4</code> 放入 <code>baskets[1] = 5</code>。</li>
42+
<li><code>fruits[1] = 2</code> 放入 <code>baskets[0] = 3</code>。</li>
43+
<li><code>fruits[2] = 5</code> 无法放入 <code>baskets[2] = 4</code>。</li>
44+
</ul>
45+
46+
<p>由于有一种水果未放置,我们返回 1。</p>
47+
</div>
48+
49+
<p><strong class="example">示例 2</strong></p>
50+
51+
<div class="example-block">
52+
<p><strong>输入:</strong> <span class="example-io">fruits = [3,6,1], baskets = [6,4,7]</span></p>
53+
54+
<p><strong>输出:</strong> <span class="example-io">0</span></p>
55+
56+
<p><strong>解释:</strong></p>
57+
58+
<ul>
59+
<li><code>fruits[0] = 3</code> 放入 <code>baskets[0] = 6</code>。</li>
60+
<li><code>fruits[1] = 6</code> 无法放入 <code>baskets[1] = 4</code>(容量不足),但可以放入下一个可用的篮子 <code>baskets[2] = 7</code>。</li>
61+
<li><code>fruits[2] = 1</code> 放入 <code>baskets[1] = 4</code>。</li>
62+
</ul>
63+
64+
<p>由于所有水果都已成功放置,我们返回 0。</p>
65+
</div>
66+
67+
<p>&nbsp;</p>
68+
69+
<p><b>提示:</b></p>
70+
71+
<ul>
72+
<li><code>n == fruits.length == baskets.length</code></li>
73+
<li><code>1 &lt;= n &lt;= 100</code></li>
74+
<li><code>1 &lt;= fruits[i], baskets[i] &lt;= 1000</code></li>
75+
</ul>
76+
77+
<!-- description:end -->
78+
79+
## 解法
80+
81+
<!-- solution:start -->
82+
83+
### 方法一
84+
85+
<!-- tabs:start -->
86+
87+
#### Python3
88+
89+
```python
90+
91+
```
92+
93+
#### Java
94+
95+
```java
96+
97+
```
98+
99+
#### C++
100+
101+
```cpp
102+
103+
```
104+
105+
#### Go
106+
107+
```go
108+
109+
```
110+
111+
<!-- tabs:end -->
112+
113+
<!-- solution:end -->
114+
115+
<!-- problem:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
---
2+
comments: true
3+
difficulty: Easy
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3477.Fruits%20Into%20Baskets%20II/README_EN.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3477. Fruits Into Baskets II](https://leetcode.com/problems/fruits-into-baskets-ii)
10+
11+
[中文文档](/solution/3400-3499/3477.Fruits%20Into%20Baskets%20II/README.md)
12+
13+
## Description
14+
15+
<!-- description:start -->
16+
17+
<p>You are given two arrays of integers, <code>fruits</code> and <code>baskets</code>, each of length <code>n</code>, where <code>fruits[i]</code> represents the <strong>quantity</strong> of the <code>i<sup>th</sup></code> type of fruit, and <code>baskets[j]</code> represents the <strong>capacity</strong> of the <code>j<sup>th</sup></code> basket.</p>
18+
19+
<p>From left to right, place the fruits according to these rules:</p>
20+
21+
<ul>
22+
<li>Each fruit type must be placed in the <strong>leftmost available basket</strong> with a capacity <strong>greater than or equal</strong> to the quantity of that fruit type.</li>
23+
<li>Each basket can hold <b>only one</b> type of fruit.</li>
24+
<li>If a fruit type <b>cannot be placed</b> in any basket, it remains <b>unplaced</b>.</li>
25+
</ul>
26+
27+
<p>Return the number of fruit types that remain unplaced after all possible allocations are made.</p>
28+
29+
<p>&nbsp;</p>
30+
<p><strong class="example">Example 1:</strong></p>
31+
32+
<div class="example-block">
33+
<p><strong>Input:</strong> <span class="example-io">fruits = [4,2,5], baskets = [3,5,4]</span></p>
34+
35+
<p><strong>Output:</strong> <span class="example-io">1</span></p>
36+
37+
<p><strong>Explanation:</strong></p>
38+
39+
<ul>
40+
<li><code>fruits[0] = 4</code> is placed in <code>baskets[1] = 5</code>.</li>
41+
<li><code>fruits[1] = 2</code> is placed in <code>baskets[0] = 3</code>.</li>
42+
<li><code>fruits[2] = 5</code> cannot be placed in <code>baskets[2] = 4</code>.</li>
43+
</ul>
44+
45+
<p>Since one fruit type remains unplaced, we return 1.</p>
46+
</div>
47+
48+
<p><strong class="example">Example 2:</strong></p>
49+
50+
<div class="example-block">
51+
<p><strong>Input:</strong> <span class="example-io">fruits = [3,6,1], baskets = [6,4,7]</span></p>
52+
53+
<p><strong>Output:</strong> <span class="example-io">0</span></p>
54+
55+
<p><strong>Explanation:</strong></p>
56+
57+
<ul>
58+
<li><code>fruits[0] = 3</code> is placed in <code>baskets[0] = 6</code>.</li>
59+
<li><code>fruits[1] = 6</code> cannot be placed in <code>baskets[1] = 4</code> (insufficient capacity) but can be placed in the next available basket, <code>baskets[2] = 7</code>.</li>
60+
<li><code>fruits[2] = 1</code> is placed in <code>baskets[1] = 4</code>.</li>
61+
</ul>
62+
63+
<p>Since all fruits are successfully placed, we return 0.</p>
64+
</div>
65+
66+
<p>&nbsp;</p>
67+
<p><strong>Constraints:</strong></p>
68+
69+
<ul>
70+
<li><code>n == fruits.length == baskets.length</code></li>
71+
<li><code>1 &lt;= n &lt;= 100</code></li>
72+
<li><code>1 &lt;= fruits[i], baskets[i] &lt;= 1000</code></li>
73+
</ul>
74+
75+
<!-- description:end -->
76+
77+
## Solutions
78+
79+
<!-- solution:start -->
80+
81+
### Solution 1
82+
83+
<!-- tabs:start -->
84+
85+
#### Python3
86+
87+
```python
88+
89+
```
90+
91+
#### Java
92+
93+
```java
94+
95+
```
96+
97+
#### C++
98+
99+
```cpp
100+
101+
```
102+
103+
#### Go
104+
105+
```go
106+
107+
```
108+
109+
<!-- tabs:end -->
110+
111+
<!-- solution:end -->
112+
113+
<!-- problem:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
---
2+
comments: true
3+
difficulty: 中等
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3478.Choose%20K%20Elements%20With%20Maximum%20Sum/README.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3478. 选出和最大的 K 个元素](https://leetcode.cn/problems/choose-k-elements-with-maximum-sum)
10+
11+
[English Version](/solution/3400-3499/3478.Choose%20K%20Elements%20With%20Maximum%20Sum/README_EN.md)
12+
13+
## 题目描述
14+
15+
<!-- description:start -->
16+
17+
<p>给你两个整数数组,<code>nums1</code> 和 <code>nums2</code>,长度均为 <code>n</code>,以及一个正整数 <code>k</code> 。</p>
18+
19+
<p>对从 <code>0</code> 到 <code>n - 1</code> 每个下标 <code>i</code> ,执行下述操作:</p>
20+
21+
<ul>
22+
<li>找出所有满足 <code>nums1[j]</code> 小于 <code>nums1[i]</code> 的下标 <code>j</code> 。</li>
23+
<li>从这些下标对应的 <code>nums2[j]</code> 中选出 <strong>至多</strong> <code>k</code> 个,并 <strong>最大化</strong> 这些值的总和作为结果。</li>
24+
</ul>
25+
26+
<p>返回一个长度为 <code>n</code> 的数组 <code>answer</code> ,其中 <code>answer[i]</code> 表示对应下标 <code>i</code> 的结果。</p>
27+
28+
<p>&nbsp;</p>
29+
30+
<p><strong class="example">示例 1:</strong></p>
31+
32+
<div class="example-block">
33+
<p><strong>输入:</strong><span class="example-io">nums1 = [4,2,1,5,3], nums2 = [10,20,30,40,50], k = 2</span></p>
34+
35+
<p><strong>输出:</strong><span class="example-io">[80,30,0,80,50]</span></p>
36+
37+
<p><strong>解释:</strong></p>
38+
39+
<ul>
40+
<li>对于 <code>i = 0</code> :满足 <code>nums1[j] &lt; nums1[0]</code> 的下标为 <code>[1, 2, 4]</code> ,选出其中值最大的两个,结果为 <code>50 + 30 = 80</code> 。</li>
41+
<li>对于 <code>i = 1</code> :满足 <code>nums1[j] &lt; nums1[1]</code> 的下标为 <code>[2]</code> ,只能选择这个值,结果为 <code>30</code> 。</li>
42+
<li>对于 <code>i = 2</code> :不存在满足 <code>nums1[j] &lt; nums1[2]</code> 的下标,结果为 <code>0</code> 。</li>
43+
<li>对于 <code>i = 3</code> :满足 <code>nums1[j] &lt; nums1[3]</code> 的下标为 <code>[0, 1, 2, 4]</code> ,选出其中值最大的两个,结果为 <code>50 + 30 = 80</code> 。</li>
44+
<li>对于 <code>i = 4</code> :满足 <code>nums1[j] &lt; nums1[4]</code> 的下标为 <code>[1, 2]</code> ,选出其中值最大的两个,结果为 <code>30 + 20 = 50</code> 。</li>
45+
</ul>
46+
</div>
47+
48+
<p><strong class="example">示例 2:</strong></p>
49+
50+
<div class="example-block">
51+
<p><strong>输入:</strong><span class="example-io">nums1 = [2,2,2,2], nums2 = [3,1,2,3], k = 1</span></p>
52+
53+
<p><strong>输出:</strong><span class="example-io">[0,0,0,0]</span></p>
54+
55+
<p><strong>解释:</strong>由于 <code>nums1</code> 中的所有元素相等,不存在满足条件 <code>nums1[j] &lt; nums1[i]</code>,所有位置的结果都是 0 。</p>
56+
</div>
57+
58+
<p>&nbsp;</p>
59+
60+
<p><strong>提示:</strong></p>
61+
62+
<ul>
63+
<li><code>n == nums1.length == nums2.length</code></li>
64+
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
65+
<li><code>1 &lt;= nums1[i], nums2[i] &lt;= 10<sup>6</sup></code></li>
66+
<li><code>1 &lt;= k &lt;= n</code></li>
67+
</ul>
68+
69+
<!-- description:end -->
70+
71+
## 解法
72+
73+
<!-- solution:start -->
74+
75+
### 方法一
76+
77+
<!-- tabs:start -->
78+
79+
#### Python3
80+
81+
```python
82+
83+
```
84+
85+
#### Java
86+
87+
```java
88+
89+
```
90+
91+
#### C++
92+
93+
```cpp
94+
95+
```
96+
97+
#### Go
98+
99+
```go
100+
101+
```
102+
103+
<!-- tabs:end -->
104+
105+
<!-- solution:end -->
106+
107+
<!-- problem:end -->

0 commit comments

Comments
 (0)