Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- long long minIncrementOperations(vector<int>& ns, int k) {
- using ll = long long;
- int n = ns.size();
- vector<ll> dp(n, LLONG_MAX / 2);
- for (int i = 0; i < 3; ++i) dp[i] = (ns[i] >= k ? 0 : k - ns[i]);
- for (int i = 0; i < n; ++i) {
- for (int j = 1; j <= 3 && i + j < n; ++j) {
- ll costj = (ns[i + j] >= k ? 0 : k - ns[i + j]);
- dp[i + j] = min(dp[i + j], dp[i] + costj);
- }
- }
- return min({dp[n-1], dp[n-2], dp[n-3]});
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement