|
39034 | 39034 | <ul class="md-nav__list">
|
39035 | 39035 |
|
39036 | 39036 | <li class="md-nav__item">
|
39037 |
| - <a href="#solution-1" class="md-nav__link"> |
| 39037 | + <a href="#solution-1-dynamic-programming" class="md-nav__link"> |
39038 | 39038 | <span class="md-ellipsis">
|
39039 |
| - Solution 1 |
| 39039 | + Solution 1: Dynamic Programming |
39040 | 39040 | </span>
|
39041 | 39041 | </a>
|
39042 | 39042 |
|
@@ -77567,7 +77567,16 @@ <h2 id="description">Description</h2>
|
77567 | 77567 | <h2 id="solutions">Solutions</h2>
|
77568 | 77568 | <!-- solution:start -->
|
77569 | 77569 |
|
77570 |
| -<h3 id="solution-1">Solution 1</h3> |
| 77570 | +<h3 id="solution-1-dynamic-programming">Solution 1: Dynamic Programming</h3> |
| 77571 | +<p>We define $f[i]$ as the minimum number of operations required to obtain $target[0,..i]$, initially setting $f[0] = target[0]$.</p> |
| 77572 | +<p>For $target[i]$, if $target[i] \leq target[i-1]$, then $f[i] = f[i-1]$; otherwise, $f[i] = f[i-1] + target[i] - target[i-1]$.</p> |
| 77573 | +<p>The final answer is $f[n-1]$.</p> |
| 77574 | +<p>We notice that $f[i]$ only depends on $f[i-1]$, so we can maintain the operation count using just one variable.</p> |
| 77575 | +<p>The time complexity is $O(n)$, where $n$ is the length of the array $target$. The space complexity is $O(1)$.</p> |
| 77576 | +<p>Similar problems:</p> |
| 77577 | +<ul> |
| 77578 | +<li><a href="https://github.com/doocs/leetcode/blob/main/solution/3200-3299/3229.Minimum%20Operations%20to%20Make%20Array%20Equal%20to%20Target/README_EN.md">3229. Minimum Operations to Make Array Equal to Target</a></li> |
| 77579 | +</ul> |
77571 | 77580 | <div class="tabbed-set tabbed-alternate" data-tabs="1:5"><input checked="checked" id="__tabbed_1_1" name="__tabbed_1" type="radio" /><input id="__tabbed_1_2" name="__tabbed_1" type="radio" /><input id="__tabbed_1_3" name="__tabbed_1" type="radio" /><input id="__tabbed_1_4" name="__tabbed_1" type="radio" /><input id="__tabbed_1_5" name="__tabbed_1" type="radio" /><div class="tabbed-labels"><label for="__tabbed_1_1">Python3</label><label for="__tabbed_1_2">Java</label><label for="__tabbed_1_3">C++</label><label for="__tabbed_1_4">Go</label><label for="__tabbed_1_5">TypeScript</label></div>
|
77572 | 77581 | <div class="tabbed-content">
|
77573 | 77582 | <div class="tabbed-block">
|
|
0 commit comments