【陪伴式刷题】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),额外常数空间
相关推荐
不知道累,只知道类7 小时前
Java 在AWS上使用SDK凭证获取顺序
java·aws
sprintzer7 小时前
10.6-10.15力扣模拟刷题
算法·leetcode·职场和发展
咖啡Beans7 小时前
SpringBoot2.7集成Swagger3.0
java·swagger
聪明的笨猪猪7 小时前
Java JVM “垃圾回收(GC)”面试清单(含超通俗生活案例与深度理解)
java·经验分享·笔记·面试
Moniane8 小时前
时序数据库全面重构指南
java·后端·struts
一匹电信狗8 小时前
【C++】C++风格的类型转换
服务器·开发语言·c++·leetcode·小程序·stl·visual studio
whm27778 小时前
Visual Basic 值传递与地址传递
java·开发语言·数据结构
没有bug.的程序员8 小时前
云原生与分布式架构的完美融合:从理论到生产实践
java·分布式·微服务·云原生·架构
村口张大爷9 小时前
Spring Boot 初始化钩子
java·spring boot·后端
x_feng_x9 小时前
Java从入门到精通 - 集合框架(二)
java·开发语言·windows