Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 713eed0

Browse files
feat: add solutions to lc problem: No.2234 (doocs#1560)
No.2234.Maximum Total Beauty of the Gardens
1 parent f7e4a87 commit 713eed0

File tree

7 files changed

+599
-4
lines changed

7 files changed

+599
-4
lines changed

‎solution/2200-2299/2234.Maximum Total Beauty of the Gardens/README.md‎

Lines changed: 212 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,28 +69,238 @@
6969

7070
<!-- 这里可写通用的实现逻辑 -->
7171

72+
**方法一:枚举 + 二分查找**
73+
74+
我们注意到,如果一个花园中种的花的数目已经大于等于 $target,ドル那么这个花园就已经是完善的花园,不能再改变。而不完善的花园中,可以通过种更多的花来使得这个花园变成完善的花园。
75+
76+
我们不妨枚举有多少个花园最终成为完善的花园,假设初始时有 $x$ 个完善的花园,那么我们可以在 $[x, n]$ 范围内枚举。我们应该选择哪些不完善花园变成完善花园呢?实际上,我们应该选择那么花的数目较多的花园,这样才能使得最终剩下的可额外种植的花更多,将这些花用于提升不完善花园的最小值。因此,我们对数组 $flowers$ 进行排序。
77+
78+
接下来,我们枚举完善花园的数目 $x,ドル那么当前要变成完善花园的是 $target[n-x],ドル需要种植的花的数量为 $\max(0, target - flowers[n - x])$。
79+
80+
我们更新剩余可种植的花 $newFlowers,ドル如果小于 0ドル,ドル说明已经不能将更多的花园变成完善花园了,直接退出枚举。
81+
82+
否则,我们在 $[0,..n-x-1]$ 范围内,二分查找可以把不完善花园变成完善花园的最大下标。记下标为 $l,ドル那么所需要种植的花的数量为 $cost = flowers[l] * (l + 1) - s[l + 1],ドル其中 $s[i]$ 是 $flowers$ 数组中前 $i$ 个数之和。如果此时还能提升最小值的大小,我们算出能提升的幅度 $\frac{newFlowers - cost}{l + 1},ドル并且保证最终的最小值不超过 $target-1$。即最小值 $y = \min(flowers[l] + \frac{newFlowers - cost}{l + 1}, target - 1)$。那么此时花园的美丽值为 $x \times full + y \times partial$。答案为所有美丽值的最大值。
83+
84+
时间复杂度 $O(n \times \log n),ドル空间复杂度 $O(n)$。其中 $n$ 是数组 $flowers$ 的长度。
85+
7286
<!-- tabs:start -->
7387

7488
### **Python3**
7589

7690
<!-- 这里可写当前语言的特殊实现逻辑 -->
7791

7892
```python
79-
93+
class Solution:
94+
def maximumBeauty(
95+
self, flowers: List[int], newFlowers: int, target: int, full: int, partial: int
96+
) -> int:
97+
flowers.sort()
98+
n = len(flowers)
99+
s = list(accumulate(flowers, initial=0))
100+
ans, i = 0, n - bisect_left(flowers, target)
101+
for x in range(i, n + 1):
102+
newFlowers -= 0 if x == 0 else max(target - flowers[n - x], 0)
103+
if newFlowers < 0:
104+
break
105+
l, r = 0, n - x - 1
106+
while l < r:
107+
mid = (l + r + 1) >> 1
108+
if flowers[mid] * (mid + 1) - s[mid + 1] <= newFlowers:
109+
l = mid
110+
else:
111+
r = mid - 1
112+
y = 0
113+
if r != -1:
114+
cost = flowers[l] * (l + 1) - s[l + 1]
115+
y = min(flowers[l] + (newFlowers - cost) // (l + 1), target - 1)
116+
ans = max(ans, x * full + y * partial)
117+
return ans
80118
```
81119

82120
### **Java**
83121

84122
<!-- 这里可写当前语言的特殊实现逻辑 -->
85123

86124
```java
125+
class Solution {
126+
public long maximumBeauty(int[] flowers, long newFlowers, int target, int full, int partial) {
127+
Arrays.sort(flowers);
128+
int n = flowers.length;
129+
long[] s = new long[n + 1];
130+
for (int i = 0; i < n; ++i) {
131+
s[i + 1] = s[i] + flowers[i];
132+
}
133+
long ans = 0;
134+
int x = 0;
135+
for (int v : flowers) {
136+
if (v >= target) {
137+
++x;
138+
}
139+
}
140+
for (; x <= n; ++x) {
141+
newFlowers -= (x == 0 ? 0 : Math.max(target - flowers[n - x], 0));
142+
if (newFlowers < 0) {
143+
break;
144+
}
145+
int l = 0, r = n - x - 1;
146+
while (l < r) {
147+
int mid = (l + r + 1) >> 1;
148+
if ((long) flowers[mid] * (mid + 1) - s[mid + 1] <= newFlowers) {
149+
l = mid;
150+
} else {
151+
r = mid - 1;
152+
}
153+
}
154+
long y = 0;
155+
if (r != -1) {
156+
long cost = (long) flowers[l] * (l + 1) - s[l + 1];
157+
y = Math.min(flowers[l] + (newFlowers - cost) / (l + 1), target - 1);
158+
}
159+
ans = Math.max(ans, (long) x * full + y * partial);
160+
}
161+
return ans;
162+
}
163+
}
164+
```
87165

166+
### **C++**
167+
168+
```cpp
169+
class Solution {
170+
public:
171+
long long maximumBeauty(vector<int>& flowers, long long newFlowers, int target, int full, int partial) {
172+
sort(flowers.begin(), flowers.end());
173+
int n = flowers.size();
174+
long long s[n + 1];
175+
s[0] = 0;
176+
for (int i = 1; i <= n; ++i) {
177+
s[i] = s[i - 1] + flowers[i - 1];
178+
}
179+
long long ans = 0;
180+
int i = flowers.end() - lower_bound(flowers.begin(), flowers.end(), target);
181+
for (int x = i; x <= n; ++x) {
182+
newFlowers -= (x == 0 ? 0 : max(target - flowers[n - x], 0));
183+
if (newFlowers < 0) {
184+
break;
185+
}
186+
int l = 0, r = n - x - 1;
187+
while (l < r) {
188+
int mid = (l + r + 1) >> 1;
189+
if (1LL * flowers[mid] * (mid + 1) - s[mid + 1] <= newFlowers) {
190+
l = mid;
191+
} else {
192+
r = mid - 1;
193+
}
194+
}
195+
int y = 0;
196+
if (r != -1) {
197+
long long cost = 1LL * flowers[l] * (l + 1) - s[l + 1];
198+
long long mx = flowers[l] + (newFlowers - cost) / (l + 1);
199+
long long threshold = target - 1;
200+
y = min(mx, threshold);
201+
}
202+
ans = max(ans, 1LL * x * full + 1LL * y * partial);
203+
}
204+
return ans;
205+
}
206+
};
207+
```
208+
209+
### **Go**
210+
211+
```go
212+
func maximumBeauty(flowers []int, newFlowers int64, target int, full int, partial int) int64 {
213+
sort.Ints(flowers)
214+
n := len(flowers)
215+
s := make([]int, n+1)
216+
for i, x := range flowers {
217+
s[i+1] = s[i] + x
218+
}
219+
ans := 0
220+
i := n - sort.SearchInts(flowers, target)
221+
for x := i; x <= n; x++ {
222+
if x > 0 {
223+
newFlowers -= int64(max(target-flowers[n-x], 0))
224+
}
225+
if newFlowers < 0 {
226+
break
227+
}
228+
l, r := 0, n-x-1
229+
for l < r {
230+
mid := (l + r + 1) >> 1
231+
if int64(flowers[mid]*(mid+1)-s[mid+1]) <= newFlowers {
232+
l = mid
233+
} else {
234+
r = mid - 1
235+
}
236+
}
237+
y := 0
238+
if r != -1 {
239+
cost := flowers[l]*(l+1) - s[l+1]
240+
y = min(flowers[l]+int((newFlowers-int64(cost))/int64(l+1)), target-1)
241+
}
242+
ans = max(ans, x*full+y*partial)
243+
}
244+
return int64(ans)
245+
}
246+
247+
func max(a, b int) int {
248+
if a > b {
249+
return a
250+
}
251+
return b
252+
}
253+
254+
func min(a, b int) int {
255+
if a < b {
256+
return a
257+
}
258+
return b
259+
}
88260
```
89261

90262
### **TypeScript**
91263

92264
```ts
93-
265+
function maximumBeauty(
266+
flowers: number[],
267+
newFlowers: number,
268+
target: number,
269+
full: number,
270+
partial: number,
271+
): number {
272+
flowers.sort((a, b) => a - b);
273+
const n = flowers.length;
274+
const s: number[] = Array(n + 1).fill(0);
275+
for (let i = 1; i <= n; i++) {
276+
s[i] = s[i - 1] + flowers[i - 1];
277+
}
278+
let x = flowers.filter(f => f >= target).length;
279+
let ans = 0;
280+
for (; x <= n; ++x) {
281+
newFlowers -= x === 0 ? 0 : Math.max(target - flowers[n - x], 0);
282+
if (newFlowers < 0) {
283+
break;
284+
}
285+
let l = 0;
286+
let r = n - x - 1;
287+
while (l < r) {
288+
const mid = (l + r + 1) >> 1;
289+
if (flowers[mid] * (mid + 1) - s[mid + 1] <= newFlowers) {
290+
l = mid;
291+
} else {
292+
r = mid - 1;
293+
}
294+
}
295+
let y = 0;
296+
if (r !== -1) {
297+
const cost = flowers[l] * (l + 1) - s[l + 1];
298+
y = Math.min(flowers[l] + Math.floor((newFlowers - cost) / (l + 1)), target - 1);
299+
}
300+
ans = Math.max(ans, x * full + y * partial);
301+
}
302+
return ans;
303+
}
94304
```
95305

96306
### **...**

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /