LeetCode | 435. Non-overlapping Intervals, 763. Partition Labels, 56. Merge Intervals

文章讨论了如何解决LeetCode中的三个问题,涉及合并重叠区间(Non-overlappingIntervals,PartitionLabels,MergeIntervals),通过排序和比较操作合并并减少重叠区间,返回覆盖所有输入区间的非重叠区间数组。
摘要由CSDN通过智能技术生成

435. Non-overlapping Intervals

Link: https://leetcode.com/problems/non-overlapping-intervals/

Description

Given an array of intervals intervals where intervals[i] = [starti, endi], return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.

Approach

  • Sort the intervals in ascending order accroding to the start.
  • Initialize cnt to store the intervals needed to be removed.
  • Loop through the sorted intervals from the second interval:
    • If the start of the current interval smaller than the end of the previous interval, increment cnt and save the smller value of end of the two intervals to the end of the current interval.
  • Return cnt.

Solution

class Solution {
    public int eraseOverlapIntervals(int[][] intervals) {
        Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0]));
        int cnt = 0;
        for (int i = 1; i < intervals.length; i++) {
            if (intervals[i][0] < intervals[i - 1][1]) {
                cnt++;
                intervals[i][1] = Math.min(intervals[i][1], intervals[i - 1][1]);
            } 
        }
        return cnt;
    }
}

763. Partition Labels

Link: https://leetcode.com/problems/partition-labels/

Description

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.

Approach

  • Loop throught s to find the last appearance of each letter and save them to edge array.
  • Initialize left and right as 0.
  • Loop through s from the first character:
    • Save the maximum value between right and the the value of edge of the current index to right.
    • If right equals to the current index, save right - left + 1 to the result list and change left to the next index of the current index.

Solution

class Solution {
    public List<Integer> partitionLabels(String s) {
        List<Integer> list = new ArrayList<>();
        int[] edge = new int[26];
        char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            edge[chars[i] - 'a'] = i;
        }
        int right = 0;
        int left = 0;
        for (int i = 0 ; i < chars.length; i++) {
            right = Math.max(right, edge[chars[i] - 'a']);
            if (right == i) {
                list.add(right - left + 1);
                left = right + 1;
            }
        }
        return list;
    }
}

56. Merge Intervals

Link: https://leetcode.com/problems/merge-intervals/

Description

Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input.

Approach

  • Sort the intervals in ascending order accroding to the start.
  • Initialize a list to store the intervals and add the first element to the list. Save the first element as last (used to save the ).
  • Loop through the sorted intervals from the second interval:
    • If the start of the current interval larger than the end of last. Save it to the list and last.
    • Else, compare the start of last and the start of the current interval, save the smaller to one to last. Then compare the end of last and the end of the current interval, save the larger one to last.
  • Return the array of list.

Solution

class Solution {
    public int[][] merge(int[][] intervals) {
        Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0]));
        List<int[]> list = new ArrayList<>();
        list.add(intervals[0]);
        int[] last = intervals[0];
        for (int i = 1; i < intervals.length; i++) {
            if (intervals[i][0] > last[1]) {
                list.add(intervals[i]);
                last = intervals[i];
            }
            else {
                list.remove(list.size() - 1);
                last[0] = last[0] < intervals[i][0] ? last[0] : intervals[i][0];
                last[1] = last[1] > intervals[i][1] ? last[1] : intervals[i][1];
                list.add(last);
            }
        }
        return list.toArray(new int[list.size()][]);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值