LeetCode每日一题(2234. Maximum Total Beauty of the Gardens)

Alice管理n个花园,通过种植花朵最大化花园总美度。题目涉及动态规划、二分查找,目标是用newFlowers数量在保持已有花朵的基础上,使得花园尽可能完整,达到target数量视为完整,满额奖励full,不足部分按partial计算。通过倒序处理flowers数组,优化决策过程,求解最大美度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Alice is a caretaker of n gardens and she wants to plant flowers to maximize the total beauty of all her gardens.

You are given a 0-indexed integer array flowers of size n, where flowers[i] is the number of flowers already planted in the ith garden. Flowers that are already planted cannot be removed. You are then given another integer newFlowers, which is the maximum number of flowers that Alice can additionally plant. You are also given the integers target, full, and partial.

A garden is considered complete if it has at least target flowers. The total beauty of the gardens is then determined as the sum of the following:

The number of complete gardens multiplied by full.
The minimum number of flowers in any of the incomplete gardens multiplied by partial. If there are no incomplete gardens, then this value will be 0.
Return the maximum total beauty that Alice can obtain after planting at most newFlowers flowers.

Example 1:

Input: flowers = [1,3,1,1], newFlowers = 7, target = 6, full = 12, partial = 1
Output: 14

Explanation: Alice can plant

  • 2 flowers in the 0th garden
  • 3 flowers in the 1st garden
  • 1 flower in the 2nd garden
  • 1 flower in the 3rd garden

The gardens will then be [3,6,2,2]. She planted a total of 2 + 3 + 1 + 1 = 7 flowers.
There is 1 garden that is complete.
The minimum number of flowers in the incomplete gardens is 2.
Thus, the total beauty is 1 _ 12 + 2 _ 1 = 12 + 2 = 14.
No other way of planting flowers can obtain a total beauty higher than 14.

Example 2:

Input: flowers = [2,4,5,3], newFlowers = 10, target = 5, full = 2, partial = 6
Output: 30

Explanation: Alice can plant

  • 3 flowers in the 0th garden
  • 0 flowers in the 1st garden
  • 0 flowers in the 2nd garden
  • 2 flowers in the 3rd garden

The gardens will then be [5,4,5,5]. She planted a total of 3 + 0 + 0 + 2 = 5 flowers.
There are 3 gardens that are complete.
The minimum number of flowers in the incomplete gardens is 4.
Thus, the total beauty is 3 _ 2 + 4 _ 6 = 6 + 24 = 30.
No other way of planting flowers can obtain a total beauty higher than 30.
Note that Alice could make all the gardens complete but in this case, she would obtain a lower total beauty.

Constraints:

  • 1 <= flowers.length <= 105
  • 1 <= flowers[i], target <= 105
  • 1 <= newFlowers <= 1010
  • 1 <= full, partial <= 105

将 flowers 进行倒序排序, 对于任意一个 flowers[i], 我们要么把 flowers[i]补齐达到 target, 要么我们将 new_flowers 放到 flowers[i…]中尽可能使其中的最小值最大化。

假设 dp[n][i]是剩余 n 株花时 flowers[i…]所能拿到的最大的 total beauty, 那 dp[n][i] = max(dp[n - (target - flowers[i])][i+1] + full, max_minimum(i)), 其中 dp[n-(target - flowers[i])][i+1] + full 是 flowers[i]补齐为 target 后所能拿到的最大收益, max_minumum(i)是将剩余的 flower 都栽种到 flowers[i…]中来尽可能提高其中的最小值所能得到的最大收益

max_minimum(i)我们可以通过 suffix sum 外加 binary search 的方法来进行查找, 因为 flowers 已经进行倒序排列, 所以对于 flowers[i], 我们要检查把剩余的花都种在 flowers[i…]中最终得到的平均值是不是能大于 flowers[i], 如果大于我们继续检查左半部分, 否则我们继续检查右半部分。



use std::collections::HashMap;

impl Solution {
   
    fn dp
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值