8.16 56. Merge Intervals
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.
- 合并区间
本题也是重叠区间问题,如果昨天三道都吸收的话,本题就容易理解了。
https://leetcode.cn/problems/merge-intervals/description/
https://programmercarl.com/0056.合并区间.html
返回值是长度不一定的二维数组?不要直接new二维数组,先将一维数组存入list,再toArray即可。
这里是合并区间,需要将左右边界都保存下来。
```java
public class mergeIntervals {
public int[][] merge(int[][] intervals) {
List<int[]> result = new ArrayList<>();
Arrays.sort(intervals,(a,b) -> Integer.compare(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){
right = Math.max(intervals[i][1],right);
}else{
result.add(new int[]{left,right});
left = intervals[i][0];
right = intervals[i][1];
}
}
result.add(new int[]{left,right});
return result.toArray(new int[result.size()][]);
}
}
class mergeIntervalsTest {
public static void main(String[] args) {
mergeIntervals example = new mergeIntervals();
int[][] intervals = {
//{1,3},{2,6},{8,10},{15,18}
{2,3},{4,5},{6,7},{8,9},{1,10}
//{-2147483646,-2147483645},{2147483646,2147483647}
};
int[][] result = example.merge(intervals);
for(int[] line : result){
System.out.print("[ ");
for(int i : line){
System.out.print(i + " ,");
}
System.out.print(" ] ");
}
}
}
```
8.17 738. Monotone Increasing Digits
An integer has monotone increasing digits if and only if each pair of adjacent digits x and y satisfy x <= y.
Given an integer n, return the largest number that is less than or equal to n with monotone increasing digits.
https://leetcode.cn/problems/monotone-increasing-digits/description/
https://programmercarl.com/0738.单调递增的数字.html
这题的巧妙之处在于,找到第一个非单调递增的数字后,将它-1,后面的数字都改为9.
```java
public class monotoneIncreasingDigits {
public monotoneIncreasingDigits() { }
public int monotoneIncreasingDigits(int n) {
String s = String.valueOf(n);
char[] chars = s.toCharArray();
//用于记录末尾的9从哪里开始
int start = chars.length;
for (int i = chars.length - 1; i > 0; i--) {
if(chars[i-1] > chars[i]){
chars[i-1]--;
start = i;
}
}
// for (int i = chars.length - 2; i >= 0; i--) {
// if(chars[i+1] < chars[i]){
// chars[i]--;
// start = i + 1;
// }
// } //没有区别,写法不一样
// System.out.println("start: " + start);
for (int i = start; i < chars.length; i++) {
chars[i] = '9';
}
return Integer.parseInt(new String(chars));
}
}
class monotoneIncreasingDigitsTest {
public static void main(String[] args) {
monotoneIncreasingDigits example = new monotoneIncreasingDigits();
System.out.println(example.monotoneIncreasingDigits(3452331));
}
}
```
8.18 968. Binary Tree Cameras(选做)
You are given the root of a binary tree. We install cameras on the tree nodes where each camera at a node can monitor its parent, itself, and its immediate children.
Return the minimum number of cameras needed to monitor all nodes of the tree.
https://leetcode.cn/problems/binary-tree-cameras/description/
https://programmercarl.com/0968.监控二叉树.html
碰到叶子节点,就让它的父节点放一个摄像头;向上遍历的时候每隔两个节点放一个摄像头。
如何实现?通过记录状态
三个状态:
0.无覆盖(没有被摄像头覆盖)
1.有摄像头
2.有覆盖
无摄像头的状态可以被0和1替代。
目的:尽可能地让叶子的父节点放摄像头。
三种情况:
1.左右孩子都有覆盖(2),父节点则无覆盖。(叶子节点返回2)
2.左右孩子有一个无覆盖,父节点需要装摄像头
3.左右孩子有一个有摄像头,父节点有覆盖。
4.遍历结束后,头结点没有覆盖,需要加一个摄像头
```java
public class binaryTreeCameras {
int result = 0;
public int minCameraCover(TreeNode root) {
//当头节点没有被覆盖,需要将一个头布置在头结点上。
if(traversal(root) == 0) result++;
return result;
}
public int traversal(TreeNode root){
//根节点不布置摄像头,直接返回有覆盖
if(root == null){
return 2;
}
int left = traversal(root.left);
int right = traversal(root.right);
// 如果左右节点都覆盖了的话, 那么本节点的状态就应该是无覆盖,没有摄像头
if(left == 2 && right == 2){
return 0;
//左右节点存在无覆盖状态,那 根节点此时应该放一个摄像头
}else if(left==0||right==0){
result++;
return 1;
}else{
//左右节点至少存在一个摄像头
return 2;
}
}
}
```