add: 230506-CN

This commit is contained in:
Eatswap 2023-05-06 19:12:38 +08:00
parent bce7fa8ad0
commit 062ec6091a
Signed by: Eatswap
GPG Key ID: BE661106A1F3FA0B
3 changed files with 58 additions and 2 deletions

52
cpp/2305/LC230506CN.cpp Normal file
View File

@ -0,0 +1,52 @@
#include <algorithm>
#include <string>
/**
* 1419. Minimum Number of Frogs Croaking
*
* You are given the string croakOfFrogs, which represents a combination of the string "croak" from different frogs, that is, multiple frogs can croak at the same time, so multiple "croak" are mixed.
* Return the minimum number of different frogs to finish all the croaks in the given string.
* A valid "croak" means a frog is printing five letters 'c', 'r', 'o', 'a', and 'k' sequentially. The frogs have to print all five letters to finish a croak. If the given string is not a combination of a valid "croak" return -1.
*/
class LC230506CN {
public:
static int minNumberOfFrogs(const std::string&) noexcept;
};
int LC230506CN::minNumberOfFrogs(const std::string& croakOfFrogs) noexcept {
if (croakOfFrogs.length() % 5)
return -1;
int cnt[4]{}, total = 0, ret = 0;
for (char c: croakOfFrogs) {
switch (c) {
case 'c':
++total, ++cnt[0];
break;
case 'r':
if (!cnt[0])
return -1;
--cnt[0], ++cnt[1];
break;
case 'o':
if (!cnt[1])
return -1;
--cnt[1], ++cnt[2];
break;
case 'a':
if (!cnt[2])
return -1;
--cnt[2], ++cnt[3];
break;
case 'k':
if (!cnt[3])
return -1;
--cnt[3], --total;
break;
default:
return -1;
}
ret = std::max(ret, total);
}
return total ? -1 : ret;
}

View File

@ -65,4 +65,9 @@ public:
static int numSubseq(std::vector<int>&, int) noexcept;
};
class LC230506CN {
public:
static int minNumberOfFrogs(const std::string&) noexcept;
};
#endif //LEETCODE_CPP_DEFS_H

View File

@ -12,7 +12,6 @@ std::ostream& operator<<(std::ostream& os, const std::vector<T>& x) noexcept {
}
int main() {
std::vector<int> arr {2,3,3,4,6,7};
std::cout << LC230506::numSubseq(arr,12);
std::cout << LC230506CN::minNumberOfFrogs("crcoakroak");
return 0;
}