forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d4486ef
commit b836fc1
Showing
7 changed files
with
110 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class Solution { | ||
public: | ||
int minNumberOfFrogs(string cr) { | ||
int c = 0, r = 0, a = 0, k = 0, o = 0, frogs = 0, res = 0; | ||
for(char i : cr) { | ||
if (i=='c') {c++; frogs++;} | ||
else if (i=='r') r++; | ||
else if (i=='o') o++; | ||
else if (i=='a') a++; | ||
else {k++; frogs--;} | ||
res = max(res, frogs); | ||
if (c < r || r < o || o < a || a < k) return -1; | ||
} | ||
if (c == r && c == o && c == a && c == k) return res; | ||
return -1; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
func minNumberOfFrogs(cr string) int { | ||
res, frogs := 0, 0 | ||
st := make([]int, 5) | ||
for _, i := range []byte(cr) { | ||
if i == 'c' { | ||
st[0]++ | ||
frogs++ | ||
} else if i == 'r' { | ||
st[1]++ | ||
} else if i == 'o' { | ||
st[2]++ | ||
} else if i == 'a' { | ||
st[3]++ | ||
} else { | ||
st[4]++ | ||
frogs-- | ||
} | ||
res = max(res, frogs) | ||
if st[0] < st[1] || st[1] < st[2] || st[2] < st[3] || st[3] < st[4] { | ||
return -1 | ||
} | ||
} | ||
|
||
if st[0] == st[1] && st[1] == st[2] && st[2] == st[3] && st[3] == st[4] { | ||
return res | ||
} | ||
return -1 | ||
} | ||
|
||
func max(a, b int) int { | ||
if a > b { | ||
return a | ||
} | ||
return b | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class Solution { | ||
public int minNumberOfFrogs(String cr) { | ||
int c = 0, r = 0, a = 0, k = 0, o = 0, frogs = 0, res = 0; | ||
for(char i : cr.toCharArray()) { | ||
if (i=='c') {c++; frogs++;} | ||
else if (i=='r') r++; | ||
else if (i=='o') o++; | ||
else if (i=='a') a++; | ||
else {k++; frogs--;} | ||
res = Math.max(res, frogs); | ||
if (c < r || r < o || o < a || a < k) return -1; | ||
} | ||
if (c == r && c == o && c == a && c == k) return res; | ||
return -1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
var minNumberOfFrogs = function(cr) { | ||
let c = 0, r = 0, a = 0, k = 0, o = 0, frogs = 0, res = 0; | ||
for(let i of cr) { | ||
if (i=='c') {c++; frogs++;} | ||
else if (i=='r') r++; | ||
else if (i=='o') o++; | ||
else if (i=='a') a++; | ||
else {k++; frogs--;} | ||
res = Math.max(res, frogs); | ||
if (c < r || r < o || o < a || a < k) return -1; | ||
} | ||
if (c == r && c == o && c == a && c == k) return res; | ||
return -1; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class Solution: | ||
def minNumberOfFrogs(self, cr: str) -> int: | ||
res, frogs = 0, 0 | ||
st = [0] * 5 | ||
for i in cr: | ||
if i == 'c': | ||
st[0] += 1 | ||
frogs += 1 | ||
elif i == 'r': | ||
st[1] += 1 | ||
elif i == 'o': | ||
st[2] += 1 | ||
elif i == 'a': | ||
st[3] += 1 | ||
else: | ||
st[4] += 1 | ||
frogs -= 1 | ||
res = max(res, frogs) | ||
if not (st[0] >= st[1] >= st[2] >= st[3] >= st[4]): return -1 | ||
|
||
if st[0] == st[1] == st[2] == st[3] == st[4]: | ||
return res | ||
return -1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters