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;

}

}

}

```

相关推荐
向往风的男子7 分钟前
【devops】devops-gitlab之部署与日常使用
运维·gitlab·devops
GDAL1 小时前
GNU力量注入Windows:打造高效跨平台开发新纪元
服务器·windows·gnu
Dola_Pan2 小时前
Linux文件IO(一)-open使用详解
java·linux·dubbo
Spring-wind2 小时前
【linux】pwd命令
linux
geekrabbit2 小时前
机器学习和深度学习的区别
运维·人工智能·深度学习·机器学习·浪浪云
ken_coding3 小时前
Windows11 WSL2的ubuntu 22.04中拉取镜像报错
linux·ubuntu·docker
阳光开朗_大男孩儿3 小时前
DBUS属性原理
linux·服务器·前端·数据库·qt
楠神说软件测试3 小时前
接口自动化框架入门(requests+pytest)
运维·数据库·自动化
gopher95113 小时前
linux驱动开发-设备树
linux·驱动开发
学习3人组3 小时前
克隆centos网卡uuid相同如何修改
linux·运维·centos