Skip to content

Commit dac0753

Browse files
authored
Merge pull request #1337 from 0xff-dev/3494
Add solution and test-cases for problem 3494
2 parents 1202d71 + 3e071b6 commit dac0753

File tree

4 files changed

+84
-26
lines changed

4 files changed

+84
-26
lines changed
56.7 KB
Loading

leetcode/3401-3500/3494.Find-the-Minimum-Amount-of-Time-to-Brew-Potions/README.md

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,49 @@
11
# [3494.Find the Minimum Amount of Time to Brew Potions][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given two integer arrays, `skill` and `mana`, of length `n` and `m`, respectively.
5+
6+
In a laboratory, `n` wizards must brew `m` potions in order. Each potion has a mana capacity `mana[j]` and **must** pass through **all** the wizards sequentially to be brewed properly. The time taken by the `ith` wizard on the `jth` potion is `timeij = skill[i] * mana[j]`.
7+
8+
Since the brewing process is delicate, a potion **must** be passed to the next wizard immediately after the current wizard completes their work. This means the timing must be synchronized so that each wizard begins working on a potion **exactly** when it arrives.
9+
10+
Return the **minimum** amount of time required for the potions to be brewed properly.
711

8-
**Example 1:**
12+
**Example 1:**
13+
14+
![1](./1.png)
915

1016
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
17+
Input: skill = [1,5,2,4], mana = [5,1,4,2]
18+
19+
Output: 110
20+
21+
Explanation:
22+
23+
As an example for why wizard 0 cannot start working on the 1st potion before time t = 52, consider the case where the wizards started preparing the 1st potion at time t = 50. At time t = 58, wizard 2 is done with the 1st potion, but wizard 3 will still be working on the 0th potion till time t = 60.
1324
```
1425

15-
## 题意
16-
> ...
26+
**Exmaple 2:**
1727

18-
## 题解
28+
```
29+
Input: skill = [1,1,1], mana = [1,1,1]
30+
31+
Output: 5
1932
20-
### 思路1
21-
> ...
22-
Find the Minimum Amount of Time to Brew Potions
23-
```go
33+
Explanation:
34+
35+
Preparation of the 0th potion begins at time t = 0, and is completed by time t = 3.
36+
Preparation of the 1st potion begins at time t = 1, and is completed by time t = 4.
37+
Preparation of the 2nd potion begins at time t = 2, and is completed by time t = 5.
2438
```
2539

40+
**Example 3:**
41+
42+
```
43+
Input: skill = [1,2,3,4], mana = [1,2]
44+
45+
Output: 21
46+
```
2647

2748
## 结语
2849

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,42 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(skill []int, mana []int) int64 {
4+
N, M := len(skill), len(mana)
5+
prevPotion := make([]int64, N)
6+
for potion := 0; potion < M; potion++ {
7+
var startTime int64
8+
if potion > 0 {
9+
// Need to find the min time in between these two to start
10+
start, end := prevPotion[0], prevPotion[N-1]
11+
for start <= end {
12+
mid := start + (end-start)/2
13+
if canCompletePotion(mid, potion, prevPotion, skill, mana) {
14+
startTime = mid
15+
end = mid - 1
16+
} else {
17+
start = mid + 1
18+
}
19+
}
20+
}
21+
for wiz := 0; wiz < N; wiz++ {
22+
prevPotion[wiz] = startTime + int64(skill[wiz])*int64(mana[potion])
23+
startTime = prevPotion[wiz]
24+
}
25+
}
26+
return prevPotion[N-1]
27+
}
28+
29+
func canCompletePotion(startTime int64, potionCount int, prevPotion []int64, skill, mana []int) bool {
30+
for wizard := 0; wizard < len(skill); wizard++ {
31+
doneTime := startTime + int64(skill[wizard])*int64(mana[potionCount])
32+
// If the done time for the previous potion is greater than this one
33+
// it means we cannot hand off this potion to the next wizard in a sequential
34+
// manner.
35+
if prevPotion[wizard] > startTime {
36+
return false
37+
}
38+
startTime = doneTime
39+
}
40+
// As long as all potions are done after the previous, we are ok.
41+
return true
542
}

leetcode/3401-3500/3494.Find-the-Minimum-Amount-of-Time-to-Brew-Potions/Solution_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,31 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
skill, mana []int
14+
expect int64
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{1, 5, 2, 4}, []int{5, 1, 4, 2}, 110},
17+
{"TestCase2", []int{1, 1, 1}, []int{1, 1, 1}, 5},
18+
{"TestCase3", []int{1, 2, 3, 4}, []int{1, 2}, 21},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.skill, c.mana)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
27+
c.expect, got, c.skill, c.mana)
2828
}
2929
})
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)