Skip to content

Commit 31ed143

Browse files
authored
Create find-the-minimum-amount-of-time-to-brew-potions.cpp
1 parent b8b65e0 commit 31ed143

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Time: O(n * m)
2+
// Space: O(1)
3+
4+
// prefix sum, greedy
5+
class Solution {
6+
public:
7+
long long minTime(vector<int>& skill, vector<int>& mana) {
8+
int64_t result = 0;
9+
for (int i = 1; i < size(mana); ++i) {
10+
int64_t prefix = 0, mx = 0;
11+
for (const auto& x : skill) {
12+
prefix += x;
13+
mx = max(mx, mana[i - 1] * prefix - mana[i] * (prefix - x));
14+
}
15+
result += mx;
16+
}
17+
result += mana.back() * accumulate(cbegin(skill), cend(skill), 0ll);
18+
return result;
19+
}
20+
};

0 commit comments

Comments
 (0)