【代码随想录训练营】Day36-贪心算法

代码随想录训练营 Day36

今日任务

435.无重叠区间
763.划分字母区间
56.合并区间
语言:Java

435. 无重叠区间

链接:https://leetcode.cn/problems/non-overlapping-intervals/
为了保证删除最少的区间,那么就要尽可能多地保留区间,也就是说要让占据空间太多的区间首先被移除

class Solution {
    public int eraseOverlapIntervals(int[][] intervals) {
        int  count = 0;
        Arrays.sort(intervals, (a, b)->{
            return a[0] - b[0];
        });
        int prev = 0;
        for(int cur = 1; cur < intervals.length; cur++){
            //overlapping
            if(intervals[prev][1] > intervals[cur][0]){
                count++;
                //[1,5][3,6] delete[3,6]
                if(intervals[prev][1] < intervals[cur][1]){
                    //nothing need to do
                }
                //[1,5][2,4] delete[1,5]
                else{
                    prev = cur;
                }
            }
            //non-overlapping
            else{
                prev = cur;
            }
        }
        return count;
    }
}

763. 划分字母区间

链接:https://leetcode.cn/problems/partition-labels/
有些复杂,不需要记录两边,只记录一遍即可

class Solution {
    public List<Integer> partitionLabels(String s) {
        List<Integer> ret = new ArrayList<Integer>();
        int[][] record = new int[26][2];
        for(int i = 0; i < 26; i++){
            record[i][0] = 501;
            record[i][1] = 501;
        }
        for(int i = 0; i < s.length(); i++){
            int cur = s.charAt(i) - 'a';
            if(record[cur][0] == 501 && record[cur][1] == 501){
                record[cur][0] = i;
                record[cur][1] = i;
            }
            else if(record[cur][0] != 501){
                record[cur][1] = i;
            }
        }
        //起始/终止位置不会有相同的情况
        Arrays.sort(record, (a, b)->{
            return a[0] - b[0];
        });
        int start = record[0][0];
        int end = record[0][1];
        for(int i = 1; i < 26; i++){
            if(record[i][0] == 501){
                break;
            }
            if(record[i][0] < end){
                start = Math.min(record[i][0], start);
                end = Math.max(record[i][1], end);
            }
            else if(record[i][0] > end){
                ret.add(end - start + 1);
                start = record[i][0];
                end = record[i][1];
            }
        }
        ret.add(end - start + 1); //要在这里add,不可以在break那里add
        //因为退出循环的原因不一定是break
        return ret;
    }
}
class Solution {
    public List<Integer> partitionLabels(String s) {
        int[] record = new int[26]; //记录最右边界
        for(int i = 0; i < s.length(); i++){
            record[s.charAt(i) - 'a'] = i;
        }
        int left = 0;
        int right = 0;
        List<Integer> ret = new ArrayList<>();
        for(int i = 0; i < s.length(); i++){
            right = Math.max(right, record[s.charAt(i) - 'a']);
            if(i == right){
                ret.add(right - left + 1);
                left = i + 1;
            }
        }
        return ret;
    }
}

56. 合并区间

链接:https://leetcode.cn/problems/merge-intervals/

class Solution {
    public int[][] merge(int[][] intervals) {
        List<int[]> res = new ArrayList<>();
        Arrays.sort(intervals, (a, b)->{
            return a[0] - b[0];
        });
        int left = intervals[0][0];
        int right = intervals[0][1];
        for(int i = 1; i < intervals.length; i++){
            if(intervals[i][0] <= right){
                left = Math.min(intervals[i][0], left);
                right = Math.max(intervals[i][1], right);
            }
            else{
                int[] temp = new int[2];
                temp[0] = left;
                temp[1] = right;
                res.add(temp);
                left = intervals[i][0];
                right = intervals[i][1];
            }
        }
        int[] temp = new int[2];
        temp[0] = left;
        temp[1] = right;
        res.add(temp);
        int[][] arr = new int[res.size()][2];
        return res.toArray(arr);
    }
}
class Solution {
    public int[][] merge(int[][] intervals) {
        List<int[]> res = new ArrayList<>();
        //已经按照左边的值进行排序了,那么就不用记录左侧的值了
        Arrays.sort(intervals, (a, b)->{
            return a[0] - b[0];
        });
        res.add(intervals[0]);
        for(int i = 1; i < intervals.length; i++){
            if(intervals[i][0] <= res.get(res.size() - 1)[1]){
                res.get(res.size() - 1)[1] = Math.max(intervals[i][1], res.get(res.size() - 1)[1]);
            }
            else{
                res.add(intervals[i]);
            }
        }
        int[][] arr = new int[res.size()][2];
        return res.toArray(arr);
    }
}

你可能感兴趣的:(代码随想录训练营,贪心算法,leetcode,java)