{"id":6649,"date":"2020-04-20T00:58:46","date_gmt":"2020-04-20T07:58:46","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6649"},"modified":"2020-04-20T01:19:39","modified_gmt":"2020-04-20T08:19:39","slug":"leetcode-1419-minimum-number-of-frogs-croaking","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-1419-minimum-number-of-frogs-croaking\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1419. Minimum Number of Frogs Croaking"},"content":{"rendered":"\n<p>Given the string&nbsp;<code>croakOfFrogs<\/code>, which represents a combination of the string &#8220;croak&#8221; from different frogs, that is, multiple frogs can croak at the same time, so multiple \u201ccroak\u201d are mixed.&nbsp;<em>Return the minimum number of&nbsp;<\/em>different<em>&nbsp;frogs to finish all the croak in the given string.<\/em><\/p>\n\n\n\n<p>A valid &#8220;croak&#8221;&nbsp;means a frog is printing 5 letters \u2018c\u2019, \u2019r\u2019, \u2019o\u2019, \u2019a\u2019, \u2019k\u2019&nbsp;<strong>sequentially<\/strong>.&nbsp;The frogs have to print all five letters to finish a croak.&nbsp;If the given string is not a combination of valid&nbsp;&#8220;croak&#8221;&nbsp;return -1.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> croakOfFrogs = \"croakcroak\"\n<strong>Output:<\/strong> 1 \n<strong>Explanation:<\/strong> One frog yelling \"croak<strong>\"<\/strong> twice.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> croakOfFrogs = \"crcoakroak\"\n<strong>Output:<\/strong> 2 \n<strong>Explanation:<\/strong> The minimum number of frogs is two.&nbsp;\nThe first frog could yell \"<strong>cr<\/strong>c<strong>oak<\/strong>roak\".\nThe second frog could yell later \"cr<strong>c<\/strong>oak<strong>roak<\/strong>\".\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> croakOfFrogs = \"croakcrook\"\n<strong>Output:<\/strong> -1\n<strong>Explanation:<\/strong> The given string is an invalid combination of \"croak<strong>\"<\/strong> from different frogs.\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> croakOfFrogs = \"croakcroa\"\n<strong>Output:<\/strong> -1\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;=&nbsp;croakOfFrogs.length &lt;= 10^5<\/code><\/li><li>All characters in the string are:&nbsp;<code>'c'<\/code>,&nbsp;<code>'r'<\/code>,&nbsp;<code>'o'<\/code>,&nbsp;<code>'a'<\/code>&nbsp;or&nbsp;<code>'k'<\/code>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Hashtable<\/strong><\/h2>\n\n\n\n<p>Count the frequency of the letters, we need to make sure f[c] &gt;= f[r] &gt;= f[o] &gt;= f[a] &gt;= f[k] holds all the time, otherwise return -1.<br>whenever encounter c, increase the current frog, whenever there is k, decrease the frog count.<br>Don&#8217;t forget to check the current frog number, should be 0 in the end, otherwise there are open letters.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int minNumberOfFrogs(string croakOfFrogs) {\n    vector<int> idx(26);\n    idx['c' - 'a'] = 0;\n    idx['r' - 'a'] = 1;\n    idx['o' - 'a'] = 2;\n    idx['a' - 'a'] = 3;\n    idx['k' - 'a'] = 4;\n    vector<int> count(5);\n    int cur = 0;\n    int ans = 0;    \n    for (char ch : croakOfFrogs) {\n      const int i = idx[ch - 'a'];\n      ++count[i];\n      if (ch == 'c') {\n        ans = max(ans, ++cur);\n        continue;\n      }\n      if (count[i] > count[i - 1]) return -1;\n      if (ch == 'k') --cur;\n    }\n    return cur ? -1 : ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given the string&nbsp;croakOfFrogs, which represents a combination of the string &#8220;croak&#8221; from different frogs, that is, multiple frogs can croak at the same time, so&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[82,177,180,4],"class_list":["post-6649","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-hashtable","tag-medium","tag-stack","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6649","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=6649"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6649\/revisions"}],"predecessor-version":[{"id":6651,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6649\/revisions\/6651"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6649"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6649"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6649"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}