【leetcode】1419. Minimum Number of Frogs Croaking
程序员文章站
2024-02-14 09:50:34
...
提交代码
class Solution {
public int minNumberOfFrogs(String croakOfFrogs) {
ArrayList<Integer> allCroaks=new ArrayList<Integer>();
char[] chars=croakOfFrogs.toCharArray();
boolean added=true;
int res=0;
for(char c:chars) {
if(c=='c') {
allCroaks.add(1);
res=allCroaks.size()>res?allCroaks.size():res;
}else if(c=='r') {
added=false;
for(int i=0;i<allCroaks.size();i++) {
if(allCroaks.get(i)==1) {
allCroaks.set(i, 2);
added=true;
break;
}
}
if(!added) return -1;
}else if(c=='o') {
added=false;
for(int i=0;i<allCroaks.size();i++) {
if(allCroaks.get(i)==2) {
allCroaks.set(i,3);
added=true;
break;
}
}
if(!added) return -1;
}else if(c=='a') {
added=false;
for(int i=0;i<allCroaks.size();i++) {
if(allCroaks.get(i)==3) {
allCroaks.set(i, 4);
added=true;
break;
}
}
if(!added) return -1;
}else if(c=='k') {
added=false;
for(int i=0;i<allCroaks.size();i++) {
if(allCroaks.get(i)==4) {
added=true;
allCroaks.remove(i);
break;
}
}
if(!added) return -1;
}
}
return allCroaks.size()==0?res:-1;
}
}
运行结果
下一篇: MapReduce实现分组排序
推荐阅读
-
【leetcode】1419. Minimum Number of Frogs Croaking
-
Leetcode 452. Minimum Number of Arrows to Burst Balloons(python+cpp)
-
【leetcode】1482. Minimum Number of Days to Make m Bouquets
-
leetcode[452] Minimum Number of Arrows to Burst Balloons
-
LeetCode 1326. Minimum Number of Taps to Open to Water a Garden 动态规划 离散化 贪心
-
[LeetCode 871] Minimum Number of Refueling Stops