zoukankan      html  css  js  c++  java
  • 【leetcode】1578. Minimum Deletion Cost to Avoid Repeating Letters

    题目如下:

    Given a string s and an array of integers cost where cost[i] is the cost of deleting the ith character in s.

    Return the minimum cost of deletions such that there are no two identical letters next to each other.

    Notice that you will delete the chosen characters at the same time, in other words, after deleting a character, the costs of deleting other characters will not change.

    Example 1:

    Input: s = "abaac", cost = [1,2,3,4,5]
    Output: 3
    Explanation: Delete the letter "a" with cost 3 to get "abac" (String without two identical letters next to each other).
    

    Example 2:

    Input: s = "abc", cost = [1,2,3]
    Output: 0
    Explanation: You don't need to delete any character because there are no identical letters next to each other.
    

    Example 3:

    Input: s = "aabaa", cost = [1,2,3,4,1]
    Output: 2
    Explanation: Delete the first and the last character, getting the string ("aba").
    

    Constraints:

    • s.length == cost.length
    • 1 <= s.length, cost.length <= 10^5
    • 1 <= cost[i] <= 10^4
    • s contains only lowercase English letters.

    解题思路:四个月前写的代码了,突然有点看不懂为什么这么做。先mark一下,后面再编辑。

    代码如下:

    class Solution(object):
        def minCost(self, s, cost):
            """
            :type s: str
            :type cost: List[int]
            :rtype: int
            """
            res = 0
            consecutive_char = None
            max_cost = 0
            total_cost = 0
    
            s += '#'
            cost += [0]
    
            for i,c in zip(s,cost):
                if consecutive_char == None:
                    consecutive_char = i
                    max_cost = c
                    total_cost = c
                elif i != consecutive_char:
                    res += (total_cost - max_cost)
                    max_cost = c
                    total_cost = c
                    consecutive_char = i
                elif i == consecutive_char:
                    max_cost = max(max_cost,c)
                    total_cost += c
    
            return res
  • 相关阅读:
    使用Modelsim对Nios II系统进行系统级仿真
    电视相关知识学习
    SAD和SATD的区别[摘]
    vim自动补全括号的最好方法
    vim sinppets插件介绍
    在centos下安装开发环境
    初始化以及动态设置Edit控件的背景及字体颜色
    CTreeCtrl SetItemData 释放问题
    C#4.0 新特性 匿名方法,lambds
    对象序列化Serialization与Deserialize方法进行反序列化
  • 原文地址:https://www.cnblogs.com/seyjs/p/14351536.html
Copyright © 2011-2022 走看看