代码随想录算法训练营第三十天-452用最少数量的箭引爆气球、435无重叠区间、763划分字母区间
初步题解
452 用最少数量的箭引爆气球
题目链接:(https://leetcode.cn/problems/minimum-number-of-arrows-to-burst-balloons)
没有思路。
435 无重叠区间
题目链接:(https://leetcode.cn/problems/non-overlapping-intervals)
还是没有思路。
763 划分字母区间
题目链接:(https://leetcode.cn/problems/partition-labels/description/)
直接看的解析。
看解析
452 用最少数量的箭引爆气球
解析:(https://programmercarl.com/0452.用最少数量的箭引爆气球.html)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
public static int findMinArrowShots(int[][] points) { Arrays.sort(points, Comparator.comparingInt(o -> o[0])); if (points.length <= 1) { return points.length; } int count = 1; for (int i = 1; i < points.length; i++) { if (points[i][0] > points[i - 1][1]) { count++; } else { points[i][1] = Math.min(points[i][1], points[i - 1][1]); } } return count; }
|
435 无重叠区间
解析:(https://programmercarl.com/0435.无重叠区间.html)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
public static int eraseOverlapIntervals(int[][] intervals) { Arrays.sort(intervals, Comparator.comparingInt(o -> o[0])); int count = 0; if (intervals.length <= 1) { return count; } for (int i = 1; i < intervals.length; i++) { if (intervals[i][0] >= intervals[i - 1][1]){ } else { count++; intervals[i][1] = Math.min(intervals[i][1], intervals[i - 1][1]); } } return count; }
|
763 划分字母区间
解析:(https://programmercarl.com/0763.划分字母区间.html)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
public List<Integer> partitionLabels(String s) { char[] chars = s.toCharArray(); int[] ints = new int[26]; Arrays.fill(ints, -1); for (int i = 0; i < chars.length; i++) { ints[chars[i] - 'a'] = i; } List<Integer> list = new ArrayList<>(); int idx = 0; int last = -1; for (int i = 0; i < chars.length; i++) { idx = Math.max(idx, ints[chars[i] - 'a']); if (i == idx) { list.add(i - last); last = i; } } return list; }
|