diff --git a/solution/0100-0199/0184.Department Highest Salary/Solution.py b/solution/0100-0199/0184.Department Highest Salary/Solution.py index 278093fdb7117..5b661d76ea904 100644 --- a/solution/0100-0199/0184.Department Highest Salary/Solution.py +++ b/solution/0100-0199/0184.Department Highest Salary/Solution.py @@ -17,4 +17,4 @@ def department_highest_salary( result = top_earners[['name_y', 'name_x', 'salary']].copy() result.columns = ['Department', 'Employee', 'Salary'] - return result \ No newline at end of file + return result diff --git a/solution/0400-0499/0416.Partition Equal Subset Sum/README.md b/solution/0400-0499/0416.Partition Equal Subset Sum/README.md index 0f7d31e783efc..b31c4b54d636d 100644 --- a/solution/0400-0499/0416.Partition Equal Subset Sum/README.md +++ b/solution/0400-0499/0416.Partition Equal Subset Sum/README.md @@ -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] } } diff --git a/solution/0400-0499/0416.Partition Equal Subset Sum/README_EN.md b/solution/0400-0499/0416.Partition Equal Subset Sum/README_EN.md index 85c913a408777..ad13f92581249 100644 --- a/solution/0400-0499/0416.Partition Equal Subset Sum/README_EN.md +++ b/solution/0400-0499/0416.Partition Equal Subset Sum/README_EN.md @@ -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] } } diff --git a/solution/0400-0499/0416.Partition Equal Subset Sum/Solution.rs b/solution/0400-0499/0416.Partition Equal Subset Sum/Solution.rs index a2acff43385bc..1d4cdd5f8fbcb 100644 --- a/solution/0400-0499/0416.Partition Equal Subset Sum/Solution.rs +++ b/solution/0400-0499/0416.Partition Equal Subset Sum/Solution.rs @@ -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] } } diff --git a/solution/0700-0799/0752.Open the Lock/Solution.py b/solution/0700-0799/0752.Open the Lock/Solution.py index 650d7757d7db7..76e24b06bb1ce 100644 --- a/solution/0700-0799/0752.Open the Lock/Solution.py +++ b/solution/0700-0799/0752.Open the Lock/Solution.py @@ -17,7 +17,7 @@ def next(s): s = set(deadends) if '0000' in s: return -1 - q = deque([('0000')]) + q = deque(['0000']) s.add('0000') ans = 0 while q: diff --git a/solution/0700-0799/0752.Open the Lock/Solution2.py b/solution/0700-0799/0752.Open the Lock/Solution2.py index 6a623bcf80b3b..87d7e03b99365 100644 --- a/solution/0700-0799/0752.Open the Lock/Solution2.py +++ b/solution/0700-0799/0752.Open the Lock/Solution2.py @@ -27,7 +27,7 @@ def extend(m1, m2, q): 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: diff --git a/solution/0700-0799/0773.Sliding Puzzle/Solution.py b/solution/0700-0799/0773.Sliding Puzzle/Solution.py index 897f9d185a622..9fe4e105727a7 100644 --- a/solution/0700-0799/0773.Sliding Puzzle/Solution.py +++ b/solution/0700-0799/0773.Sliding Puzzle/Solution.py @@ -29,7 +29,7 @@ def f(): if start == end: return 0 vis = {start} - q = deque([(start)]) + q = deque([start]) ans = 0 while q: ans += 1 diff --git a/solution/1200-1299/1237.Find Positive Integer Solution for a Given Equation/Solution.py b/solution/1200-1299/1237.Find Positive Integer Solution for a Given Equation/Solution.py index 5ded8ceac1b32..55795052d710e 100644 --- a/solution/1200-1299/1237.Find Positive Integer Solution for a Given Equation/Solution.py +++ b/solution/1200-1299/1237.Find Positive Integer Solution for a Given Equation/Solution.py @@ -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): """ diff --git a/solution/1200-1299/1237.Find Positive Integer Solution for a Given Equation/Solution2.py b/solution/1200-1299/1237.Find Positive Integer Solution for a Given Equation/Solution2.py index 424c1376a9106..c6c5774e3fe87 100644 --- a/solution/1200-1299/1237.Find Positive Integer Solution for a Given Equation/Solution2.py +++ b/solution/1200-1299/1237.Find Positive Integer Solution for a Given Equation/Solution2.py @@ -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): """ diff --git a/solution/3300-3399/3396.Minimum Number of Operations to Make Elements in Array Distinct/Solution.rs b/solution/3300-3399/3396.Minimum Number of Operations to Make Elements in Array Distinct/Solution.rs index f342802788c2d..78e5a37e5f741 100644 --- a/solution/3300-3399/3396.Minimum Number of Operations to Make Elements in Array Distinct/Solution.rs +++ b/solution/3300-3399/3396.Minimum Number of Operations to Make Elements in Array Distinct/Solution.rs @@ -1,13 +1,13 @@ -use std::collections::HashSet; - -impl Solution { - pub fn minimum_operations(nums: Vec) -> 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 - } -} \ No newline at end of file +use std::collections::HashSet; + +impl Solution { + pub fn minimum_operations(nums: Vec) -> 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 + } +} diff --git a/solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains/README.md b/solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains/README.md index 2e81320abb412..726f4d0dc6c8f 100644 --- a/solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains/README.md +++ b/solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains/README.md @@ -6,7 +6,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3506.Fi -# [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) diff --git a/solution/3500-3599/3511.Make a Positive Array/README.md b/solution/3500-3599/3511.Make a Positive Array/README.md new file mode 100644 index 0000000000000..2cf0205532104 --- /dev/null +++ b/solution/3500-3599/3511.Make a Positive Array/README.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 +--- + + + +# [3511. 构造正数组 🔒](https://leetcode.cn/problems/make-a-positive-array) + +[English Version](/solution/3500-3599/3511.Make%20a%20Positive%20Array/README_EN.md) + +## 题目描述 + + + +

