【陪伴式刷题】Day 32|贪心算法|763.划分字母区间(Partition Labels)

刷题顺序按照代码随想录建议

题目描述

英文版描述

You are given a string s. We want to partition the string into as many parts as possible so that each letter appears in at most one part.

Note that the partition is done so that after concatenating all the parts in order, the resultant string should be s.

Return a list of integers representing the size of these parts.

Example 1:

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.

Example 2:

Input: s = "eccbbbbdec" Output: [10]

Constraints:

  • 1 <= s.length <= 500
  • s consists of lowercase English letters.

英文版地址

leetcode.com/problems/pa...

中文版描述

给你一个字符串 s 。我们要把这个字符串划分为尽可能多的片段,同一字母最多出现在一个片段中。

注意,划分结果需要满足:将所有划分结果按顺序连接,得到的字符串仍然是 s 。

返回一个表示每个字符串片段的长度的列表。

示例 1:

输入: s = "ababcbacadefegdehijhklij" 输出: [9,7,8] 解释: 划分结果为 "ababcbaca"、"defegde"、"hijhklij" 。 每个字母最多出现在一个片段中。 像 "ababcbacadefegde", "hijhklij" 这样的划分是错误的,因为划分的片段数较少。

示例 2:

输入: s = "eccbbbbdec" 输出: [10]

提示:

  • 1 <= s.length <= 500
  • s 仅由小写英文字母组成

中文版地址

leetcode.cn/problems/pa...

解题方法

俺这版

java 复制代码
class Solution {
    List<Integer> result = new ArrayList<>();
    public List<Integer> partitionLabels(String s) {
        if (s == null || s.length() == 1) {
            return result;
        }
        Map<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (map.get(c) != null) {
                map.put(c, i);
            } else {
                map.put(c, i);
            }
        }
        int end = 0;
        int last = -1;
        for (int i = 0; i < s.length(); i++) {
            end = Math.max(end, map.get(s.charAt(i)));
            if (i == end) {
                result.add(i - last);
                last = i;
            }
        }
        return result;
    }
}

复杂度分析

  • 时间复杂度:O(n),其中 n 是字符串长度
  • 空间复杂度:O(1),字母数量确定,最多26个字母,因此额外只需要常数空间

官方版

两种方法逻辑基本一致,主要是官方将Map优化为了数组,效果果然好了很多(不过基于之前有篇文章作了深入的分析与测试,Leetcode官方的这个时间复杂度和空间复杂度大家就姑且看看,图一乐,别太当真)

ini 复制代码
class Solution {
    public List<Integer> partitionLabels(String S) {
        List<Integer> list = new LinkedList<>();
        int[] edge = new int[26];
        char[] chars = S.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            edge[chars[i] - 'a'] = i;
        }
        int idx = 0;
        int last = -1;
        for (int i = 0; i < chars.length; i++) {
            idx = Math.max(idx,edge[chars[i] - 'a']);
            if (i == idx) {
                list.add(i - last);
                last = i;
            }
        }
        return list;
    }
}

复杂度分析

  • 时间复杂度:O(n),其中 n 是字符串长度
  • 空间复杂度:O(1),额外常数空间
相关推荐
mjr21 分钟前
设计模式-Java
java·设计模式
零星_AagT24 分钟前
Apache-CC6链审计笔记
java·笔记·apache·代码审计
01_26 分钟前
力扣hot100 ——搜索二维矩阵 || m+n复杂度优化解法
算法·leetcode·矩阵
SylviaW0828 分钟前
python-leetcode 35.二叉树的中序遍历
算法·leetcode·职场和发展
篮l球场29 分钟前
LeetCodehot 力扣热题100
算法·leetcode·职场和发展
程序员张335 分钟前
使用IDEA提交SpringBoot项目到Gitee上
java·gitee·intellij-idea
sunnyday04261 小时前
MyBatis XML映射文件中的批量插入和更新
xml·java·mysql·mybatis
程序员阿鹏1 小时前
jdbc批量插入数据到MySQL
java·开发语言·数据库·mysql·intellij-idea
莲动渔舟1 小时前
国产编辑器EverEdit - 在编辑器中对文本进行排序
java·开发语言·编辑器
qy发大财1 小时前
分发糖果(力扣135)
数据结构·算法·leetcode