diff --git a/solution/3300-3399/3381.Maximum Subarray Sum With Length Divisible by K/README.md b/solution/3300-3399/3381.Maximum Subarray Sum With Length Divisible by K/README.md index fcdbe78a63445..faf93a022a935 100644 --- a/solution/3300-3399/3381.Maximum Subarray Sum With Length Divisible by K/README.md +++ b/solution/3300-3399/3381.Maximum Subarray Sum With Length Divisible by K/README.md @@ -85,25 +85,99 @@ tags: #### Python3 ```python - +class Solution: + def maxSubarraySum(self, nums: List[int], k: int) -> int: + f = [inf] * k + ans = -inf + s = f[-1] = 0 + for i, x in enumerate(nums): + s += x + ans = max(ans, s - f[i % k]) + f[i % k] = min(f[i % k], s) + return ans ``` #### Java ```java - +class Solution { + public long maxSubarraySum(int[] nums, int k) { + long[] f = new long[k]; + final long inf = 1L << 62; + Arrays.fill(f, inf); + f[k - 1] = 0; + long s = 0; + long ans = -inf; + for (int i = 0; i < nums.length; ++i) { + s += nums[i]; + ans = Math.max(ans, s - f[i % k]); + f[i % k] = Math.min(f[i % k], s); + } + return ans; + } +} ``` #### C++ ```cpp - +class Solution { +public: + long long maxSubarraySum(vector& nums, int k) { + using ll = long long; + ll inf = 1e18; + vector f(k, inf); + ll ans = -inf; + ll s = 0; + f[k - 1] = 0; + for (int i = 0; i < nums.size(); ++i) { + s += nums[i]; + ans = max(ans, s - f[i % k]); + f[i % k] = min(f[i % k], s); + } + return ans; + } +}; ``` #### Go ```go +func maxSubarraySum(nums []int, k int) int64 { + inf := int64(1) << 62 + f := make([]int64, k) + for i := range f { + f[i] = inf + } + f[k-1] = 0 + + var s, ans int64 + ans = -inf + for i := 0; i < len(nums); i++ { + s += int64(nums[i]) + ans = max(ans, s-f[i%k]) + f[i%k] = min(f[i%k], s) + } + + return ans +} +``` +#### TypeScript + +```ts +function maxSubarraySum(nums: number[], k: number): number { + const f: number[] = Array(k).fill(Infinity); + f[k - 1] = 0; + let ans = -Infinity; + let s = 0; + for (let i = 0; i < nums.length; ++i) { + s += nums[i]; + ans = Math.max(ans, s - f[i % k]); + f[i % k] = Math.min(f[i % k], s); + } + return ans; +} ``` diff --git a/solution/3300-3399/3381.Maximum Subarray Sum With Length Divisible by K/README_EN.md b/solution/3300-3399/3381.Maximum Subarray Sum With Length Divisible by K/README_EN.md index 34851c1969635..62088db765355 100644 --- a/solution/3300-3399/3381.Maximum Subarray Sum With Length Divisible by K/README_EN.md +++ b/solution/3300-3399/3381.Maximum Subarray Sum With Length Divisible by K/README_EN.md @@ -82,25 +82,99 @@ tags: #### Python3 ```python - +class Solution: + def maxSubarraySum(self, nums: List[int], k: int) -> int: + f = [inf] * k + ans = -inf + s = f[-1] = 0 + for i, x in enumerate(nums): + s += x + ans = max(ans, s - f[i % k]) + f[i % k] = min(f[i % k], s) + return ans ``` #### Java ```java - +class Solution { + public long maxSubarraySum(int[] nums, int k) { + long[] f = new long[k]; + final long inf = 1L << 62; + Arrays.fill(f, inf); + f[k - 1] = 0; + long s = 0; + long ans = -inf; + for (int i = 0; i < nums.length; ++i) { + s += nums[i]; + ans = Math.max(ans, s - f[i % k]); + f[i % k] = Math.min(f[i % k], s); + } + return ans; + } +} ``` #### C++ ```cpp - +class Solution { +public: + long long maxSubarraySum(vector& nums, int k) { + using ll = long long; + ll inf = 1e18; + vector f(k, inf); + ll ans = -inf; + ll s = 0; + f[k - 1] = 0; + for (int i = 0; i < nums.size(); ++i) { + s += nums[i]; + ans = max(ans, s - f[i % k]); + f[i % k] = min(f[i % k], s); + } + return ans; + } +}; ``` #### Go ```go +func maxSubarraySum(nums []int, k int) int64 { + inf := int64(1) << 62 + f := make([]int64, k) + for i := range f { + f[i] = inf + } + f[k-1] = 0 + + var s, ans int64 + ans = -inf + for i := 0; i < len(nums); i++ { + s += int64(nums[i]) + ans = max(ans, s-f[i%k]) + f[i%k] = min(f[i%k], s) + } + + return ans +} +``` +#### TypeScript + +```ts +function maxSubarraySum(nums: number[], k: number): number { + const f: number[] = Array(k).fill(Infinity); + f[k - 1] = 0; + let ans = -Infinity; + let s = 0; + for (let i = 0; i < nums.length; ++i) { + s += nums[i]; + ans = Math.max(ans, s - f[i % k]); + f[i % k] = Math.min(f[i % k], s); + } + return ans; +} ``` diff --git a/solution/3300-3399/3381.Maximum Subarray Sum With Length Divisible by K/Solution.cpp b/solution/3300-3399/3381.Maximum Subarray Sum With Length Divisible by K/Solution.cpp new file mode 100644 index 0000000000000..74e26bdcffa77 --- /dev/null +++ b/solution/3300-3399/3381.Maximum Subarray Sum With Length Divisible by K/Solution.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + long long maxSubarraySum(vector& nums, int k) { + using ll = long long; + ll inf = 1e18; + vector f(k, inf); + ll ans = -inf; + ll s = 0; + f[k - 1] = 0; + for (int i = 0; i < nums.size(); ++i) { + s += nums[i]; + ans = max(ans, s - f[i % k]); + f[i % k] = min(f[i % k], s); + } + return ans; + } +}; diff --git a/solution/3300-3399/3381.Maximum Subarray Sum With Length Divisible by K/Solution.go b/solution/3300-3399/3381.Maximum Subarray Sum With Length Divisible by K/Solution.go new file mode 100644 index 0000000000000..6ac69c3e2b9cd --- /dev/null +++ b/solution/3300-3399/3381.Maximum Subarray Sum With Length Divisible by K/Solution.go @@ -0,0 +1,18 @@ +func maxSubarraySum(nums []int, k int) int64 { + inf := int64(1) << 62 + f := make([]int64, k) + for i := range f { + f[i] = inf + } + f[k-1] = 0 + + var s, ans int64 + ans = -inf + for i := 0; i < len(nums); i++ { + s += int64(nums[i]) + ans = max(ans, s-f[i%k]) + f[i%k] = min(f[i%k], s) + } + + return ans +} diff --git a/solution/3300-3399/3381.Maximum Subarray Sum With Length Divisible by K/Solution.java b/solution/3300-3399/3381.Maximum Subarray Sum With Length Divisible by K/Solution.java new file mode 100644 index 0000000000000..6aeaf225482ba --- /dev/null +++ b/solution/3300-3399/3381.Maximum Subarray Sum With Length Divisible by K/Solution.java @@ -0,0 +1,16 @@ +class Solution { + public long maxSubarraySum(int[] nums, int k) { + long[] f = new long[k]; + final long inf = 1L << 62; + Arrays.fill(f, inf); + f[k - 1] = 0; + long s = 0; + long ans = -inf; + for (int i = 0; i < nums.length; ++i) { + s += nums[i]; + ans = Math.max(ans, s - f[i % k]); + f[i % k] = Math.min(f[i % k], s); + } + return ans; + } +} diff --git a/solution/3300-3399/3381.Maximum Subarray Sum With Length Divisible by K/Solution.py b/solution/3300-3399/3381.Maximum Subarray Sum With Length Divisible by K/Solution.py new file mode 100644 index 0000000000000..89cfe7e95e14c --- /dev/null +++ b/solution/3300-3399/3381.Maximum Subarray Sum With Length Divisible by K/Solution.py @@ -0,0 +1,10 @@ +class Solution: + def maxSubarraySum(self, nums: List[int], k: int) -> int: + f = [inf] * k + ans = -inf + s = f[-1] = 0 + for i, x in enumerate(nums): + s += x + ans = max(ans, s - f[i % k]) + f[i % k] = min(f[i % k], s) + return ans diff --git a/solution/3300-3399/3381.Maximum Subarray Sum With Length Divisible by K/Solution.ts b/solution/3300-3399/3381.Maximum Subarray Sum With Length Divisible by K/Solution.ts new file mode 100644 index 0000000000000..66e535fd9168d --- /dev/null +++ b/solution/3300-3399/3381.Maximum Subarray Sum With Length Divisible by K/Solution.ts @@ -0,0 +1,12 @@ +function maxSubarraySum(nums: number[], k: number): number { + const f: number[] = Array(k).fill(Infinity); + f[k - 1] = 0; + let ans = -Infinity; + let s = 0; + for (let i = 0; i < nums.length; ++i) { + s += nums[i]; + ans = Math.max(ans, s - f[i % k]); + f[i % k] = Math.min(f[i % k], s); + } + return ans; +} diff --git a/solution/3300-3399/3384.Team Dominance by Pass Success/README_EN.md b/solution/3300-3399/3384.Team Dominance by Pass Success/README_EN.md index a0c685340bf36..dfa999f424271 100644 --- a/solution/3300-3399/3384.Team Dominance by Pass Success/README_EN.md +++ b/solution/3300-3399/3384.Team Dominance by Pass Success/README_EN.md @@ -23,7 +23,7 @@ tags: | Column Name | Type | +-------------+---------+ | player_id | int | -| team_name | varchar | +| team_name | varchar | +-------------+---------+ player_id is the unique key for this table. Each row contains the unique identifier for player and the name of one of the teams participating in that match. diff --git a/solution/3300-3399/3385.Minimum Time to Break Locks II/README.md b/solution/3300-3399/3385.Minimum Time to Break Locks II/README.md new file mode 100644 index 0000000000000..42b87c8294043 --- /dev/null +++ b/solution/3300-3399/3385.Minimum Time to Break Locks II/README.md @@ -0,0 +1,214 @@ +--- +comments: true +difficulty: 困难 +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3385.Minimum%20Time%20to%20Break%20Locks%20II/README.md +--- + + + +# [3385. Minimum Time to Break Locks II 🔒](https://leetcode.cn/problems/minimum-time-to-break-locks-ii) + +[English Version](/solution/3300-3399/3385.Minimum%20Time%20to%20Break%20Locks%20II/README_EN.md) + +## 题目描述 + + + +

Bob is stuck in a dungeon and must break n locks, each requiring some amount of energy to break. The required energy for each lock is stored in an array called strength where strength[i] indicates the energy needed to break the ith lock.

+ +

To break a lock, Bob uses a sword with the following characteristics:

+ +
    +
  • The initial energy of the sword is 0.
  • +
  • The initial factor X by which the energy of the sword increases is 1.
  • +
  • Every minute, the energy of the sword increases by the current factor X.
  • +
  • To break the ith lock, the energy of the sword must reach at least strength[i].
  • +
  • After breaking a lock, the energy of the sword resets to 0, and the factor X increases by 1.
  • +
+ +

Your task is to determine the minimum time in minutes required for Bob to break all n locks and escape the dungeon.

+ +

Return the minimum time required for Bob to break all n locks.

+ +

 

+

Example 1:

+ +
+

Input: strength = [3,4,1]

+ +

Output: 4

+ +

Explanation:

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
TimeEnergyXActionUpdated X
001Nothing1
111Break 3rd Lock2
222Nothing2
342Break 2nd Lock3
433Break 1st Lock3
+ +

The locks cannot be broken in less than 4 minutes; thus, the answer is 4.

+
+ +

Example 2:

+ +
+

Input: strength = [2,5,4]

+ +

Output: 6