给定一个数组 nums。一个数组被认为是 的,如果每个包含 超过两个 元素的 子数组 的所有数字之和都是正数。

+ +

您可以执行以下操作任意多次:

+ + + +

找到使 nums 变为正数组所需的最小操作数。

+ +

 

+ +

示例 1:

+ +
+

输入:nums = [-10,15,-12]

+ +

输出:1

+ +

解释:

+ +

唯一有超过 2 个元素的子数组是这个数组本身。所有元素的和是 (-10) + 15 + (-12) = -7。通过将 nums[0] 替换为 0,新的和变为 0 + 15 + (-12) = 3。因此,现在数组是正的。

+
+ +

示例 2:

+ +
+

输入:nums = [-1,-2,3,-1,2,6]

+ +

输出:1

+ +

解释:

+ +

具有 2 个以上元素且和非正的子数组是:

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
子数组下标子数组替换后的子数组(令 nums[1] = 1)新的和
nums[0...2][-1, -2, 3]0[-1, 1, 3]3
nums[0...3][-1, -2, 3, -1]-1[-1, 1, 3, -1]2
nums[1...3][-2, 3, -1]0[1, 3, -1]3
+ +

因此,nums 在一次操作后是正的。

+
+ +

示例 3:

+ +
+

输入:nums = [1,2,3]

+ +

输出:0

+ +

解释:

+ +

数组已经是正的,所以不需要操作。

+
+ +

 

+ +

提示:

+ + + + + +## 解法 + + + +### 方法一 + + + +#### Python3 + +```python + +``` + +#### Java + +```java + +``` + +#### C++ + +```cpp + +``` + +#### Go + +```go + +``` + + + + + + diff --git a/solution/3500-3599/3511.Make a Positive Array/README_EN.md b/solution/3500-3599/3511.Make a Positive Array/README_EN.md new file mode 100644 index 0000000000000..128f81a7eaf9f --- /dev/null +++ b/solution/3500-3599/3511.Make a Positive Array/README_EN.md @@ -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 +--- + + + +# [3511. Make a Positive Array 🔒](https://leetcode.com/problems/make-a-positive-array) + +[中文文档](/solution/3500-3599/3511.Make%20a%20Positive%20Array/README.md) + +## Description + + + +

You are given an array nums. An array is considered positive if the sum of all numbers in each subarray with more than two elements is positive.

+ +

You can perform the following operation any number of times:

+ + + +

Find the minimum number of operations needed to make nums positive.

+ +

 

+

Example 1:

+ +
+

Input: nums = [-10,15,-12]

+ +

Output: 1

+ +

Explanation:

+ +

The only subarray with more than 2 elements is the array itself. The sum of all elements is (-10) + 15 + (-12) = -7. By replacing nums[0] with 0, the new sum becomes 0 + 15 + (-12) = 3. Thus, the array is now positive.

+
+ +

Example 2:

+ +
+

Input: nums = [-1,-2,3,-1,2,6]

+ +

Output: 1

+ +

Explanation:

+ +

The only subarrays with more than 2 elements and a non-positive sum are:

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Subarray IndicesSubarraySumSubarray After Replacement (Set nums[1] = 1)New Sum
nums[0...2][-1, -2, 3]0[-1, 1, 3]3
nums[0...3][-1, -2, 3, -1]-1[-1, 1, 3, -1]2
nums[1...3][-2, 3, -1]0[1, 3, -1]3
+ +

Thus, nums is positive after one operation.

+
+ +

Example 3:

+ +
+

Input: nums = [1,2,3]

+ +

Output: 0

+ +

Explanation:

+ +

The array is already positive, so no operations are needed.

+
+ +

 

+

Constraints:

+ + + + + +## Solutions + + + +### Solution 1 + + + +#### Python3 + +```python + +``` + +#### Java + +```java + +``` + +#### C++ + +```cpp + +``` + +#### Go + +```go + +``` + + + + + + diff --git a/solution/README.md b/solution/README.md index fcc47a7159eb1..fd95929aef19c 100644 --- a/solution/README.md +++ b/solution/README.md @@ -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) | | 中等 | 🔒 | ## 版权 diff --git a/solution/README_EN.md b/solution/README_EN.md index 73216af8ee38a..ea1a1206d5453 100644 --- a/solution/README_EN.md +++ b/solution/README_EN.md @@ -3520,6 +3520,7 @@ Press Control + F(or Command + F 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