You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/2900-2999/2919.Minimum Increment Operations to Make Array Beautiful/README_EN.md
+67-3
Original file line number
Diff line number
Diff line change
@@ -69,30 +69,94 @@ Hence, the answer is 0.
69
69
70
70
## Solutions
71
71
72
+
**Solution 1: Dynamic Programming**
73
+
74
+
We define $f$, $g$, and $h$ as the minimum number of increment operations needed to get the maximum value from the last three items in the first $i$ items, initially $f = 0$, $g = 0$, $h = 0$.
75
+
76
+
Next, we traverse the array $nums$. For each $x$, we need to update the values of $f$, $g$, and $h$ to meet the requirements of the problem, that is:
77
+
78
+
$$
79
+
\begin{aligned}
80
+
f' &= g \\
81
+
g' &= h \\
82
+
h' &= \min(f, g, h) + \max(k - x, 0)
83
+
\end{aligned}
84
+
$$
85
+
86
+
Finally, we only need to return the minimum value among $f$, $g$, and $h$.
87
+
88
+
The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.
0 commit comments