【1419. 数青蛙】

目录

一、题目描述

二、算法原理

三、代码实现

代码实现1:我自己写的比较挫,但是比较简单。

cpp 复制代码
class Solution {
public:
    int minNumberOfFrogs(string croakOfFrogs) 
    {
        unordered_map<char,int> hash;
        hash['c']=0,hash['r']=0,hash['o']=0,hash['a']=0,hash['k']=0;

        for(int i=0;i<croakOfFrogs.size();i++)
        {
            if(croakOfFrogs[i]=='c')
            {
                if(hash['k']==0)
                    hash['c']++;
                else
                {
                    hash['k']--;
                    hash['c']++;
                }

            }
            else if(croakOfFrogs[i]=='r')
            {
                if(hash['c']!=0)
                {
                    hash['r']++;
                    hash['c']--;
                }
                else
                {
                    return -1;

                }
            }
            else if(croakOfFrogs[i]=='o')
            {
                if(hash['r']!=0)
                {
                    hash['o']++;
                    hash['r']--;
                }
                else
                {
                    return -1;

                }
            }
            else if(croakOfFrogs[i]=='a')
            {
                if(hash['o']!=0)
                {
                    hash['a']++;
                    hash['o']--;
                }
                else
                {
                    return -1;

                }
            }
            else if(croakOfFrogs[i]=='k')
            {
                if(hash['a']!=0)
                {
                    hash['k']++;
                    hash['a']--;
                }
                else
                {
                    return -1;
                }
            }
        }
        string str="croa";
        for(int i=0;i<str.size();i++)
        {
            if(hash[str[i]]!=0)
            {
                return -1;
            }
        }
        return hash['k'];
    }
};

代码实现2:大佬写的,干净简洁。

cpp 复制代码
class Solution {
public:
    int minNumberOfFrogs(string croakOfFrogs) 
    {
        string str="croak";
        int n=str.size();
        vector<int> hash(n);
        unordered_map<char,int> index;

        for(int i=0;i<n;i++)
        {
            index[str[i]]=i;
        }

        for(auto ch:croakOfFrogs)
        {
            if(ch=='c')
            {
                if(hash[n-1]!=0) hash[n-1]--;
                hash[0]++;
            }
            else
            {
                int i=index[ch];
                if(hash[i-1]==0)
                {
                    return -1;
                }
                else
                {
                    hash[i-1]--;
                    hash[i]++;
                }
            }
        }

        for(int i=0;i<n-1;i++)
        {
            if(hash[i]!=0)
            {
                return -1;
            }
        }
        return hash[n-1];
    }
};
相关推荐
西阳未落36 分钟前
LeetCode——双指针(进阶)
c++·算法·leetcode
一二学长1 小时前
异或相关的算法题
算法
暴力求解1 小时前
c++类和对象(下)
开发语言·c++·算法
深栈2 小时前
机器学习:支持向量机
算法·机器学习·支持向量机
刘海东刘海东2 小时前
结构型智能科技理论研究(草稿)
科技·算法
C嘎嘎嵌入式开发2 小时前
(10)100天python从入门到拿捏《Python中的数据结构与自定义数据结构》
数据结构·python·算法
熬了夜的程序员2 小时前
【LeetCode】69. x 的平方根
开发语言·算法·leetcode·职场和发展·动态规划
Niuguangshuo2 小时前
音频特征提取算法介绍
算法·音视频
fengfuyao9853 小时前
基于MATLAB的匈牙利算法实现任务分配
算法·数学建模·matlab
CoovallyAIHub3 小时前
超详细链式插补 (MICE) 多元插补:机器学习模型的高级缺失数据处理
算法·机器学习·计算机视觉