贪心专题3 - leetcode763. Partition Labels/605. Can Place Flowers - Mark

763. Partition Labels

题目描述

字符串S只包含小写字母。想把字符串拆成尽可能多的片段,同一个字母只会出现在其中的一个片段。返回一个表示每个字符串片段的长度的列表。

例子

Input: S = “ababcbacadefegdehijhklij”
Output: [9,7,8]
Explanation:
The partition is “ababcbaca”, “defegde”, “hijhklij”.
This is a partition so that each letter appears in at most one part.
A partition like “ababcbacadefegde”, “hijhklij” is incorrect, because it splits S into less parts.

思想
贪心!
遍历S,记录每一个字符的最大索引~

解法
暴力,时间复杂度O(n^2)

class Solution(object):
    def partitionLabels(self, S):
        """
        :type S: str
        :rtype: List[int]
        """
        res = []
        start = end = 0
        for i, c in enumerate(S):
            end = max(end, S.rfind(c))
            if i == end:
                res.append(end - start + 1)
                start = end + 1
        return res

改进:首先用字典记录每个字符的最大索引。时间复杂度O(n),空间复杂度O(n)。

class Solution(object):
    def partitionLabels(self, S):
        """
        :type S: str
        :rtype: List[int]
        """
        dic = {}
        for i, c in enumerate(S):
            dic[c] = i
            
        res = []
        start = end = 0
        for i, c in enumerate(S):
            end = max(end, dic[c])
            if i == end:
                res.append(end - start + 1)
                start = end + 1
        return res

605. Can Place Flowers

题目描述

假设你有一个很长的花坛,一部分地块种植了花,另一部分却没有。
但花不能种植在相邻的地块上,因为它们会争夺水源,两者都会死去。

给定一个只包含0和1的花坛数组(其中0表示没种花,1表示种了花),和一个数n 。
问能否在不打破种植规则的情况下种n朵花?

例子

Example 1:
Input: flowerbed = [1,0,0,0,1], n = 1
Output: True

Example 2:
Input: flowerbed = [1,0,0,0,1], n = 2
Output: False

思想
只需统计连续0的数量
1)开头/结尾,连续2/3 —— 种1个;连续4/5 —— 种2个;…
2)连续3/4个 —— 种1个;连续5/6个 —— 种2个;…

Trick:对开头和结尾进行填充

解法

class Solution(object):
    def canPlaceFlowers(self, flowerbed, n):
        """
        :type flowerbed: List[int]
        :type n: int
        :rtype: bool
        """
        cnt = 0
        zero = 1
        for f in flowerbed:
            if f == 0:
                zero += 1
            else:
                cnt += (zero - 1) // 2
                zero = 0
        if zero:    # Tail
            cnt += zero // 2
        return n <= cnt

改进。预填充flowerbed

class Solution(object):
    def canPlaceFlowers(self, flowerbed, n):
        """
        :type flowerbed: List[int]
        :type n: int
        :rtype: bool
        """
        flowerbed = [1, 0] + flowerbed + [0, 1]
        
        cnt = zero = 0
        for f in flowerbed:
            if f == 0:
                zero += 1
            elif zero > 0:
                cnt += (zero - 1)//2
                zero = 0
        return n <= cnt

你可能感兴趣的:(Leetcode)