Advertisement
pb_jiang

LC2919

Nov 3rd, 2023
567
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.56 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     long long minIncrementOperations(vector<int>& ns, int k) {
  4.         using ll = long long;
  5.         int n = ns.size();
  6.         vector<ll> dp(n, LLONG_MAX / 2);
  7.         for (int i = 0; i < 3; ++i) dp[i] = (ns[i] >= k ? 0 : k - ns[i]);
  8.         for (int i = 0; i < n; ++i) {
  9.             for (int j = 1; j <= 3 && i + j < n; ++j) {
  10.                 ll costj = (ns[i + j] >= k ? 0 : k - ns[i + j]);
  11.                 dp[i + j] = min(dp[i + j], dp[i] + costj);
  12.             }
  13.         }
  14.         return min({dp[n-1], dp[n-2], dp[n-3]});
  15.     }
  16. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement