2086. Minimum Number of Buckets Required to Collect Rainwater from Houses Medium

@problem@discussion
#String#Dynamic Programming#Greedy



1/**
2 * [2086] Minimum Number of Buckets Required to Collect Rainwater from Houses
3 *
4 * You are given a 0-indexed string street. Each character in street is either 'H' representing a house or '.' representing an empty space.
5 * You can place buckets on the empty spaces to collect rainwater that falls from the adjacent houses. The rainwater from a house at index i is collected if a bucket is placed at index i - 1 and/or index i + 1. A single bucket, if placed adjacent to two houses, can collect the rainwater from both houses.
6 * Return the minimum number of buckets needed so that for every house, there is at least one bucket collecting rainwater from it, or -1 if it is impossible.
7 *  
8 * Example 1:
9 * 
10 * Input: street = "H..H"
11 * Output: 2
12 * Explanation:
13 * We can put buckets at index 1 and index 2.
14 * "H..H" -> "HBBH" ('B' denotes where a bucket is placed).
15 * The house at index 0 has a bucket to its right, and the house at index 3 has a bucket to its left.
16 * Thus, for every house, there is at least one bucket collecting rainwater from it.
17 * 
18 * Example 2:
19 * 
20 * Input: street = ".H.H."
21 * Output: 1
22 * Explanation:
23 * We can put a bucket at index 2.
24 * ".H.H." -> ".HBH." ('B' denotes where a bucket is placed).
25 * The house at index 1 has a bucket to its right, and the house at index 3 has a bucket to its left.
26 * Thus, for every house, there is at least one bucket collecting rainwater from it.
27 * 
28 * Example 3:
29 * 
30 * Input: street = ".HHH."
31 * Output: -1
32 * Explanation:
33 * There is no empty space to place a bucket to collect the rainwater from the house at index 2.
34 * Thus, it is impossible to collect the rainwater from all the houses.
35 * 
36 *  
37 * Constraints:
38 * 
39 * 	1 <= street.length <= 10^5
40 * 	street[i] is either'H' or '.'.
41 * 
42 */
43pub struct Solution {}
44
45// problem: https://leetcode.com/problems/minimum-number-of-buckets-required-to-collect-rainwater-from-houses/
46// discuss: https://leetcode.com/problems/minimum-number-of-buckets-required-to-collect-rainwater-from-houses/discuss/?currentPage=1&orderBy=most_votes&query=
47
48// submission codes start here
49
50impl Solution {
51    pub fn minimum_buckets(street: String) -> i32 {
52        0
53    }
54}
55
56// submission codes end
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61
62    #[test]
63    fn test_2086() {
64    }
65}
66


Back
© 2025 bowen.ge All Rights Reserved.