Advertisement
pb_jiang

dp iteration

Oct 4th, 2022
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.37 KB | None | 0 0
  1. for (int i = 1; i < n; ++i) {
  2.     if (a[i] == a[i - 1]) dp[i][0] = dp[i-1][1] + 1, dp[i][1] = dp[i-1][0] + 1;
  3.     else if (a[i] == a[i - 1] + 1) dp[i][0] = dp[i-1][0], dp[i][1] = min(dp[i-1][0], dp[i-1][1]) + 1;
  4.     else if (a[i] + 1 == a[i - 1]) dp[i][0] = min(dp[i-1][0], dp[i-1][1]), dp[i][1] = dp[i-1][1] + 1;
  5.     else dp[i][0] = min(dp[i-1][0], dp[i-1][1]), dp[i][1] = dp[i][0] + 1;
  6. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement