Skip to content

Commit c2d1cd9

Browse files
authored
feat: add solutions to lc problem: No.3494 (#4773)
1 parent bbbf305 commit c2d1cd9

File tree

7 files changed

+268
-8
lines changed

7 files changed

+268
-8
lines changed

solution/3400-3499/3494.Find the Minimum Amount of Time to Brew Potions/README.md

Lines changed: 95 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,32 +129,123 @@ tags:
129129

130130
<!-- solution:start -->
131131

132-
### 方法一
132+
### 方法一:递推
133+
134+
我们定义 $f[i]$ 表示巫师 $i$ 完成上一瓶药水的时间。
135+
136+
对于当前的药水 $x$,我们需要计算每个巫师完成该药水的时间。定义 $\textit{tot}$ 表示当前药水完成的时间,初始时 $\textit{tot} = 0$。
137+
138+
对于每个巫师 $i$,他开始处理当前药水的时间为 $\max(\textit{tot}, f[i])$,处理该药水需要的时间为 $skill[i] \times mana[x]$,因此他完成该药水的时间为 $\max(\textit{tot}, f[i]) + skill[i] \times mana[x]$。我们更新 $\textit{tot}$ 为该值。
139+
140+
由于酿造过程要求药水在当前巫师完成工作后必须立即传递给下一个巫师并开始处理,因此我们需要更新每个巫师完成上一瓶药水的时间 $f[i]$。对于最后一个巫师 $n-1$,我们直接将 $f[n-1]$ 更新为 $\textit{tot}$。对于其他巫师 $i$,我们可以通过倒序遍历来更新 $f[i]$,具体地,$f[i] = f[i+1] - skill[i+1] \times mana[x]$。
141+
142+
最终 $f[n-1]$ 即为所有药水酿造完成的最短总时间。
143+
144+
时间复杂度 $O(n \times m)$,空间复杂度 $O(n)$。其中 $n$ 和 $m$ 分别为巫师和药水的数量。
133145

134146
<!-- tabs:start -->
135147

136148
#### Python3
137149

138150
```python
139-
151+
class Solution:
152+
def minTime(self, skill: List[int], mana: List[int]) -> int:
153+
max = lambda a, b: a if a > b else b
154+
n = len(skill)
155+
f = [0] * n
156+
for x in mana:
157+
tot = 0
158+
for i in range(n):
159+
tot = max(tot, f[i]) + skill[i] * x
160+
f[-1] = tot
161+
for i in range(n - 2, -1, -1):
162+
f[i] = f[i + 1] - skill[i + 1] * x
163+
return f[-1]
140164
```
141165

142166
#### Java
143167

144168
```java
145-
169+
class Solution {
170+
public long minTime(int[] skill, int[] mana) {
171+
int n = skill.length;
172+
long[] f = new long[n];
173+
for (int x : mana) {
174+
long tot = 0;
175+
for (int i = 0; i < n; ++i) {
176+
tot = Math.max(tot, f[i]) + skill[i] * x;
177+
}
178+
f[n - 1] = tot;
179+
for (int i = n - 2; i >= 0; --i) {
180+
f[i] = f[i + 1] - skill[i + 1] * x;
181+
}
182+
}
183+
return f[n - 1];
184+
}
185+
}
146186
```
147187

148188
#### C++
149189

150190
```cpp
151-
191+
class Solution {
192+
public:
193+
long long minTime(vector<int>& skill, vector<int>& mana) {
194+
int n = skill.size();
195+
vector<long long> f(n);
196+
for (int x : mana) {
197+
long long tot = 0;
198+
for (int i = 0; i < n; ++i) {
199+
tot = max(tot, f[i]) + 1LL * skill[i] * x;
200+
}
201+
f[n - 1] = tot;
202+
for (int i = n - 2; i >= 0; --i) {
203+
f[i] = f[i + 1] - 1LL * skill[i + 1] * x;
204+
}
205+
}
206+
return f[n - 1];
207+
}
208+
};
152209
```
153210
154211
#### Go
155212
156213
```go
214+
func minTime(skill []int, mana []int) int64 {
215+
n := len(skill)
216+
f := make([]int64, n)
217+
for _, x := range mana {
218+
var tot int64
219+
for i := 0; i < n; i++ {
220+
tot = max(tot, f[i]) + int64(skill[i])*int64(x)
221+
}
222+
f[n-1] = tot
223+
for i := n - 2; i >= 0; i-- {
224+
f[i] = f[i+1] - int64(skill[i+1])*int64(x)
225+
}
226+
}
227+
return f[n-1]
228+
}
229+
```
157230

231+
#### TypeScript
232+
233+
```ts
234+
function minTime(skill: number[], mana: number[]): number {
235+
const n = skill.length;
236+
const f: number[] = Array(n).fill(0);
237+
for (const x of mana) {
238+
let tot = 0;
239+
for (let i = 0; i < n; ++i) {
240+
tot = Math.max(tot, f[i]) + skill[i] * x;
241+
}
242+
f[n - 1] = tot;
243+
for (let i = n - 2; i >= 0; --i) {
244+
f[i] = f[i + 1] - skill[i + 1] * x;
245+
}
246+
}
247+
return f[n - 1];
248+
}
158249
```
159250

160251
<!-- tabs:end -->

solution/3400-3499/3494.Find the Minimum Amount of Time to Brew Potions/README_EN.md

Lines changed: 95 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,32 +126,123 @@ tags:
126126

127127
<!-- solution:start -->
128128

129-
### Solution 1
129+
### Solution 1: Dynamic Programming
130+
131+
We define $f[i]$ as the time when wizard $i$ completes the previous potion.
132+
133+
For the current potion $x$, we need to calculate the completion time for each wizard. Let $\textit{tot}$ represent the completion time of the current potion, initially $\textit{tot} = 0$.
134+
135+
For each wizard $i$, the time he starts processing the current potion is $\max(\textit{tot}, f[i])$, and the time required to process this potion is $skill[i] \times mana[x]$. Therefore, the time he completes this potion is $\max(\textit{tot}, f[i]) + skill[i] \times mana[x]$. We update $\textit{tot}$ to this value.
136+
137+
Since the brewing process requires that the potion must be immediately passed to the next wizard and processing must start immediately after the current wizard completes their work, we need to update the completion time $f[i]$ for each wizard's previous potion. For the last wizard $n-1$, we directly update $f[n-1]$ to $\textit{tot}$. For other wizards $i$, we can update $f[i]$ by traversing in reverse order, specifically, $f[i] = f[i+1] - skill[i+1] \times mana[x]$.
138+
139+
Finally, $f[n-1]$ is the minimum total time required to complete brewing all potions.
140+
141+
The time complexity is $O(n \times m)$ and the space complexity is $O(n)$, where $n$ and $m$ are the number of wizards and potions respectively.
130142

131143
<!-- tabs:start -->
132144

133145
#### Python3
134146

135147
```python
136-
148+
class Solution:
149+
def minTime(self, skill: List[int], mana: List[int]) -> int:
150+
max = lambda a, b: a if a > b else b
151+
n = len(skill)
152+
f = [0] * n
153+
for x in mana:
154+
tot = 0
155+
for i in range(n):
156+
tot = max(tot, f[i]) + skill[i] * x
157+
f[-1] = tot
158+
for i in range(n - 2, -1, -1):
159+
f[i] = f[i + 1] - skill[i + 1] * x
160+
return f[-1]
137161
```
138162

139163
#### Java
140164

141165
```java
142-
166+
class Solution {
167+
public long minTime(int[] skill, int[] mana) {
168+
int n = skill.length;
169+
long[] f = new long[n];
170+
for (int x : mana) {
171+
long tot = 0;
172+
for (int i = 0; i < n; ++i) {
173+
tot = Math.max(tot, f[i]) + skill[i] * x;
174+
}
175+
f[n - 1] = tot;
176+
for (int i = n - 2; i >= 0; --i) {
177+
f[i] = f[i + 1] - skill[i + 1] * x;
178+
}
179+
}
180+
return f[n - 1];
181+
}
182+
}
143183
```
144184

145185
#### C++
146186

147187
```cpp
148-
188+
class Solution {
189+
public:
190+
long long minTime(vector<int>& skill, vector<int>& mana) {
191+
int n = skill.size();
192+
vector<long long> f(n);
193+
for (int x : mana) {
194+
long long tot = 0;
195+
for (int i = 0; i < n; ++i) {
196+
tot = max(tot, f[i]) + 1LL * skill[i] * x;
197+
}
198+
f[n - 1] = tot;
199+
for (int i = n - 2; i >= 0; --i) {
200+
f[i] = f[i + 1] - 1LL * skill[i + 1] * x;
201+
}
202+
}
203+
return f[n - 1];
204+
}
205+
};
149206
```
150207
151208
#### Go
152209
153210
```go
211+
func minTime(skill []int, mana []int) int64 {
212+
n := len(skill)
213+
f := make([]int64, n)
214+
for _, x := range mana {
215+
var tot int64
216+
for i := 0; i < n; i++ {
217+
tot = max(tot, f[i]) + int64(skill[i])*int64(x)
218+
}
219+
f[n-1] = tot
220+
for i := n - 2; i >= 0; i-- {
221+
f[i] = f[i+1] - int64(skill[i+1])*int64(x)
222+
}
223+
}
224+
return f[n-1]
225+
}
226+
```
154227

228+
#### TypeScript
229+
230+
```ts
231+
function minTime(skill: number[], mana: number[]): number {
232+
const n = skill.length;
233+
const f: number[] = Array(n).fill(0);
234+
for (const x of mana) {
235+
let tot = 0;
236+
for (let i = 0; i < n; ++i) {
237+
tot = Math.max(tot, f[i]) + skill[i] * x;
238+
}
239+
f[n - 1] = tot;
240+
for (let i = n - 2; i >= 0; --i) {
241+
f[i] = f[i + 1] - skill[i + 1] * x;
242+
}
243+
}
244+
return f[n - 1];
245+
}
155246
```
156247

157248
<!-- tabs:end -->
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
long long minTime(vector<int>& skill, vector<int>& mana) {
4+
int n = skill.size();
5+
vector<long long> f(n);
6+
for (int x : mana) {
7+
long long tot = 0;
8+
for (int i = 0; i < n; ++i) {
9+
tot = max(tot, f[i]) + 1LL * skill[i] * x;
10+
}
11+
f[n - 1] = tot;
12+
for (int i = n - 2; i >= 0; --i) {
13+
f[i] = f[i + 1] - 1LL * skill[i + 1] * x;
14+
}
15+
}
16+
return f[n - 1];
17+
}
18+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
func minTime(skill []int, mana []int) int64 {
2+
n := len(skill)
3+
f := make([]int64, n)
4+
for _, x := range mana {
5+
var tot int64
6+
for i := 0; i < n; i++ {
7+
tot = max(tot, f[i]) + int64(skill[i])*int64(x)
8+
}
9+
f[n-1] = tot
10+
for i := n - 2; i >= 0; i-- {
11+
f[i] = f[i+1] - int64(skill[i+1])*int64(x)
12+
}
13+
}
14+
return f[n-1]
15+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public long minTime(int[] skill, int[] mana) {
3+
int n = skill.length;
4+
long[] f = new long[n];
5+
for (int x : mana) {
6+
long tot = 0;
7+
for (int i = 0; i < n; ++i) {
8+
tot = Math.max(tot, f[i]) + skill[i] * x;
9+
}
10+
f[n - 1] = tot;
11+
for (int i = n - 2; i >= 0; --i) {
12+
f[i] = f[i + 1] - skill[i + 1] * x;
13+
}
14+
}
15+
return f[n - 1];
16+
}
17+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def minTime(self, skill: List[int], mana: List[int]) -> int:
3+
max = lambda a, b: a if a > b else b
4+
n = len(skill)
5+
f = [0] * n
6+
for x in mana:
7+
tot = 0
8+
for i in range(n):
9+
tot = max(tot, f[i]) + skill[i] * x
10+
f[-1] = tot
11+
for i in range(n - 2, -1, -1):
12+
f[i] = f[i + 1] - skill[i + 1] * x
13+
return f[-1]
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function minTime(skill: number[], mana: number[]): number {
2+
const n = skill.length;
3+
const f: number[] = Array(n).fill(0);
4+
for (const x of mana) {
5+
let tot = 0;
6+
for (let i = 0; i < n; ++i) {
7+
tot = Math.max(tot, f[i]) + skill[i] * x;
8+
}
9+
f[n - 1] = tot;
10+
for (let i = n - 2; i >= 0; --i) {
11+
f[i] = f[i + 1] - skill[i + 1] * x;
12+
}
13+
}
14+
return f[n - 1];
15+
}

0 commit comments

Comments
 (0)