modify code

master
algorithmzuo 2 years ago
parent 8211929803
commit 5ca934ef7d

@ -1,87 +0,0 @@
package 03.mca_04;
// 给定一个二叉树找到其中最大的二叉搜索树BST子树并返回该子树的大小
// 其中,最大指的是子树节点数最多的
// 二叉搜索树BST中的所有节点都具备以下属性
// 左子树的值小于其父(根)节点的值
// 右子树的值大于其父(根)节点的值
// 注意:子树必须包含其所有后代
// 测试链接 : https://leetcode.cn/problems/largest-bst-subtree
public class Code03_MaxSubBSTSize {
// 提交时不要提交这个类
public static class TreeNode {
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int value) {
val = value;
}
}
// 提交如下方法
public static int largestBSTSubtree(TreeNode head) {
if (head == null) {
return 0;
}
return process(head).maxBSTSubtreeSize;
}
public static class Info {
public int maxBSTSubtreeSize;
public int allSize;
public int max;
public int min;
public Info(int m, int a, int ma, int mi) {
maxBSTSubtreeSize = m;
allSize = a;
max = ma;
min = mi;
}
}
public static Info process(TreeNode x) {
if (x == null) {
return null;
}
Info leftInfo = process(x.left);
Info rightInfo = process(x.right);
int max = x.val;
int min = x.val;
int allSize = 1;
if (leftInfo != null) {
max = Math.max(leftInfo.max, max);
min = Math.min(leftInfo.min, min);
allSize += leftInfo.allSize;
}
if (rightInfo != null) {
max = Math.max(rightInfo.max, max);
min = Math.min(rightInfo.min, min);
allSize += rightInfo.allSize;
}
int p1 = -1;
if (leftInfo != null) {
p1 = leftInfo.maxBSTSubtreeSize;
}
int p2 = -1;
if (rightInfo != null) {
p2 = rightInfo.maxBSTSubtreeSize;
}
int p3 = -1;
boolean leftBST = leftInfo == null ? true : (leftInfo.maxBSTSubtreeSize == leftInfo.allSize);
boolean rightBST = rightInfo == null ? true : (rightInfo.maxBSTSubtreeSize == rightInfo.allSize);
if (leftBST && rightBST) {
boolean leftMaxLessX = leftInfo == null ? true : (leftInfo.max < x.val);
boolean rightMinMoreX = rightInfo == null ? true : (x.val < rightInfo.min);
if (leftMaxLessX && rightMinMoreX) {
int leftSize = leftInfo == null ? 0 : leftInfo.allSize;
int rightSize = rightInfo == null ? 0 : rightInfo.allSize;
p3 = leftSize + rightSize + 1;
}
}
return new Info(Math.max(p1, Math.max(p2, p3)), allSize, max, min);
}
}

@ -13,7 +13,7 @@ import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
public class Code04_UnionFind {
public class Code03_UnionFind {
public static int MAXN = 1000001;

@ -1,9 +1,17 @@
package 03.mca_04;
// 本题为leetcode原题
// 有 n 个城市,其中一些彼此相连,另一些没有相连
// 如果城市 a 与城市 b 直接相连
// 且城市 b 与城市 c 直接相连
// 那么城市 a 与城市 c 间接相连
// 省份 是一组直接或间接相连的城市
// 组内不含其他没有相连的城市。
// 给你一个 n x n 的矩阵 isConnected
// 其中 isConnected[i][j] = 1 表示第 i 个城市和第 j 个城市直接相连
// 而 isConnected[i][j] = 0 表示二者不直接相连。
// 返回矩阵中 省份 的数量。
// 测试链接https://leetcode.cn/problems/friend-circles/
// 可以直接通过
public class Code05_FriendCircles {
public class Code04_FriendCircles {
public static int findCircleNum(int[][] M) {
int N = M.length;

@ -6,14 +6,10 @@ package 第03期.mca_04;
// 返回 最少交换座位的次数,以便每对情侣可以并肩坐在一起
// 每次交换可选择任意两人,让他们站起来交换座位
// 测试链接 : https://leetcode.cn/problems/couples-holding-hands/
public class Code06_CouplesHoldingHands {
public class Code05_CouplesHoldingHands {
public int minSwapsCouples(int[] row) {
// n人数偶数
int n = row.length;
// n/2
// 0 1 -> 0 0
// 4 5 -> 2 2
UnionFind uf = new UnionFind(n / 2);
for (int i = 0; i < n; i += 2) {
uf.union(row[i] / 2, row[i + 1] / 2);

@ -1,7 +1,7 @@
package 03.mca_04;
// 本题测试链接 : https://leetcode.cn/problems/longest-increasing-subsequence
public class Code07_LIS {
public class Code06_LIS {
public static int lengthOfLIS(int[] arr) {
if (arr == null || arr.length == 0) {

@ -2,8 +2,8 @@ package 第03期.mca_04;
import java.util.HashMap;
// 本题测试链接 : https://leetcode.cn/problems/lru-cache/
public class Code08_LRUCache {
// 测试链接 : https://leetcode.cn/problems/lru-cache/
public class Code07_LRUCache {
// 提交以下这个类
public class LRUCache {
Loading…
Cancel
Save