Code random recording training camp day37| 738. Monotonically increasing numbers 968. Monitoring binary tree

@TOC


foreword

Code Random Record Algorithm Training Camp day37


1. Leetcode 968. Monitor Binary Tree v

1. Topic

Given a binary tree, we install cameras on the nodes of the tree.

Each camera on a node can monitor its parent, itself, and its immediate children.

Calculate the minimum number of cameras required to monitor all nodes of the tree.

Example 1:

Input: [0,0,null,0,0] Output: 1 Explanation: As shown in the figure, one camera is enough to monitor all nodes.

Example 2:

Input: [0,0,null,0,null,0,null,null,0] Output: 2 Explanation: At least two cameras are required to monitor all nodes of the tree. The image above shows one of the valid places for camera placement.

hint:

给定树的节点数的范围是 [1, 1000]。
每个节点的值都是 0。

Source: LeetCode Link: https://leetcode.cn/problems/binary-tree-cameras

2. Problem-solving ideas

Method 1: Dynamic Programming

Ideas and Algorithms

This question is based on a binary tree, and it is not difficult to think of solving it recursively. The difficulty of this question lies in how to deduce the state of the parent node from the state of the left and right subtrees.

For the convenience of expression, we agree that if all nodes of a tree are monitored, the tree is said to be "covered".

Assume that the current node is root, and its left and right children are left, rightleft, right. If you want to overwrite a tree rooted at root, there are two cases:

若在 rootroot 处安放摄像头,则孩子 left,rightleft,right 一定也会被监控到。此时,只需要保证 leftleft 的两棵子树被覆盖,同时保证 rightright 的两棵子树也被覆盖即可。
否则, 如果 rootroot 处不安放摄像头,则除了覆盖 rootroot 的两棵子树之外,孩子 left,rightleft,right 之一必须要安装摄像头,从而保证 rootroot 会被监控到。

According to the above discussion, it can be analyzed that for each node root, three types of states need to be maintained:

状态 aa:rootroot 必须放置摄像头的情况下,覆盖整棵树需要的摄像头数目。
状态 bb:覆盖整棵树需要的摄像头数目,无论 rootroot 是否放置摄像头。
状态 cc:覆盖两棵子树需要的摄像头数目,无论节点 rootroot 本身是否被监控到。

According to their definition, there must be a≥b≥ca≥b≥c.

For the node rootroot, let the state variables corresponding to its left, right left, and right children be (la,lb,lc)(la​,lb​,lc​) and (ra,rb,rc)(ra​, rb​,rc​). According to the discussion at the beginning, we have obtained the process of solving a,ba,b:

a=lc+rc+1a=lc​+rc​+1
b=min⁡(a,min⁡(la+rb,ra+lb))b=min(a,min(la​+rb​,ra​+lb​))

For cc, to ensure that the two subtrees are completely covered, either a camera is placed at the root, and the number of cameras required is aa; or no camera is placed at the root, at this time, the two subtrees are guaranteed to be covered respectively, and the number of cameras required It is lb+rblb​+rb​.

It should be noted that for root, if one of its children is empty, the current node cannot be monitored by placing a camera at the child. Therefore, the variable aa corresponding to this child should return a large integer, which is used to identify the impossible situation.

Finally, the state variable bb of the root node is the required answer.

3. Code implementation

```java class Solution { public int minCameraCover(TreeNode root) { int[] array = dfs(root); return array[1]; }

public int[] dfs(TreeNode root) {
    if (root == null) {
        return new int[]{Integer.MAX_VALUE / 2, 0, 0};
    }
    int[] leftArray = dfs(root.left);
    int[] rightArray = dfs(root.right);
    int[] array = new int[3];
    array[0] = leftArray[2] + rightArray[2] + 1;
    array[1] = Math.min(array[0], Math.min(leftArray[0] + rightArray[1], rightArray[0] + leftArray[1]));
    array[2] = Math.min(array[0], leftArray[1] + rightArray[1]);
    return array;
}

}

```

2. Leetcode 738. Monotonically increasing numbers

1. Topic

An integer is said to be monotonically increasing if and only if the number x and y in each adjacent digit satisfies x <= y.

Given an integer n, returns the largest number less than or equal to n that increases monotonically.

Example 1:

Input: n = 10 Output: 9

Example 2:

Input: n = 1234 Output: 1234

Example 3:

Input: n = 332 Output: 299

hint:

0 <= n <= 109

Source: LeetCode Link: https://leetcode.cn/problems/monotone-increasing-digits

2. Problem-solving ideas

Method One: Greedy

We can construct this largest monotonically increasing number less than or equal to nn bitwise from high to low. Assuming that the limitation of nn is not considered, then for a number with a length of kk, the largest monotonically increasing number must be a number with 99 digits.

Note that strN[i]strN[i] represents the ii-th digit of the number nn from high to low (ii starts from 00).

If the whole number nn itself is already bitwise monotonically increasing, then the largest number is nn.

If the first position ii is found such that the digits of [0,i−1][0,i−1] are monotonically increasing and strN[i−1]>strN[i]strN[i−1]>strN[i], At this time, the digits of [0,i][0,i] are equal to the corresponding digits of nn, and are still restricted by nn, that is, we cannot fill in [i+1,k−1][i+1,k−1 at will. ] in place of a number. In order to get the largest number, we need to remove the restriction of nn, so that all the remaining low bits become 99, that is, we can get the largest integer smaller than nn. From a greedy point of view, we need to try to make the high digits equal to the corresponding digits of nn, so try to subtract 11 from the digits of strN[i−1]strN[i−1]. At this time, it is no longer restricted by nn, and it is enough to directly change all the numbers at the positions of [i,k−1][i,k−1] to 99.

But there is a problem here: when strN[i−1]strN[i−1] is subtracted by 11, it may make strN[i−1]strN[i−1] and strN[i−2]strN[i−1] 2] The incremental relationship is no longer satisfied, so we need to decrementally compare the relationship between adjacent digits from i−1i−1 until we find the first position jj so that strN[j]strN[j] reduces its own digit by 11 and then strN[ j−1]strN[j−1] and strN[j]strN[j] still maintain an increasing relationship, or the position jj has reached the leftmost (that is, the value of jj is 00), at this time we will [j+1,k −1][j+1,k−1] all become 99 to get the final correct answer.

3. Code implementation

```java class Solution { public int monotoneIncreasingDigits(int n) { char[] strN = Integer.toString(n).toCharArray(); int i = 1; while (i < strN.length && strN[i - 1] <= strN[i]) { i += 1; } if (i < strN.length) { while (i > 0 && strN[i - 1] > strN[i]) { strN[i - 1] -= 1; i -= 1; } for (i += 1; i < strN.length; ++i) { strN[i] = '9'; } } return Integer.parseInt(new String(strN)); } }

```

Guess you like

Origin blog.csdn.net/HHX_01/article/details/131285415