← Home

1419. Minimum Number of Frogs Croaking

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
141

This is one of those problems where the clean idea matters more than the amount of code. For 1419. Minimum Number of Frogs Croaking, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

The first job is to translate the English prompt into state, transition, and stopping conditions. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: straightforward implementation.

Guide

When?

This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are minNumberOfFrogs, count.

Guide

Why?

The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.

  • A map keeps the lookup side cheap; the code pays a little memory to avoid repeated searching.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Start from the smallest reliable state.
  2. Expand one legal move at a time.
  3. Cache, count, or merge information as soon as it becomes settled.
  4. Let the final stored value answer the original question.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 312 ms, faster than 20.09% of C++ online submissions for Minimum Number of Frogs Croaking.
02//Memory Usage: 9.1 MB, less than 100.00% of C++ online submissions for Minimum Number of Frogs Croaking.
03class Solution {
04public:
05    int minNumberOfFrogs(string croakOfFrogs) {
06        int pos = -2; //represents for meaningless value
07        int visited = 0;
08        int count = 0;
09        
10        vector<string> croak = {"c", "r", "o", "a", "k"};
11        
12        while(visited < croakOfFrogs.size()){
13            pos = -2;
14            bool jump = false; //whether to jump from inner loop
15            while(pos == -2 || (pos >= 0 && pos < croakOfFrogs.size())){
16                for(string s : croak){
17                    pos = croakOfFrogs.find(s, (pos == -2) ? 0 : pos+1);
18                    if(pos != string::npos){
19                        //revise the char in-place, so the char won't be visited again
20                        croakOfFrogs[pos] = '#';
21                        visited++;
22                    }else{
23                        /*
24                        not jump at first char, invalid string,
25                        for example, we have found 'c', 'r', 'o',
26                        but we cannot find 'a', it must be invalid string
27                        */
28                        if(s != "c") return -1;
29                        //we need another frog
30                        jump = true;
31                        break;
32                    }
33                }
34                if(jump) break;
35            }
36            
37            //we have used one frog
38            count++;
39        }
40        
41        return count;
42    }
43};
44
45//one pass
46//https://leetcode.com/problems/minimum-number-of-frogs-croaking/discuss/586543/C%2B%2BJava-with-picture-simulation
47//Runtime: 76 ms, faster than 69.22% of C++ online submissions for Minimum Number of Frogs Croaking.
48//Memory Usage: 9.5 MB, less than 100.00% of C++ online submissions for Minimum Number of Frogs Croaking.
49class Solution {
50public:
51    int minNumberOfFrogs(string croakOfFrogs) {
52        unordered_map<char, int> croak2id = {
53            {'c', 0},
54            {'r', 1},
55            {'o', 2},
56            {'a', 3},
57            {'k', 4}
58        };
59        
60        vector<int> count(5, 0);
61        int curFrogs = 0, maxFrogs = 0;
62        
63        for(char c : croakOfFrogs){
64            int id = croak2id[c];
65            if(id == 0){
66                //we need a new frog
67                count[0]++;
68                curFrogs++;
69                maxFrogs = max(maxFrogs, curFrogs);
70            }else if(id == 4){
71                //one frog finish singing
72                count[3]--;
73                curFrogs--;
74            }else{
75                //one frog sing from previous char to current char
76                --count[id-1];
77                ++count[id];
78                if(count[id-1] < 0) return -1;
79            }
80        }
81        
82        //curFrogs == 0: check if all frogs successfully finish
83        return (curFrogs == 0) ? maxFrogs : -1;
84    }
85};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.