【陪伴式刷题】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),额外常数空间
相关推荐
碎叶城李白5 分钟前
若依学习笔记1-validated
java·笔记·学习·validated
Kaltistss31 分钟前
98.验证二叉搜索树
算法·leetcode·职场和发展
都叫我大帅哥32 分钟前
🌊 Redis Stream深度探险:从秒杀系统到面试通关
java·redis
都叫我大帅哥33 分钟前
Redis持久化全解析:从健忘症患者到记忆大师的逆袭
java·redis
程序猿阿越1 小时前
Kafka源码(一)Controller选举与创建Topic
java·后端·源码
程序无bug1 小时前
Spring6 当中 Bean 的生命周期的详细解析:有五步,有七步,有十步
java
二川bro1 小时前
飞算智造JavaAI:智能编程革命——AI重构Java开发新范式
java·人工智能·重构
Q_970956391 小时前
java+vue+SpringBoo校园失物招领网站(程序+数据库+报告+部署教程+答辩指导)
java·数据库·vue.js
Wyc724091 小时前
Maven
java·数据库·maven