There was an error while loading. Please reload this page.
1 parent b78e9ee commit 670a4aaCopy full SHA for 670a4aa
Deque/2969.Minimum-Number-of-Coins-for-Fruits-II/2969.Minimum-Number-of-Coins-for-Fruits-II.cpp
@@ -0,0 +1,29 @@
1
+class Solution {
2
+ int dp[100005][2];
3
+public:
4
+ int minimumCoins(vector<int>& prices)
5
+ {
6
+ int n = prices.size();
7
+ prices.insert(prices.begin(), 0);
8
+ dp[1][0] = INT_MAX/2;
9
+ dp[1][1] = prices[1];
10
+
11
+ deque<int>dq;
12
+ dq.push_back(1);
13
14
+ for (int i=2; i<=n; i++)
15
16
+ dp[i][1] = min(dp[i-1][0], dp[i-1][1]) + prices[i];
17
+ while (!dq.empty() && dq.front()*2<i)
18
+ dq.pop_front();
19
+ dp[i][0] = dp[dq.front()][1];
20
21
+ while (!dq.empty() && dp[dq.back()][1] >= dp[i][1])
22
+ dq.pop_back();
23
+ dq.push_back(i);
24
+ }
25
26
+ return min(dp[n][0], dp[n][1]);
27
28
29
+};
0 commit comments