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

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(suffix_sum: &[i64], flowers: &[i64], new_flowers: i64, target: i64, full: i64, partial: i64, i: usize, cache: &mut HashMap<(usize, i64), i64>) -> i64 {
        if i == flowers.len() {
            return 0;
        }
        if let Some(c) = cache.get(&(i, new_flowers)) {
            return *c;
        }
        let diff_to_target = target - flowers[i];
        if diff_to_target <= 0 {
            return Solution::dp(suffix_sum, flowers, new_flowers, target, full, partial, i + 1, cache) + full;
        }
        let mut ans = 0;
        if new_flowers >= diff_to_target {
            ans = Solution::dp(suffix_sum, flowers, new_flowers - diff_to_target, target, full, partial, i + 1, cache) + full;
        }
        let mut l = i;
        let mut r = suffix_sum.len() - 1;
        while l < r {
            let m = (l + r) / 2;
            let needed = flowers[m] * (flowers.len() - m) as i64 - suffix_sum[m];
            if new_flowers >= needed {
                r = m;
                continue;
            }
            l = m + 1;
        }
        ans = ans.max(((suffix_sum[l] + new_flowers) / (flowers.len() - l) as i64).min(target - 1) * partial);
        cache.insert((i, new_flowers), ans);
        ans
    }

    pub fn maximum_beauty(flowers: Vec<i32>, new_flowers: i64, target: i32, full: i32, partial: i32) -> i64 {
        let mut flowers: Vec<i64> = flowers.into_iter().map(|v| v as i64).collect();
        flowers.sort();
        flowers.reverse();
        let mut suffix_sum: Vec<i64> = flowers
            .iter()
            .rev()
            .scan(0, |s, v| {
                *s += *v;
                Some(*s)
            })
            .collect();
        suffix_sum.reverse();
        Solution::dp(&suffix_sum, &flowers, new_flowers, target as i64, full as i64, partial as i64, 0, &mut HashMap::new())
    }
}

你可能感兴趣的:(leetcode,c++,动态规划)