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;

}

}

}

```

相关推荐
Dawn-bit5 分钟前
Linux打包压缩与用户权限管理详解
linux·服务器·数据库
ls_elect6 分钟前
PostgreSQL 18.4 SCRAM-SHA-256 认证故障诊断及修复办法
linux·数据库·postgresql
段一凡-华北理工大学28 分钟前
AI Agent 从入门到封神:24 讲打造你的超级智能体~系列文章23:从Demo到上线:Agent应用的架构设计、性能优化与成本控制实战
运维·网络·人工智能·性能优化·高炉炼铁·工业智能体
L16247637 分钟前
Zabbix 7.0 LTS 完整部署与运维手册(AlmaLinux 9 + MySQL 8.0 + Nginx)
运维·mysql·zabbix
happymade38 分钟前
MSRM3 区域层级嵌套功能深度解析与实操教程
运维·网络拓扑·网络管理·网络运维·msrm3
陈同学xxx1 小时前
Hermes Agent 接入飞书,在手机上随时聊天
linux·python·飞书
网络小白不怕黑1 小时前
14.VSFTP服务相关配置
linux·运维·服务器
是摆烂第一名呀1 小时前
RK3576+GMSL摄像头底层适配与调试
linux·arm开发·驱动开发·嵌入式硬件·数码相机
爱喝水的鱼丶1 小时前
SAP-ABAP:ALV字段布局定制全攻略——快速适配业务端个性化展示需求
运维·性能优化·sap·abap·经验交流·alv报表
qetfw2 小时前
CentOS 7 配置 firewalld 防火墙
linux·运维·centos