Skip to content

Latest commit

 

History

History
49 lines (36 loc) · 1.25 KB

_2086. Minimum Number of Food Buckets to Feed the Hamsters.md

File metadata and controls

49 lines (36 loc) · 1.25 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : July 01, 2024

Last updated : July 01, 2024


Related Topics : String, Dynamic Programming, Greedy

Acceptance Rate : 46.482 %


Solutions

Java

class Solution {
    public int minimumBuckets(String hamsters) {
        int counter = 0;

        for (int i = 0; i < hamsters.length(); i++) {
            if (hamsters.charAt(i) != 'H') {
                continue;
            }
            counter++;
            
            if ((i == hamsters.length() - 1 || hamsters.charAt(i + 1) == 'H') && (i == 0 || hamsters.charAt(i - 1) == 'H')) {
                return -1;
            }
            if (i != hamsters.length() - 1 && hamsters.charAt(i + 1) != 'H') {
                i += 2;
            }
        }

        return counter;
    }
}