-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
100 lines (91 loc) · 3.3 KB
/
main.cpp
File metadata and controls
100 lines (91 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Source: https://leetcode.com/problems/find-the-minimum-amount-of-time-to-brew-potions
// Title: Find the Minimum Amount of Time to Brew Potions
// Difficulty: Medium
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// You are given two integer arrays, `skill` and `mana`, of length `n` and `m`, respectively.
//
// 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 `i^th` wizard on the `j^th` potion is `time_ij = skill[i] * mana[j]`.
//
// 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.
//
// Return the **minimum** amount of time required for the potions to be brewed properly.
//
// **Example 1:**
//
// ```
// Input: skill = [1,5,2,4], mana = [5,1,4,2]
// Output: 110
// Explanation:
// - Potion Number: 0 1 2 3
// - Start time: 0 52 53 86
// - Wizard 0 done by: 5 53 58 88
// - Wizard 1 done by: 30 58 78 98
// - Wizard 2 done by: 40 60 86 102
// - Wizard 3 done by: 60 64 102 110
// As an example for why wizard 0 cannot start working on the 1^st potion before time `t = 52`, consider the case where the wizards started preparing the 1^st potion at time `t = 50`. At time `t = 58`, wizard 2 is done with the 1^st potion, but wizard 3 will still be working on the 0^th potion till time `t = 60`.
// ```
//
// **Example 2:**
//
// ```
// Input: skill = [1,1,1], mana = [1,1,1]
// Output: 5
// Explanation:
// - Preparation of the 0^th potion begins at time `t = 0`, and is completed by time `t = 3`.
// - Preparation of the 1^st potion begins at time `t = 1`, and is completed by time `t = 4`.
// - Preparation of the 2^nd potion begins at time `t = 2`, and is completed by time `t = 5`.
// ```
//
// **Example 3:**
//
// ```
// Input: skill = [1,2,3,4], mana = [1,2]
// Output: 21
// ```
//
// **Constraints:**
//
// - `n == skill.length`
// - `m == mana.length`
// - `1 <= n, m <= 5000`
// - `1 <= mana[i], skill[i] <= 5000`
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <cstdint>
#include <vector>
using namespace std;
// Brute-Force
class Solution {
public:
int64_t minTime(vector<int>& skill, vector<int>& mana) {
int n = skill.size(), m = mana.size();
// Prepare
auto wizards = vector<int64_t>(n);
auto cost = vector<int64_t>(n);
// Brew potions
for (auto j = 0; j < m; ++j) {
// Compute cost
for (auto i = 0; i < n; ++i) {
cost[i] = skill[i] * mana[j];
}
// Pre-brew
int64_t now = wizards[0];
int64_t start = now;
for (auto i = 0; i < n; ++i) {
if (now < wizards[i]) {
start += wizards[i] - now;
now = wizards[i];
}
now += cost[i];
}
// Brew
now = start;
for (auto i = 0; i < n; ++i) {
now += cost[i];
wizards[i] = now;
}
}
return wizards.back();
}
};