LC.P630[课程表III]
LC.P630[课程表III]
方法一:贪心+优先队列1234567891011121314class Solution { public int scheduleCourse(int[][] courses) { Arrays.sort(courses, (a, b) -> a[1] - b[1]); PriorityQueue<Integer> q = new PriorityQueue<>((a, b) -> b - a); int sum = 0; for (int[] course : courses) { int duration = course[0], lastDay = course[1]; sum += duration; q.offer(duration); if (sum > lastDay) sum -= q.poll(); & ...
LC.P210[课程表II]
LC.P210[课程表II]
方法一:拓扑排序123456789101112131415161718192021222324252627class Solution { public int[] findOrder(int numCourses, int[][] prerequisites) { List<Integer>[] g = new List[numCourses]; Arrays.setAll(g, k -> new ArrayList<>()); int[] in = new int[numCourses]; for (int[] p : prerequisites) { int a = p[0], b = p[1]; g[b].add(a); ++in[a]; } Deque<Integer> queue = new ArrayDequ ...
LC.P207[课程表]
LC.P207[课程表]
方法一:拓扑排序123456789101112131415161718192021222324class Solution { public boolean canFinish(int numCourses, int[][] prerequisites) { List<Integer>[] g = new List[numCourses]; Arrays.setAll(g, k -> new ArrayList<>()); int[] in = new int[numCourses]; for (int[] p : prerequisites) { int a = p[0], b = p[1]; g[b].add(a); ++in[a]; } Deque<Integer> queue = new ArrayDeque<& ...
LC.P99[恢复二叉搜索树]
LC.P99[恢复二叉搜索树]
方法一:DFS123456789101112131415161718192021222324252627282930313233343536373839404142/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */class Solution ...
LC.P2651[计算列车到站时间]
LC.P2651[计算列车到站时间]
方法一:数学12345class Solution { public int findDelayedArrivalTime(int arrivalTime, int delayedTime) { return (arrivalTime + delayedTime) % 24; }}
时间复杂度:$O(1)$
空间复杂度:$O(1)$
LC.P897[递增顺序搜索树]
LC.P897[递增顺序搜索树]
方法一:DFS12345678910111213141516171819202122232425262728293031323334/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */class Solution { public T ...
LC.P2594[修车的最少时间]
LC.P2594[修车的最少时间]
方法一:二分查找123456789101112131415class Solution { public long repairCars(int[] ranks, int cars) { long left = 0, right = (long) ranks[0] * cars * cars; while (left < right) { long mid = left + right >> 1; long cnt = 0; for (int r : ranks) { cnt += Math.sqrt(mid / r); } if (cnt >= cars) right = mid; else left = mid + 1; } return le ...
LC.P1719[重构一棵树的方案数]
LC.P1719[重构一棵树的方案数]
方法一:模拟+构造1234567891011121314151617181920212223242526272829303132333435363738394041424344454647class Solution { int N = 510; int[] cnts = new int[N], fa = new int[N]; boolean[][] g = new boolean[N][N]; public int checkWays(int[][] pairs) { int m = pairs.length; Set<Integer> set = new HashSet<>(); for (int[] p : pairs) { int a = p[0], b = p[1]; g[a][b] = g[b][a] = true; ++cnts[a]; ...
LC.P1123[最深叶节点的最近公共祖先]
LC.P1123[最深叶节点的最近公共祖先]
方法一:DFS1234567891011121314151617181920212223242526272829303132333435363738/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */class Solution ...
LC.P687[最长同值路径]
LC.P687[最长同值路径]
方法一:DFS12345678910111213141516171819202122232425262728293031323334/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */class Solution { int a ...