【模拟】Leetcode 数青蛙

题目讲解

1419. 数青蛙


算法讲解

cpp 复制代码
class Solution {
public:
    int minNumberOfFrogs(string croakOfFrogs) {
        string target = "croak";
        int n = target.size();

        //保存target每个字符的位置index
        unordered_map<char, int>index;
        for(int i = 0; i < n; i++)index[target[i]] = i;

        //按照target的顺序制作Hash
        vector<int>Hash(n); 

        for(int i = 0; i < croakOfFrogs.size(); i++)
        {
            if(croakOfFrogs[i] == 'c')
            {
                if(Hash[n-1] != 0)Hash[n-1]--;
                Hash[0]++;
            }
            else
            {
                //获取当前 croak 字母的下标
                int char_index = index[croakOfFrogs[i]];
                //判断前一个字符是否在Hash中出现 
                if(Hash[char_index - 1] == 0)return -1;
                Hash[char_index - 1]--; Hash[char_index]++;
            }

           
        }
         //怕出现这种croakcro
        for(int i = 0; i < n-1; i++)
        {
            if(Hash[i] != 0)return -1;
        }
        return Hash[n-1];
    }
};
相关推荐
INGNIGHT34 分钟前
91 · 最小调整代价(序列dp)
数据结构·算法·leetcode
lunar_coder8 小时前
哈希扩展 -- 位图思想
c++·算法·哈希算法
Navigator_Z9 小时前
LeetCode //C - 1306. Jump Game III
c语言·算法·leetcode
傅科摆 _ py9 小时前
回溯力扣复习
算法·leetcode·职场和发展
一去不复返的通信er18 小时前
力扣-图论-岛屿数量
算法·leetcode·图论
Xin7701 天前
LeetCode 79. 单词搜索(DFS 回溯法)
leetcode
qq_452396231 天前
第四篇:《索引原理与实战:B-tree、Hash、GIN、GiST、BRIN》
算法·哈希算法·gin
INGNIGHT1 天前
440 · 背包问题(三)(无限次数)
算法·leetcode·动态规划
Tisfy1 天前
LeetCode 3619.总价值可以被 K 整除的岛屿数目:广度优先搜索BFS
算法·leetcode·bfs·宽度优先
文丑颜不良啊1 天前
LeetCode926~950题目解析
java·leetcode