day31 贪心算法-mergeIntervals+monotoneIncreasingDigits+binaryTreeCameras

8.16 56. Merge Intervals

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

  1. 合并区间

本题也是重叠区间问题,如果昨天三道都吸收的话,本题就容易理解了。

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(a0,b0));

int left = intervals00;

int right = intervals01;

for (int i = 1; i < intervals.length; i++) {

//这里其实是将当前左边界和上一组的右边界进行比对,并更新右边界。

if(intervalsi0 <= right){

right = Math.max(intervalsi1,right);

}else{

result.add(new int\[\]{left,right});

left = intervalsi0;

right = intervalsi1;

}

}

result.add(new int\[\]{left,right});

return result.toArray(new intresult.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(charsi-1 > charsi){

charsi-1--;

start = i;

}

}

// for (int i = chars.length - 2; i >= 0; i--) {

// if(charsi+1 < charsi){

// charsi--;

// start = i + 1;

// }

// } //没有区别,写法不一样

// System.out.println("start: " + start);

for (int i = start; i < chars.length; i++) {

charsi = '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;

}

}

}

```

相关推荐
minji...5 分钟前
Linux 高级IO(四)多路转接之epoll,epoll 模型及原理
linux·运维·服务器·多路转接·epoll·epoll模型·红黑树/就绪队列/回调
蜡笔婧萱6 分钟前
网络服务综合大实验--包含NFS服务器,Web服务器,DNS域名服务器
linux·服务器·网络
bitbrowser11 分钟前
2026年Facebook广告账户频频“连坐”被封?聊聊出海投流
运维·服务器·facebook
汽车仪器仪表相关领域13 分钟前
Kvaser Hybrid CAN/LIN 单通道三合一总线分析仪:高性价比CAN FD/LIN集成测试利器
运维·服务器·网络·数据挖掘·数据分析·单元测试·集成测试
林熙蕾LXL13 分钟前
守护进程&IO多路复用介绍
linux·服务器·网络
idolao13 分钟前
ChemSketch 10安装教程 Windows版:自定义路径+轻量看图软件指南
windows
志栋智能13 分钟前
超自动化安全:实现安全运营现代化的关键
大数据·运维·网络·安全·自动化
mounter62528 分钟前
技术前沿:在内核实时更新(Live Update)期间保留 hugetlbfs 内存
linux·linux kernel·kernel·kexec
zzipeng44 分钟前
Linux 并发与竞争
java·linux·运维
福大大架构师每日一题44 分钟前
YOLO v8.4.56 修复 QNN 导出兼容性:builtin provider wheels 也能稳定导出,Linux x86-64 更友好
linux·运维·yolo