【陪伴式刷题】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),额外常数空间
相关推荐
API小爬虫5 分钟前
淘宝按图搜索商品(拍立淘)Java 爬虫实战指南
java·爬虫·图搜索算法
lyrhhhhhhhh14 分钟前
Spring 框架 JDBC 模板技术详解
java·数据库·spring
亚林瓜子1 小时前
AWS Elastic Beanstalk控制台部署Spring极简工程
java·spring·云计算·aws·eb
2401_cf1 小时前
如何创建maven项目
java·maven·intellij-idea
软行2 小时前
LeetCode 每日一题 3341. 到达最后一个房间的最少时间 I + II
数据结构·c++·算法·leetcode·职场和发展
How_doyou_do2 小时前
备战菊厂笔试4
python·算法·leetcode
C4程序员3 小时前
Java百度身份证识别接口实现【配置即用】
java·开发语言
炒空心菜菜3 小时前
MapReduce 实现 WordCount
java·开发语言·ide·后端·spark·eclipse·mapreduce
zy happy3 小时前
搭建运行若依微服务版本ruoyi-cloud最新教程
java·spring boot·spring cloud·微服务·ruoyi
芯眼3 小时前
STM32启动文件详解(重点)
java·开发语言·c++·stm32·单片机·mybatis