2234. Maximum Total Beauty of the Gardens Hard

@problem@discussion
#Array#Two Pointers#Binary Search#Greedy#Sorting



1/**
2 * [2234] Maximum Total Beauty of the Gardens
3 *
4 * Alice is a caretaker of n gardens and she wants to plant flowers to maximize the total beauty of all her gardens.
5 * You are given a 0-indexed integer array flowers of size n, where flowers[i] is the number of flowers already planted in the i^th 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.
6 * 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:
7 * 
8 * 	The number of complete gardens multiplied by full.
9 * 	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.
10 * 
11 * Return the maximum total beauty that Alice can obtain after planting at most newFlowers flowers.
12 *  
13 * Example 1:
14 * 
15 * Input: flowers = [1,3,1,1], newFlowers = 7, target = 6, full = 12, partial = 1
16 * Output: 14
17 * Explanation: Alice can plant
18 * - 2 flowers in the 0^th garden
19 * - 3 flowers in the 1^st garden
20 * - 1 flower in the 2^nd garden
21 * - 1 flower in the 3^rd garden
22 * The gardens will then be [3,6,2,2]. She planted a total of 2 + 3 + 1 + 1 = 7 flowers.
23 * There is 1 garden that is complete.
24 * The minimum number of flowers in the incomplete gardens is 2.
25 * Thus, the total beauty is 1 * 12 + 2 * 1 = 12 + 2 = 14.
26 * No other way of planting flowers can obtain a total beauty higher than 14.
27 * 
28 * Example 2:
29 * 
30 * Input: flowers = [2,4,5,3], newFlowers = 10, target = 5, full = 2, partial = 6
31 * Output: 30
32 * Explanation: Alice can plant
33 * - 3 flowers in the 0^th garden
34 * - 0 flowers in the 1^st garden
35 * - 0 flowers in the 2^nd garden
36 * - 2 flowers in the 3^rd garden
37 * The gardens will then be [5,4,5,5]. She planted a total of 3 + 0 + 0 + 2 = 5 flowers.
38 * There are 3 gardens that are complete.
39 * The minimum number of flowers in the incomplete gardens is 4.
40 * Thus, the total beauty is 3 * 2 + 4 * 6 = 6 + 24 = 30.
41 * No other way of planting flowers can obtain a total beauty higher than 30.
42 * Note that Alice could make all the gardens complete but in this case, she would obtain a lower total beauty.
43 * 
44 *  
45 * Constraints:
46 * 
47 * 	1 <= flowers.length <= 10^5
48 * 	1 <= flowers[i], target <= 10^5
49 * 	1 <= newFlowers <= 10^10
50 * 	1 <= full, partial <= 10^5
51 * 
52 */
53pub struct Solution {}
54
55// problem: https://leetcode.com/problems/maximum-total-beauty-of-the-gardens/
56// discuss: https://leetcode.com/problems/maximum-total-beauty-of-the-gardens/discuss/?currentPage=1&orderBy=most_votes&query=
57
58// submission codes start here
59
60impl Solution {
61    pub fn maximum_beauty(flowers: Vec<i32>, new_flowers: i64, target: i32, full: i32, partial: i32) -> i64 {
62        
63    }
64}
65
66// submission codes end
67
68#[cfg(test)]
69mod tests {
70    use super::*;
71
72    #[test]
73    fn test_2234() {
74    }
75}
76


Back
© 2025 bowen.ge All Rights Reserved.