diff --git a/Leecode/src/main/java/com/markilue/leecode/hot100/interviewHot/difference/LC_1094_CarPooling.java b/Leecode/src/main/java/com/markilue/leecode/hot100/interviewHot/difference/LC_1094_CarPooling.java index ff55468..9c01fcb 100644 --- a/Leecode/src/main/java/com/markilue/leecode/hot100/interviewHot/difference/LC_1094_CarPooling.java +++ b/Leecode/src/main/java/com/markilue/leecode/hot100/interviewHot/difference/LC_1094_CarPooling.java @@ -114,4 +114,28 @@ public class LC_1094_CarPooling { } + + + //三刷:本质上还是通过差分数组计算每一个位置的数量,然后通过判断来确定可不可以 + public boolean carPooling2(int[][] trips, int capacity) { + + int[] diff = new int[1000];//不在找最小最大来确定差分的范围,而是根据题意来直接确定大小 + int min = 1005; + int max = -1; + for (int[] trip : trips) { + if (trip[1] < min) min = trip[1]; + if (trip[2] > max) max = trip[2]; + diff[trip[1]] += trip[0]; + diff[trip[2]] -= trip[0];//至于为什么不加1,是题意决定的 + } + + //统计每一个位置的数量 + if (diff[min] > capacity) return false; + for (int i = min + 1; i < max; i++) { + diff[i] += diff[i - 1]; + if (diff[i] > capacity) return false; + } + return true; + + } } diff --git a/Leecode/src/main/java/com/markilue/leecode/hot100/interviewHot/difference/LC_1109_CorpFlightBookings.java b/Leecode/src/main/java/com/markilue/leecode/hot100/interviewHot/difference/LC_1109_CorpFlightBookings.java index 4856791..d6f6497 100644 --- a/Leecode/src/main/java/com/markilue/leecode/hot100/interviewHot/difference/LC_1109_CorpFlightBookings.java +++ b/Leecode/src/main/java/com/markilue/leecode/hot100/interviewHot/difference/LC_1109_CorpFlightBookings.java @@ -131,4 +131,26 @@ public class LC_1109_CorpFlightBookings { } + + //三刷:差分 + public int[] corpFlightBookings4(int[][] bookings, int n) { + + //构建差分数组 + int[] diff = new int[n]; + + for (int[] booking : bookings) { + diff[booking[0] - 1] += booking[2]; + if (booking[1] < diff.length) { + diff[booking[1]] -= booking[2]; + } + } + + //通过差分数组还原每一个位置 + for (int i = 1; i < diff.length; i++) { + diff[i] += diff[i - 1]; + } + + return diff; + + } } diff --git a/Leecode/src/main/java/com/markilue/leecode/hot100/interviewHot/listnode/LC_143_ReorderList.java b/Leecode/src/main/java/com/markilue/leecode/hot100/interviewHot/listnode/LC_143_ReorderList.java index 0bb5781..4c5c9f5 100644 --- a/Leecode/src/main/java/com/markilue/leecode/hot100/interviewHot/listnode/LC_143_ReorderList.java +++ b/Leecode/src/main/java/com/markilue/leecode/hot100/interviewHot/listnode/LC_143_ReorderList.java @@ -38,4 +38,28 @@ public class LC_143_ReorderList { p.next = cur; p = p.next.next;//移动到下一个需要操作的位置 } + + + //二刷:本质上就是寻找循环不变量 + public void reorderList1(ListNode head) { + p = head; + recur1(head); + } + + public void recur1(ListNode node) { + if (node == null) { + return; + } + + recur1(node); + + if (p.next == null || p == node || node.next == p) { + p.next = null;//重复了,不在遍历 + return; + } + + node.next = p.next; + p.next = node; + p = p.next.next; + } } diff --git a/Leecode/src/main/java/com/markilue/leecode/hot100/interviewHot/listnode/LC_25_ReverseKGroup.java b/Leecode/src/main/java/com/markilue/leecode/hot100/interviewHot/listnode/LC_25_ReverseKGroup.java index 8773a31..978c69d 100644 --- a/Leecode/src/main/java/com/markilue/leecode/hot100/interviewHot/listnode/LC_25_ReverseKGroup.java +++ b/Leecode/src/main/java/com/markilue/leecode/hot100/interviewHot/listnode/LC_25_ReverseKGroup.java @@ -17,7 +17,7 @@ public class LC_25_ReverseKGroup { @Test public void test() { ListNode root = ListNodeUtils.build(new int[]{1, 2, 3, 4, 5}); - ListNodeUtils.print(reverseKGroup(root,2)); + ListNodeUtils.print(reverseKGroup(root, 2)); } @@ -45,7 +45,7 @@ public class LC_25_ReverseKGroup { if (start == null) return start; ListNode fake = new ListNode(); - ListNode temp =start; + ListNode temp = start; ListNode tempNext; while (temp != end) { tempNext = temp.next; @@ -57,4 +57,49 @@ public class LC_25_ReverseKGroup { return fake.next; } + + //二刷: + public ListNode reverseKGroup1(ListNode head, int k) { + + if (head == null) { + return head; + } + + ListNode temp = head; + + for (int i = 0; i < k; i++) { + if (temp == null) return head;//不够,不进行反转 + temp = temp.next; + } + + //够,反转遍历的范围 + ListNode root = reverseNode1(head, temp); + head.next = reverseKGroup1(temp, k); + + return root; + + + } + + public ListNode reverseNode1(ListNode start, ListNode end) { + if (start == null) { + return start; + } + + ListNode fake = new ListNode(); + ListNode temp = start; + ListNode tempNext; + + while (temp != end) { + tempNext = temp.next; + temp.next = fake.next; + fake.next = temp; + temp = tempNext; + } + + return fake.next; + + } + + } diff --git a/Leecode/src/main/java/com/markilue/leecode/hot100/second/T49_124_MaxPathSum.java b/Leecode/src/main/java/com/markilue/leecode/hot100/second/T49_124_MaxPathSum.java index 7fc9d18..086b403 100644 --- a/Leecode/src/main/java/com/markilue/leecode/hot100/second/T49_124_MaxPathSum.java +++ b/Leecode/src/main/java/com/markilue/leecode/hot100/second/T49_124_MaxPathSum.java @@ -56,9 +56,29 @@ public class T49_124_MaxPathSum { int curMax = root.val + left + right; if (maxSum < curMax) maxSum = curMax; - return root.val+Math.max(left,right); + return root.val + Math.max(left, right); } + + public int maxPathSum1(TreeNode root) { + findMax(root); + return maxSum; + } + + //本质上就是,每个节点都在自己的位置上获得最大值,那么通过全局遍历,就可以找到整体最大值 + //返回值为要当前位置的最大值 + public int findMax(TreeNode root) { + if (root == null) { + return 0; + } + int leftMax = Math.max(findMax(root.left), 0); + int rightMax = Math.max(findMax(root.right), 0); + + int curMax = leftMax + rightMax + root.val; + if (curMax > maxSum) maxSum = curMax; + return Math.max(leftMax, rightMax) + root.val; + } + } diff --git a/Leecode/src/main/java/com/markilue/leecode/hot100/second/T57_142_MaxProduct.java b/Leecode/src/main/java/com/markilue/leecode/hot100/second/T57_142_MaxProduct.java index 19ea477..6c7072f 100644 --- a/Leecode/src/main/java/com/markilue/leecode/hot100/second/T57_142_MaxProduct.java +++ b/Leecode/src/main/java/com/markilue/leecode/hot100/second/T57_142_MaxProduct.java @@ -50,7 +50,7 @@ public class T57_142_MaxProduct { //当前位置,一定是要了当前的数的,通过max来中间的过程 temp = dp0; dp0 = Math.min(Math.min(nums[i], dp0 * nums[i]), dp1 * nums[i]); - dp1 = Math.max(Math.max(nums[i], temp* nums[i]), dp1 * nums[i]); + dp1 = Math.max(Math.max(nums[i], temp * nums[i]), dp1 * nums[i]); if (max < dp1) { max = dp1; } @@ -58,4 +58,23 @@ public class T57_142_MaxProduct { return max; } + + //二刷:其实确定当前位置要的最大值和最小值 + public int maxProduct2(int[] nums) { + int result = nums[0]; + + int[][] dp = new int[nums.length][2]; + + dp[0][0] = nums[0];//要当前的最小值 :因为是子数组,必须要当前 + dp[0][1] = nums[0];//要当前的最大值 :因为是子数组,必须要当前 + + for (int i = 1; i < nums.length; i++) { + dp[i][0] = Math.min(Math.min(nums[i], dp[i-1][0] * nums[i]), dp[i-1][1] * nums[i]); + dp[i][1] = Math.max(Math.max(nums[i], dp[i-1][0] * nums[i]), dp[i-1][1] * nums[i]);//因为如果当前为负数,最大值也可以变最小值;最小值也可以变最大值 + if (result < dp[i][1]) result = dp[i][1]; + } + + + return result; + } } diff --git a/Leecode/src/main/java/com/markilue/leecode/hot100/second/T58_155_MinStack.java b/Leecode/src/main/java/com/markilue/leecode/hot100/second/T58_155_MinStack.java new file mode 100644 index 0000000..83c8958 --- /dev/null +++ b/Leecode/src/main/java/com/markilue/leecode/hot100/second/T58_155_MinStack.java @@ -0,0 +1,48 @@ +package com.markilue.leecode.hot100.second; + +import java.util.ArrayDeque; +import java.util.Deque; + +/** + *@BelongsProject: Leecode + *@BelongsPackage: com.markilue.leecode.hot100.second + *@Author: markilue + *@CreateTime: 2023-05-16 11:29 + *@Description: TODO 力扣155 最小栈 + *@Version: 1.0 + */ +public class T58_155_MinStack { + + Deque stack; + Deque minStack; + + //使用两个stack来记录 + public T58_155_MinStack() { + //两个栈同进同出,只是进的永远是最小值即可 + stack = new ArrayDeque<>(); + minStack = new ArrayDeque<>(); + minStack.push(Integer.MAX_VALUE);//永远保证最小栈里有元素 + + } + + public void push(int val) { + stack.push(val); + minStack.push(Math.min(minStack.peek(), val)); + } + + public void pop() { + stack.pop(); + minStack.pop(); + } + + public int top() { + if (stack.isEmpty()) { + return -1; + } + return stack.peek(); + } + + public int getMin() { + return minStack.peek(); + } +}