zoukankan      html  css  js  c++  java
  • 【leetcode】1419. Minimum Number of Frogs Croaking

    题目如下:

    Given the string croakOfFrogs, which represents a combination of the string "croak" from different frogs, that is, multiple frogs can croak at the same time, so multiple “croak” are mixed. Return the minimum number of different frogs to finish all the croak in the given string.

    A valid "croak" means a frog is printing 5 letters ‘c’, ’r’, ’o’, ’a’, ’k’ sequentially. The frogs have to print all five letters to finish a croak. If the given string is not a combination of valid "croak" return -1. 

    Example 1:

    Input: croakOfFrogs = "croakcroak"
    Output: 1 
    Explanation: One frog yelling "croak" twice.
    

    Example 2:

    Input: croakOfFrogs = "crcoakroak"
    Output: 2 
    Explanation: The minimum number of frogs is two. 
    The first frog could yell "crcoakroak".
    The second frog could yell later "crcoakroak".
    

    Example 3:

    Input: croakOfFrogs = "croakcrook"
    Output: -1
    Explanation: The given string is an invalid combination of "croak" from different frogs.
    

    Example 4:

    Input: croakOfFrogs = "croakcroa"
    Output: -1

    Constraints:

    • 1 <= croakOfFrogs.length <= 10^5
    • All characters in the string are: 'c''r''o''a' or 'k'.

    解题思路:从左往右遍历croakOfFrogs,遇到字符c则计数加一,出现一次完整的croak字符串则计数减一,答案就是计数出现过的最大值。

    代码如下:

    class Solution(object):
        def minNumberOfFrogs(self, croakOfFrogs):
            """
            :type croakOfFrogs: str
            :rtype: int
            """
            pending = {}
            lettes = ['k','a','o','r','c']
            res = 0
            for char in croakOfFrogs:
                if char == 'c':
                    pending[char] = pending.setdefault(char,0) + 1
                else:
                    pre = lettes[lettes.index(char) + 1]
                    flag = False
    
                    for key in pending.iterkeys():
                        if pre == key[-1]:
                            flag = True
                            pending[key] -= 1
                            if pending[key] == 0:
                                del pending[key]
                            if key + char != 'croak':
                                pending[key+char] = pending.setdefault(key+char,0) + 1
                            break
                    if flag == False:return -1
    
                count = 0
                for key in pending.iterkeys():
                    count += pending[key]
                res = max(res,count)
    
            # uncomplete string
            if len(pending) > 0:
                return -1
            return res
  • 相关阅读:
    菜鸟学python之程序初体验
    菜鸟学python之大数据的初认识
    js获取本地ip地址和外网IP地址
    Js中foreach()用法及使用的坑
    模拟实现Promise,探究Promise原理
    搞懂JS的事件循环(Event Loop)和宏任务/微任务
    NodeJS 中的 LRU 缓存(CLOCK-2-hand)实现
    设计模式在前端项目中的应用
    JS 中一些高效的魔法运算符
    Js中如何克隆对象?
  • 原文地址:https://www.cnblogs.com/seyjs/p/13026424.html
Copyright © 2011-2022 走看看