+ +

Explanation:

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
TimeEnergyXActionUpdated X
001Nothing1
111Nothing1
221Break 1st Lock2
322Nothing2
442Break 3rd Lock3
533Nothing3
663Break 2nd Lock4
+ +

The locks cannot be broken in less than 6 minutes; thus, the answer is 6.

+
+ +

 

+

Constraints:

+ +
    +
  • n == strength.length
  • +
  • 1 <= n <= 80
  • +
  • 1 <= strength[i] <= 106
  • +
  • n == strength.length
  • +
+ + + +## 解法 + + + +### 方法一 + + + +#### Python3 + +```python + +``` + +#### Java + +```java + +``` + +#### C++ + +```cpp + +``` + +#### Go + +```go + +``` + + + + + + diff --git a/solution/3300-3399/3385.Minimum Time to Break Locks II/README_EN.md b/solution/3300-3399/3385.Minimum Time to Break Locks II/README_EN.md new file mode 100644 index 0000000000000..3e636df4a7773 --- /dev/null +++ b/solution/3300-3399/3385.Minimum Time to Break Locks II/README_EN.md @@ -0,0 +1,214 @@ +--- +comments: true +difficulty: Hard +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3385.Minimum%20Time%20to%20Break%20Locks%20II/README_EN.md +--- + + + +# [3385. Minimum Time to Break Locks II 🔒](https://leetcode.com/problems/minimum-time-to-break-locks-ii) + +[中文文档](/solution/3300-3399/3385.Minimum%20Time%20to%20Break%20Locks%20II/README.md) + +## Description + + + +

