Skip to content

Commit 8c9073c

Browse files
committed
Add solution #1419
1 parent a9e93df commit 8c9073c

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,294 LeetCode solutions in JavaScript
1+
# 1,295 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1085,6 +1085,7 @@
10851085
1416|[Restore The Array](./solutions/1416-restore-the-array.js)|Hard|
10861086
1417|[Reformat The String](./solutions/1417-reformat-the-string.js)|Easy|
10871087
1418|[Display Table of Food Orders in a Restaurant](./solutions/1418-display-table-of-food-orders-in-a-restaurant.js)|Medium|
1088+
1419|[Minimum Number of Frogs Croaking](./solutions/1419-minimum-number-of-frogs-croaking.js)|Medium|
10881089
1422|[Maximum Score After Splitting a String](./solutions/1422-maximum-score-after-splitting-a-string.js)|Easy|
10891090
1431|[Kids With the Greatest Number of Candies](./solutions/1431-kids-with-the-greatest-number-of-candies.js)|Easy|
10901091
1436|[Destination City](./solutions/1436-destination-city.js)|Easy|
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* 1419. Minimum Number of Frogs Croaking
3+
* https://leetcode.com/problems/minimum-number-of-frogs-croaking/
4+
* Difficulty: Medium
5+
*
6+
* You are given the string croakOfFrogs, which represents a combination of the string "croak"
7+
* from different frogs, that is, multiple frogs can croak at the same time, so multiple "croak"
8+
* are mixed.
9+
*
10+
* Return the minimum number of different frogs to finish all the croaks in the given string.
11+
*
12+
* A valid "croak" means a frog is printing five letters 'c', 'r', 'o', 'a', and 'k' sequentially.
13+
* The frogs have to print all five letters to finish a croak. If the given string is not a
14+
* combination of a valid "croak" return -1.
15+
*/
16+
17+
/**
18+
* @param {string} croakOfFrogs
19+
* @return {number}
20+
*/
21+
var minNumberOfFrogs = function(croakOfFrogs) {
22+
let activeFrogs = 0;
23+
let maxFrogs = 0;
24+
const counts = { c: 0, r: 0, o: 0, a: 0, k: 0 };
25+
const order = 'croak';
26+
27+
for (const char of croakOfFrogs) {
28+
if (!order.includes(char)) return -1;
29+
counts[char]++;
30+
31+
if (char === 'c') {
32+
activeFrogs++;
33+
maxFrogs = Math.max(maxFrogs, activeFrogs);
34+
} else if (char === 'k') {
35+
activeFrogs--;
36+
}
37+
38+
for (let i = 1; i < order.length; i++) {
39+
if (counts[order[i]] > counts[order[i - 1]]) return -1;
40+
}
41+
}
42+
43+
return activeFrogs === 0 && counts.c === counts.k ? maxFrogs : -1;
44+
};

0 commit comments

Comments
 (0)