forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjump-game-ix.cpp
More file actions
25 lines (24 loc) · 775 Bytes
/
jump-game-ix.cpp
File metadata and controls
25 lines (24 loc) · 775 Bytes
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
// Time: O(n)
// Space: O(1)
// dp, mono stack
class Solution {
public:
long long minCost(vector<int>& nums, vector<int>& costs) {
vector<int> stk1, stk2;
vector<int64_t> dp(size(nums), numeric_limits<int64_t>::max());
dp[0] = 0;
for (int i = 0; i < size(nums); ++i) {
while (!empty(stk1) && nums[stk1.back()] <= nums[i]) {
dp[i] = min(dp[i], dp[stk1.back()] + costs[i]);
stk1.pop_back();
}
stk1.emplace_back(i);
while (!empty(stk2) && nums[stk2.back()] > nums[i]) {
dp[i] = min(dp[i], dp[stk2.back()] + costs[i]);
stk2.pop_back();
}
stk2.emplace_back(i);
}
return dp.back();
}
};