Leetcode 1534 Solution

This article provides solution to leetcode question 1534 (minimum-number-of-frogs-croaking)

https://leetcode.com/problems/minimum-number-of-frogs-croaking

Solution

class Solution:
    def minNumberOfFrogs(self, croakOfFrogs: str) -> int:
        cnts = [0] * 5
        total_cnt = 0

        ans = 0
        for ch in croakOfFrogs:
            if ch == 'c':
                cnts[0] += 1
                total_cnt += 1
            elif ch == 'r':
                if cnts[0] == 0:
                    return -1

                cnts[0] -= 1
                cnts[1] += 1
            elif ch == 'o':
                if cnts[1] == 0:
                    return -1

                cnts[1] -= 1
                cnts[2] += 1
            elif ch == 'a':
                if cnts[2] == 0:
                    return -1

                cnts[2] -= 1
                cnts[3] += 1
            elif ch == 'k':
                if cnts[3] == 0:
                    return -1

                cnts[3] -= 1
                total_cnt -= 1
            else:
                return -1

            ans = max(ans, total_cnt)

        return ans if total_cnt == 0 else -1