Bob is stuck in a dungeon and must break n locks, each requiring some amount of energy to break. The required energy for each lock is stored in an array called strength where strength[i] indicates the energy needed to break the ith lock.

+ +

To break a lock, Bob uses a sword with the following characteristics:

+ +
    +
  • The initial energy of the sword is 0.
  • +
  • The initial factor X by which the energy of the sword increases is 1.
  • +
  • Every minute, the energy of the sword increases by the current factor X.
  • +
  • To break the ith lock, the energy of the sword must reach at least strength[i].
  • +
  • After breaking a lock, the energy of the sword resets to 0, and the factor X increases by 1.
  • +
+ +

Your task is to determine the minimum time in minutes required for Bob to break all n locks and escape the dungeon.

+ +

Return the minimum time required for Bob to break all n locks.

+ +

 

+

Example 1:

+ +
+

Input: strength = [3,4,1]

+ +

Output: 4

+ +

Explanation:

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
TimeEnergyXActionUpdated X
001Nothing1
111Break 3rd Lock2
222Nothing2
342Break 2nd Lock3
433Break 1st Lock3
+ +

The locks cannot be broken in less than 4 minutes; thus, the answer is 4.

+
+ +

Example 2:

+ +
+

Input: strength = [2,5,4]

+ +

Output: 6

+ +

Explanation:

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
TimeEnergyXActionUpdated X
001Nothing1
111Nothing1
221Break 1st Lock2
322Nothing2
442Break 3rd Lock3
533Nothing3
663Break 2nd Lock4
+ +

The locks cannot be broken in less than 6 minutes; thus, the answer is 6.

+
+ +

 

+

Constraints:

+ +
    +
  • n == strength.length
  • +
  • 1 <= n <= 80
  • +
  • 1 <= strength[i] <= 106
  • +
  • n == strength.length
  • +
+ + + +## Solutions + + + +### Solution 1 + + + +#### Python3 + +```python + +``` + +#### Java + +```java + +``` + +#### C++ + +```cpp + +``` + +#### Go + +```go + +``` + + + + + + diff --git a/solution/README.md b/solution/README.md index 226ea5aea0194..dc063d1fd8561 100644 --- a/solution/README.md +++ b/solution/README.md @@ -3395,6 +3395,7 @@ | 3382 | [用点构造面积最大的矩形 II](/solution/3300-3399/3382.Maximum%20Area%20Rectangle%20With%20Point%20Constraints%20II/README.md) | `树状数组`,`线段树`,`几何`,`数组`,`数学`,`排序` | 困难 | 第 427 场周赛 | | 3383 | [施法所需最低符文数量](/solution/3300-3399/3383.Minimum%20Runes%20to%20Add%20to%20Cast%20Spell/README.md) | `深度优先搜索`,`广度优先搜索`,`图`,`拓扑排序`,`数组` | 困难 | 🔒 | | 3384 | [球队传球成功的优势得分](/solution/3300-3399/3384.Team%20Dominance%20by%20Pass%20Success/README.md) | `数据库` | 困难 | 🔒 | +| 3385 | [Minimum Time to Break Locks II](/solution/3300-3399/3385.Minimum%20Time%20to%20Break%20Locks%20II/README.md) | | 困难 | 🔒 | ## 版权 diff --git a/solution/README_EN.md b/solution/README_EN.md index 7a0366117385a..ab020557183f2 100644 --- a/solution/README_EN.md +++ b/solution/README_EN.md @@ -3393,6 +3393,7 @@ Press Control + F(or Command + F on | 3382 | [Maximum Area Rectangle With Point Constraints II](/solution/3300-3399/3382.Maximum%20Area%20Rectangle%20With%20Point%20Constraints%20II/README_EN.md) | `Binary Indexed Tree`,`Segment Tree`,`Geometry`,`Array`,`Math`,`Sorting` | Hard | Weekly Contest 427 | | 3383 | [Minimum Runes to Add to Cast Spell](/solution/3300-3399/3383.Minimum%20Runes%20to%20Add%20to%20Cast%20Spell/README_EN.md) | `Depth-First Search`,`Breadth-First Search`,`Graph`,`Topological Sort`,`Array` | Hard | 🔒 | | 3384 | [Team Dominance by Pass Success](/solution/3300-3399/3384.Team%20Dominance%20by%20Pass%20Success/README_EN.md) | `Database` | Hard | 🔒 | +| 3385 | [Minimum Time to Break Locks II](/solution/3300-3399/3385.Minimum%20Time%20to%20Break%20Locks%20II/README_EN.md) | | Hard | 🔒 | ## Copyright