day31 贪心算法-mergeIntervals+monotoneIncreasingDigits+binaryTreeCameras

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.

  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(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;

}

}

}

```

相关推荐
IT小哥哥呀1 小时前
Nginx高可用配置实战:负载均衡 + 健康检查 + 动态扩展
运维·nginx·负载均衡·devops·日志分析·openresty·动态扩展
刘某的Cloud1 小时前
ceph osd down排查
linux·运维·ceph·系统·osd
喜欢你,还有大家5 小时前
Docker-仓库-镜像制作
运维·docker·容器
安审若无6 小时前
图数据库neoj4安装部署使用
linux·运维·数据库
做运维的阿瑞7 小时前
CentOS DNS故障排查完整解决方案:从症状到根因的系统化诊断
linux·运维·centos
天地之于壹炁兮7 小时前
编程I/O入门指南:核心操作全解析
数据库·windows·microsoft
QT 小鲜肉8 小时前
【个人成长笔记】在 Linux 系统下撰写老化测试脚本以实现自动压测效果(亲测有效)
linux·开发语言·笔记·单片机·压力测试
深圳市恒讯科技8 小时前
英国服务器Windows系统远程桌面安装与优化
运维·服务器·windows
itachi-uchiha8 小时前
head和tail命令使用
linux·运维·服务器
violet-lz8 小时前
Socket编程实战:从基础API到多线程服务器
运维·服务器