githubEdit

12. Max Min Height

βœ… GFG solution to Maximize Minimum Height of Flowers: find the maximum possible minimum height using binary search and greedy watering strategy. πŸš€

The problem can be found at the following link: πŸ”— Question Linkarrow-up-right

🧩 Problem Description

You are given an array arr[] representing the initial heights of flowers in a garden. You have k days to water the flowers. When you water a flower at position i, it also waters all flowers in the range [i-w, i+w] (where w is the watering window). Each watering increases the height of the affected flowers by 1.

Your task is to maximize the minimum height among all flowers after optimally using the k watering days.

πŸ“˜ Examples

Example 1

Input: arr[] = [2, 3, 4, 5, 1], k = 2, w = 2
Output: 2
Explanation: The minimum height after watering is 2.
Day 1: Water the last two flowers -> arr becomes [2, 3, 4, 6, 2]
Day 2: Water the last two flowers -> arr becomes [2, 3, 4, 7, 3]

Example 2

Input: arr[] = [5, 8], k = 5, w = 1
Output: 9
Explanation: The minimum height after watering is 9.
Day 1 - Day 4: Water the first flower -> arr becomes [9, 8]
Day 5: Water the second flower -> arr becomes [9, 9]

πŸ”’ Constraints

  • $1 \le \text{arr.size()} \le 10^5$

  • $1 \le \text{arr}[i] \le 10^9$

  • $1 \le k \le 10^5$

  • $0 \le w < \text{arr.size()}$

βœ… My Approach

The optimal solution uses Binary Search on Answer combined with a Greedy Validation Strategy:

Binary Search + Greedy Checking

  1. Define Search Space:

    • low = minimum value in the array (best we can guarantee without watering)

    • high = minimum value + k (if we water the shortest flower k times)

  2. Binary Search Logic:

    • For each mid value, check if it's possible to make every flower at least mid height using k waterings.

    • If possible, try for a higher answer (move low up)

    • If not possible, reduce the target (move high down)

  3. Greedy Validation (isPossible):

    • Iterate through each flower left to right

    • For each flower, if its current height (including previous waterings in range) is less than target:

      • Water it enough times to reach the target

      • This watering also affects flowers within window w

    • Track remaining days; if we run out (k < 0), return false

  4. Optimization Trick:

    • Maintain cumulative watering effects to avoid recalculating window sums

    • Subtract waterings that fall outside the current window range

πŸ“ Time and Auxiliary Space Complexity

  • Expected Time Complexity: O(n log(k)), where n is the size of the array. The binary search runs in O(log k) iterations, and each validation check takes O(n) time to process all flowers.

  • Expected Auxiliary Space Complexity: O(n), as we use an auxiliary array to track cumulative watering effects during the validation check.

πŸ§‘β€πŸ’» Code (C++)

chevron-right⚑ View Alternative Approaches with Code and Analysishashtag

πŸ“Š 2️⃣ Prefix Sum Optimization

πŸ’‘ Algorithm Steps:

  1. Use binary search on the answer range as main approach.

  2. In validation, maintain a running prefix sum of water additions.

  3. Calculate current height by adding prefix effects and subtracting out-of-window effects.

  4. Greedily water each flower that falls below target height.

πŸ“ Complexity Analysis:

  • Time: ⏱️ O(n log k) - Binary search with linear validation

  • Auxiliary Space: πŸ’Ύ O(n) - Water tracking array

βœ… Why This Approach?

  • Clear prefix sum semantics

  • Explicit window boundary handling

  • Easy to debug and verify correctness

πŸ“Š 3️⃣ Simplified Greedy Approach

πŸ’‘ Algorithm Steps:

  1. Binary search on answer with streamlined bounds.

  2. For validation, track cumulative water effect in single variable.

  3. Process flowers sequentially, adding water as needed.

  4. Subtract window-expired water effects on the fly.

πŸ“ Complexity Analysis:

  • Time: ⏱️ O(n log k) - Standard binary search complexity

  • Auxiliary Space: πŸ’Ύ O(n) - Water tracking array

βœ… Why This Approach?

  • Single-pass validation per binary search iteration

  • Direct calculation without recursion

  • Handles large values with long long

πŸ†š πŸ” Comparison of Approaches

πŸš€ Approach

⏱️ Time Complexity

πŸ’Ύ Space Complexity

βœ… Pros

⚠️ Cons

🎯 Binary Search + Greedy

🟒 O(n log k)

🟑 O(n)

πŸš€ Optimal and clean

πŸ’Ύ Requires auxiliary array

πŸ”„ Prefix Sum Optimization

🟒 O(n log k)

🟑 O(n)

πŸ“Š Clear prefix semantics

πŸ”§ Similar to main approach

🎨 Simplified Greedy

🟒 O(n log k)

🟑 O(n)

πŸ“– Easy to understand

πŸ”§ Handles overflow with long long

πŸ† Best Choice Recommendation

🎯 Scenario

πŸŽ–οΈ Recommended Approach

πŸ”₯ Performance Rating

πŸ… Competitive programming

πŸ₯‡ Binary Search + Greedy

β˜…β˜…β˜…β˜…β˜…

πŸ“– Interview clarity

πŸ₯ˆ Prefix Sum Optimization

β˜…β˜…β˜…β˜…β˜…

🎯 Learning purposes

πŸ… Simplified Greedy

β˜…β˜…β˜…β˜…β˜†

β˜• Code (Java)

🐍 Code (Python)

🧠 Contribution and Support

For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: πŸ“¬ Any Questions?arrow-up-right. Let's make this learning journey more collaborative!

⭐ If you find this helpful, please give this repository a star! ⭐


πŸ“Visitor Count

Visitor counter

Last updated