|
54302 | 54302 | <ul class="md-nav__list"> |
54303 | 54303 |
|
54304 | 54304 | <li class="md-nav__item"> |
54305 | | - <a href="#solution-1" class="md-nav__link"> |
| 54305 | + <a href="#solution-1-enumeration-binary-search" class="md-nav__link"> |
54306 | 54306 | <span class="md-ellipsis"> |
54307 | | - Solution 1 |
| 54307 | + Solution 1: Enumeration + Binary Search |
54308 | 54308 | </span> |
54309 | 54309 | </a> |
54310 | 54310 |
|
@@ -82181,7 +82181,13 @@ <h2 id="description">Description</h2> |
82181 | 82181 | <h2 id="solutions">Solutions</h2> |
82182 | 82182 | <!-- solution:start --> |
82183 | 82183 |
|
82184 | | -<h3 id="solution-1">Solution 1</h3> |
| 82184 | +<h3 id="solution-1-enumeration-binary-search">Solution 1: Enumeration + Binary Search</h3> |
| 82185 | +<p>We note that if the number of flowers in a garden is already greater than or equal to $\textit{target}$, then this garden is already a perfect garden and cannot be changed. For imperfect gardens, we can plant more flowers to make them perfect gardens.</p> |
| 82186 | +<p>Let's enumerate how many gardens will eventually become perfect gardens. Suppose initially there are $x$ perfect gardens, then we can enumerate in the range $[x, n]$. Which imperfect gardens should we choose to become perfect gardens? In fact, we should choose the gardens with more flowers so that the remaining flowers can be used to increase the minimum value of the imperfect gardens. Therefore, we sort the array $\textit{flowers}$.</p> |
| 82187 | +<p>Next, we enumerate the number of perfect gardens $x$. The current garden to become a perfect garden is $\textit{target}[n-x]$, and the number of flowers needed is $\max(0, \textit{target} - \textit{flowers}[n - x])$.</p> |
| 82188 | +<p>We update the remaining flowers $\textit{newFlowers}$. If it is less than $0$, it means we can no longer make more gardens perfect, so we stop the enumeration.</p> |
| 82189 | +<p>Otherwise, we perform a binary search in the range $[0,..n-x-1]$ to find the maximum index of the imperfect gardens that can be turned into perfect gardens. Let the index be $l$, then the number of flowers needed is $\textit{cost} = \textit{flowers}[l] \times (l + 1) - s[l + 1]$, where $s[i]$ is the sum of the first $i$ numbers in the $\textit{flowers}$ array. If we can still increase the minimum value, we calculate the increase $\frac{\textit{newFlowers} - \textit{cost}}{l + 1}$, and ensure that the final minimum value does not exceed $\textit{target}-1$. That is, the minimum value $y = \min(\textit{flowers}[l] + \frac{\textit{newFlowers} - \textit{cost}}{l + 1}, \textit{target} - 1)$. Then the beauty value of the garden is $x \times \textit{full} + y \times \textit{partial}$. The answer is the maximum of all beauty values.</p> |
| 82190 | +<p>The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the $\textit{flowers}$ array.</p> |
82185 | 82191 | <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> |
82186 | 82192 | <div class="tabbed-content"> |
82187 | 82193 | <div class="tabbed-block"> |
|
0 commit comments