mirror of https://github.com/doocs/leetcode.git
chore: update lc problems (#4336)
This commit is contained in:
parent
2e5b60f0fa
commit
1bc9bd3300
|
|
@ -17,4 +17,4 @@ def department_highest_salary(
|
|||
result = top_earners[['name_y', 'name_x', 'salary']].copy()
|
||||
result.columns = ['Department', 'Employee', 'Salary']
|
||||
|
||||
return result
|
||||
return result
|
||||
|
|
|
|||
|
|
@ -202,14 +202,14 @@ impl Solution {
|
|||
let n = nums.len();
|
||||
let mut f = vec![vec![false; m + 1]; n + 1];
|
||||
f[0][0] = true;
|
||||
|
||||
|
||||
for i in 1..=n {
|
||||
let x = nums[i - 1] as usize;
|
||||
for j in 0..=m {
|
||||
f[i][j] = f[i - 1][j] || (j >= x && f[i - 1][j - x]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
f[n][m]
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -201,14 +201,14 @@ impl Solution {
|
|||
let n = nums.len();
|
||||
let mut f = vec![vec![false; m + 1]; n + 1];
|
||||
f[0][0] = true;
|
||||
|
||||
|
||||
for i in 1..=n {
|
||||
let x = nums[i - 1] as usize;
|
||||
for j in 0..=m {
|
||||
f[i][j] = f[i - 1][j] || (j >= x && f[i - 1][j - x]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
f[n][m]
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,14 +8,14 @@ impl Solution {
|
|||
let n = nums.len();
|
||||
let mut f = vec![vec![false; m + 1]; n + 1];
|
||||
f[0][0] = true;
|
||||
|
||||
|
||||
for i in 1..=n {
|
||||
let x = nums[i - 1] as usize;
|
||||
for j in 0..=m {
|
||||
f[i][j] = f[i - 1][j] || (j >= x && f[i - 1][j - x]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
f[n][m]
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ class Solution:
|
|||
s = set(deadends)
|
||||
if '0000' in s:
|
||||
return -1
|
||||
q = deque([('0000')])
|
||||
q = deque(['0000'])
|
||||
s.add('0000')
|
||||
ans = 0
|
||||
while q:
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ class Solution:
|
|||
|
||||
def bfs():
|
||||
m1, m2 = {"0000": 0}, {target: 0}
|
||||
q1, q2 = deque([('0000')]), deque([(target)])
|
||||
q1, q2 = deque(['0000']), deque([target])
|
||||
while q1 and q2:
|
||||
t = extend(m1, m2, q1) if len(q1) <= len(q2) else extend(m2, m1, q2)
|
||||
if t != -1:
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@ class Solution:
|
|||
if start == end:
|
||||
return 0
|
||||
vis = {start}
|
||||
q = deque([(start)])
|
||||
q = deque([start])
|
||||
ans = 0
|
||||
while q:
|
||||
ans += 1
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
"""
|
||||
This is the custom function interface.
|
||||
You should not implement it, or speculate about its implementation
|
||||
class CustomFunction:
|
||||
# Returns f(x, y) for any given positive integers x and y.
|
||||
# Note that f(x, y) is increasing with respect to both x and y.
|
||||
# i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)
|
||||
def f(self, x, y):
|
||||
This is the custom function interface.
|
||||
You should not implement it, or speculate about its implementation
|
||||
class CustomFunction:
|
||||
# Returns f(x, y) for any given positive integers x and y.
|
||||
# Note that f(x, y) is increasing with respect to both x and y.
|
||||
# i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)
|
||||
def f(self, x, y):
|
||||
|
||||
"""
|
||||
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
"""
|
||||
This is the custom function interface.
|
||||
You should not implement it, or speculate about its implementation
|
||||
class CustomFunction:
|
||||
# Returns f(x, y) for any given positive integers x and y.
|
||||
# Note that f(x, y) is increasing with respect to both x and y.
|
||||
# i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)
|
||||
def f(self, x, y):
|
||||
This is the custom function interface.
|
||||
You should not implement it, or speculate about its implementation
|
||||
class CustomFunction:
|
||||
# Returns f(x, y) for any given positive integers x and y.
|
||||
# Note that f(x, y) is increasing with respect to both x and y.
|
||||
# i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)
|
||||
def f(self, x, y):
|
||||
|
||||
"""
|
||||
|
||||
|
|
|
|||
|
|
@ -1,13 +1,13 @@
|
|||
use std::collections::HashSet;
|
||||
|
||||
impl Solution {
|
||||
pub fn minimum_operations(nums: Vec<i32>) -> i32 {
|
||||
let mut s = HashSet::new();
|
||||
for i in (0..nums.len()).rev() {
|
||||
if !s.insert(nums[i]) {
|
||||
return (i / 3) as i32 + 1;
|
||||
}
|
||||
}
|
||||
0
|
||||
}
|
||||
}
|
||||
use std::collections::HashSet;
|
||||
|
||||
impl Solution {
|
||||
pub fn minimum_operations(nums: Vec<i32>) -> i32 {
|
||||
let mut s = HashSet::new();
|
||||
for i in (0..nums.len()).rev() {
|
||||
if !s.insert(nums[i]) {
|
||||
return (i / 3) as i32 + 1;
|
||||
}
|
||||
}
|
||||
0
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3506.Fi
|
|||
|
||||
<!-- problem:start -->
|
||||
|
||||
# [3506. 查找消除细菌菌株所需时间 II 🔒](https://leetcode.cn/problems/find-time-required-to-eliminate-bacterial-strains)
|
||||
# [3506. 查找消除细菌菌株所需时间 🔒](https://leetcode.cn/problems/find-time-required-to-eliminate-bacterial-strains)
|
||||
|
||||
[English Version](/solution/3500-3599/3506.Find%20Time%20Required%20to%20Eliminate%20Bacterial%20Strains/README_EN.md)
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,147 @@
|
|||
---
|
||||
comments: true
|
||||
difficulty: 中等
|
||||
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3511.Make%20a%20Positive%20Array/README.md
|
||||
---
|
||||
|
||||
<!-- problem:start -->
|
||||
|
||||
# [3511. 构造正数组 🔒](https://leetcode.cn/problems/make-a-positive-array)
|
||||
|
||||
[English Version](/solution/3500-3599/3511.Make%20a%20Positive%20Array/README_EN.md)
|
||||
|
||||
## 题目描述
|
||||
|
||||
<!-- description:start -->
|
||||
|
||||
<p>给定一个数组 <code>nums</code>。一个数组被认为是 <strong>正</strong> 的,如果每个包含 <strong>超过两个</strong> 元素的 <strong><span data-keyword="subarray">子数组</span></strong> 的所有数字之和都是正数。</p>
|
||||
|
||||
<p>您可以执行以下操作任意多次:</p>
|
||||
|
||||
<ul>
|
||||
<li>用 -10<sup>18</sup> 和 10<sup>18 </sup>之间的任意整数替换 <code>nums</code> 中的 <strong>一个</strong> 元素。</li>
|
||||
</ul>
|
||||
|
||||
<p>找到使 <code>nums</code> 变为正数组所需的最小操作数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><span class="example-io"><b>输入:</b>nums = [-10,15,-12]</span></p>
|
||||
|
||||
<p><span class="example-io"><b>输出:</b>1</span></p>
|
||||
|
||||
<p><strong>解释:</strong></p>
|
||||
|
||||
<p>唯一有超过 2 个元素的子数组是这个数组本身。所有元素的和是 <code>(-10) + 15 + (-12) = -7</code>。通过将 <code>nums[0]</code> 替换为 0,新的和变为 <code>0 + 15 + (-12) = 3</code>。因此,现在数组是正的。</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><span class="example-io"><b>输入:</b>nums = [-1,-2,3,-1,2,6]</span></p>
|
||||
|
||||
<p><strong>输出:</strong><span class="example-io">1</span></p>
|
||||
|
||||
<p><strong>解释:</strong></p>
|
||||
|
||||
<p>具有 2 个以上元素且和非正的子数组是:</p>
|
||||
|
||||
<table style="border: 1px solid black;">
|
||||
<tbody>
|
||||
<tr>
|
||||
<th style="border: 1px solid black;">子数组下标</th>
|
||||
<th style="border: 1px solid black;">子数组</th>
|
||||
<th style="border: 1px solid black;">和</th>
|
||||
<th style="border: 1px solid black;">替换后的子数组(令 nums[1] = 1)</th>
|
||||
<th style="border: 1px solid black;">新的和</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="border: 1px solid black;">nums[0...2]</td>
|
||||
<td style="border: 1px solid black;">[-1, -2, 3]</td>
|
||||
<td style="border: 1px solid black;">0</td>
|
||||
<td style="border: 1px solid black;">[-1, 1, 3]</td>
|
||||
<td style="border: 1px solid black;">3</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="border: 1px solid black;">nums[0...3]</td>
|
||||
<td style="border: 1px solid black;">[-1, -2, 3, -1]</td>
|
||||
<td style="border: 1px solid black;">-1</td>
|
||||
<td style="border: 1px solid black;">[-1, 1, 3, -1]</td>
|
||||
<td style="border: 1px solid black;">2</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="border: 1px solid black;">nums[1...3]</td>
|
||||
<td style="border: 1px solid black;">[-2, 3, -1]</td>
|
||||
<td style="border: 1px solid black;">0</td>
|
||||
<td style="border: 1px solid black;">[1, 3, -1]</td>
|
||||
<td style="border: 1px solid black;">3</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<p>因此,<code>nums</code> 在一次操作后是正的。</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><span class="example-io"><b>输入:</b>nums = [1,2,3]</span></p>
|
||||
|
||||
<p><span class="example-io"><b>输出:</b>0</span></p>
|
||||
|
||||
<p><strong>解释:</strong></p>
|
||||
|
||||
<p>数组已经是正的,所以不需要操作。</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
||||
|
||||
<!-- description:end -->
|
||||
|
||||
## 解法
|
||||
|
||||
<!-- solution:start -->
|
||||
|
||||
### 方法一
|
||||
|
||||
<!-- tabs:start -->
|
||||
|
||||
#### Python3
|
||||
|
||||
```python
|
||||
|
||||
```
|
||||
|
||||
#### Java
|
||||
|
||||
```java
|
||||
|
||||
```
|
||||
|
||||
#### C++
|
||||
|
||||
```cpp
|
||||
|
||||
```
|
||||
|
||||
#### Go
|
||||
|
||||
```go
|
||||
|
||||
```
|
||||
|
||||
<!-- tabs:end -->
|
||||
|
||||
<!-- solution:end -->
|
||||
|
||||
<!-- problem:end -->
|
||||
|
|
@ -0,0 +1,145 @@
|
|||
---
|
||||
comments: true
|
||||
difficulty: Medium
|
||||
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3511.Make%20a%20Positive%20Array/README_EN.md
|
||||
---
|
||||
|
||||
<!-- problem:start -->
|
||||
|
||||
# [3511. Make a Positive Array 🔒](https://leetcode.com/problems/make-a-positive-array)
|
||||
|
||||
[中文文档](/solution/3500-3599/3511.Make%20a%20Positive%20Array/README.md)
|
||||
|
||||
## Description
|
||||
|
||||
<!-- description:start -->
|
||||
|
||||
<p>You are given an array <code>nums</code>. An array is considered <strong>positive</strong> if the sum of all numbers in each <strong><span data-keyword="subarray">subarray</span></strong> with <strong>more than two</strong> elements is positive.</p>
|
||||
|
||||
<p>You can perform the following operation any number of times:</p>
|
||||
|
||||
<ul>
|
||||
<li>Replace <strong>one</strong> element in <code>nums</code> with any integer between -10<sup>18</sup> and 10<sup>18</sup>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Find the <strong>minimum</strong> number of operations needed to make <code>nums</code> <strong>positive</strong>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [-10,15,-12]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">1</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The only subarray with more than 2 elements is the array itself. The sum of all elements is <code>(-10) + 15 + (-12) = -7</code>. By replacing <code>nums[0]</code> with 0, the new sum becomes <code>0 + 15 + (-12) = 3</code>. Thus, the array is now positive.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [-1,-2,3,-1,2,6]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">1</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The only subarrays with more than 2 elements and a non-positive sum are:</p>
|
||||
|
||||
<table style="border: 1px solid black;">
|
||||
<tbody>
|
||||
<tr>
|
||||
<th style="border: 1px solid black;">Subarray Indices</th>
|
||||
<th style="border: 1px solid black;">Subarray</th>
|
||||
<th style="border: 1px solid black;">Sum</th>
|
||||
<th style="border: 1px solid black;">Subarray After Replacement (Set nums[1] = 1)</th>
|
||||
<th style="border: 1px solid black;">New Sum</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="border: 1px solid black;">nums[0...2]</td>
|
||||
<td style="border: 1px solid black;">[-1, -2, 3]</td>
|
||||
<td style="border: 1px solid black;">0</td>
|
||||
<td style="border: 1px solid black;">[-1, 1, 3]</td>
|
||||
<td style="border: 1px solid black;">3</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="border: 1px solid black;">nums[0...3]</td>
|
||||
<td style="border: 1px solid black;">[-1, -2, 3, -1]</td>
|
||||
<td style="border: 1px solid black;">-1</td>
|
||||
<td style="border: 1px solid black;">[-1, 1, 3, -1]</td>
|
||||
<td style="border: 1px solid black;">2</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="border: 1px solid black;">nums[1...3]</td>
|
||||
<td style="border: 1px solid black;">[-2, 3, -1]</td>
|
||||
<td style="border: 1px solid black;">0</td>
|
||||
<td style="border: 1px solid black;">[1, 3, -1]</td>
|
||||
<td style="border: 1px solid black;">3</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<p>Thus, <code>nums</code> is positive after one operation.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">0</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The array is already positive, so no operations are needed.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
||||
|
||||
<!-- description:end -->
|
||||
|
||||
## Solutions
|
||||
|
||||
<!-- solution:start -->
|
||||
|
||||
### Solution 1
|
||||
|
||||
<!-- tabs:start -->
|
||||
|
||||
#### Python3
|
||||
|
||||
```python
|
||||
|
||||
```
|
||||
|
||||
#### Java
|
||||
|
||||
```java
|
||||
|
||||
```
|
||||
|
||||
#### C++
|
||||
|
||||
```cpp
|
||||
|
||||
```
|
||||
|
||||
#### Go
|
||||
|
||||
```go
|
||||
|
||||
```
|
||||
|
||||
<!-- tabs:end -->
|
||||
|
||||
<!-- solution:end -->
|
||||
|
||||
<!-- problem:end -->
|
||||
|
|
@ -3517,11 +3517,12 @@
|
|||
| 3504 | [子字符串连接后的最长回文串 II](/solution/3500-3599/3504.Longest%20Palindrome%20After%20Substring%20Concatenation%20II/README.md) | `双指针`,`字符串`,`动态规划` | 困难 | 第 443 场周赛 |
|
||||
| 3505 | [使 K 个子数组内元素相等的最少操作数](/solution/3500-3599/3505.Minimum%20Operations%20to%20Make%20Elements%20Within%20K%20Subarrays%20Equal/README.md) | `数组`,`哈希表`,`数学`,`动态规划`,`滑动窗口`,`堆(优先队列)` | 困难 | 第 443 场周赛 |
|
||||
| 3506 | [Find Time Required to Eliminate Bacterial Strains II](/solution/3500-3599/3506.Find%20Time%20Required%20to%20Eliminate%20Bacterial%20Strains%20II/README.md) | | 困难 | 🔒 |
|
||||
| 3506 | [查找消除细菌菌株所需时间 II](/solution/3500-3599/3506.Find%20Time%20Required%20to%20Eliminate%20Bacterial%20Strains/README.md) | | 困难 | 🔒 |
|
||||
| 3506 | [查找消除细菌菌株所需时间](/solution/3500-3599/3506.Find%20Time%20Required%20to%20Eliminate%20Bacterial%20Strains/README.md) | | 困难 | 🔒 |
|
||||
| 3507 | [移除最小数对使数组有序 I](/solution/3500-3599/3507.Minimum%20Pair%20Removal%20to%20Sort%20Array%20I/README.md) | | 简单 | 第 444 场周赛 |
|
||||
| 3508 | [设计路由器](/solution/3500-3599/3508.Implement%20Router/README.md) | | 中等 | 第 444 场周赛 |
|
||||
| 3509 | [最大化交错和为 K 的子序列乘积](/solution/3500-3599/3509.Maximum%20Product%20of%20Subsequences%20With%20an%20Alternating%20Sum%20Equal%20to%20K/README.md) | | 困难 | 第 444 场周赛 |
|
||||
| 3510 | [移除最小数对使数组有序 II](/solution/3500-3599/3510.Minimum%20Pair%20Removal%20to%20Sort%20Array%20II/README.md) | | 困难 | 第 444 场周赛 |
|
||||
| 3511 | [构造正数组](/solution/3500-3599/3511.Make%20a%20Positive%20Array/README.md) | | 中等 | 🔒 |
|
||||
|
||||
## 版权
|
||||
|
||||
|
|
|
|||
|
|
@ -3520,6 +3520,7 @@ Press <kbd>Control</kbd> + <kbd>F</kbd>(or <kbd>Command</kbd> + <kbd>F</kbd> on
|
|||
| 3508 | [Implement Router](/solution/3500-3599/3508.Implement%20Router/README_EN.md) | | Medium | Weekly Contest 444 |
|
||||
| 3509 | [Maximum Product of Subsequences With an Alternating Sum Equal to K](/solution/3500-3599/3509.Maximum%20Product%20of%20Subsequences%20With%20an%20Alternating%20Sum%20Equal%20to%20K/README_EN.md) | | Hard | Weekly Contest 444 |
|
||||
| 3510 | [Minimum Pair Removal to Sort Array II](/solution/3500-3599/3510.Minimum%20Pair%20Removal%20to%20Sort%20Array%20II/README_EN.md) | | Hard | Weekly Contest 444 |
|
||||
| 3511 | [Make a Positive Array](/solution/3500-3599/3511.Make%20a%20Positive%20Array/README_EN.md) | | Medium | 🔒 |
|
||||
|
||||
## Copyright
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue