欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【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;
    }
    
    
}

运行结果

【leetcode】1419. Minimum Number of Frogs Croaking

相关标签: leetcode