diff --git a/src/main/java/cn/whaifree/leetCode/Greedy/LeetCode134.java b/src/main/java/cn/whaifree/leetCode/Greedy/LeetCode134.java index 09397bb..a36b9c5 100644 --- a/src/main/java/cn/whaifree/leetCode/Greedy/LeetCode134.java +++ b/src/main/java/cn/whaifree/leetCode/Greedy/LeetCode134.java @@ -11,11 +11,26 @@ import org.junit.Test; public class LeetCode134 { @Test - public void test() { - System.out.println(new Solution().canCompleteCircuit( - new int[]{5,1,2,3,4}, - new int[]{4,4,1,5,1} - )); + public void test() throws InterruptedException { + + System.out.println("初始化内存 -Xms"); + System.out.println(Runtime.getRuntime().totalMemory() / 1024 / 1024 + "m"); + + System.out.println("最大可使用内存 :"); + System.out.println(Runtime.getRuntime().freeMemory() / 1024 / 1024 + "m"); + + System.out.println("最大堆内存:-Xmx"); + System.out.println(Runtime.getRuntime().maxMemory() / 1024 / 1024 + "m"); + + // -XX+PrintGCDetails + + int[] ints = new int[10000]; +// Thread.sleep(1000000); + +// System.out.println(new Solution().canCompleteCircuit( +// new int[]{5,1,2,3,4}, +// new int[]{4,4,1,5,1} +// )); } diff --git a/src/main/java/cn/whaifree/leetCode/Greedy/LeetCode406.java b/src/main/java/cn/whaifree/leetCode/Greedy/LeetCode406.java new file mode 100644 index 0000000..3fa6ba1 --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Greedy/LeetCode406.java @@ -0,0 +1,66 @@ +package cn.whaifree.leetCode.Greedy; + +import org.junit.Test; + +import java.util.*; +import java.util.function.IntFunction; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/2/29 11:52 + * @注释 + */ +public class LeetCode406 { + + @Test + public void test() throws InterruptedException { + int[][] ints = new int[][]{ + {7,0},{4,4},{7,1},{5,0},{6,1},{5,2} + }; + + String gc = new String("gc"); + List list = new LinkedList<>(); + int i = 0; + while (true) { + list.add(gc + gc + i++); + } + +// for (int[] i : new Solution().reconstructQueue(ints)) { +// System.out.println(Arrays.toString(i)); +// } + } + + class Solution { + public int[][] reconstructQueue(int[][] people) { + // {7,0},{4,4},{7,1},{5,0},{6,1},{5,2} + Arrays.sort(people, new Comparator() { + @Override + public int compare(int[] o1, int[] o2) { + if (o1[0] == o2[0]) { + // 身高相同时,前面数量少的在前 + return o1[1] - o2[1]; + }else { + return o2[0] - o1[0]; + } + } + }); + for (int[] person : people) { + System.out.println(Arrays.toString(person)); + } + + System.out.println("===="); + + // 排序: [[7,0], [7,1], [6,1], [5,0], [5,2],[4,4]] + List list = new LinkedList<>(); + + // 节点i的前面必然都比i高,那么只要把i插入到i[1]对应的位置,必然满足条件 + for (int[] p : people) { + list.add(p[1],p); + } + + return list.toArray(new int[people.length][]); + } + } + +} diff --git a/src/main/java/cn/whaifree/leetCode/Greedy/LeetCode860.java b/src/main/java/cn/whaifree/leetCode/Greedy/LeetCode860.java index 9d4c63b..d59b095 100644 --- a/src/main/java/cn/whaifree/leetCode/Greedy/LeetCode860.java +++ b/src/main/java/cn/whaifree/leetCode/Greedy/LeetCode860.java @@ -16,13 +16,14 @@ public class LeetCode860 { @Test public void test() { - System.out.println(new Solution().lemonadeChange(new int[]{5,5,10,10,20})); + System.out.println(new Solution().lemonadeChange(new int[]{5, 5, 10, 10, 20})); } class Solution { /** * bills[i] 不是 5 就是 10 或是 20 + * * @param bills * @return */ @@ -55,4 +56,6 @@ public class LeetCode860 { } + + } diff --git a/src/main/java/cn/whaifree/leetCode/T.java b/src/main/java/cn/whaifree/leetCode/T.java index f819f69..e2af711 100644 --- a/src/main/java/cn/whaifree/leetCode/T.java +++ b/src/main/java/cn/whaifree/leetCode/T.java @@ -1,5 +1,8 @@ package cn.whaifree.leetCode; +import java.util.ArrayList; +import java.util.Random; + /** * @version 1.0 * @Author whai文海 @@ -100,3 +103,28 @@ class VirtualMethodTest { fa.show("a"); } } + + + +class Picture{ + int number = 0; + public Picture() { + + } + + public Picture(int number) { + this.number = number; + } + + public static void main(String[]args){ + ArrayList list = new ArrayList<>(); + while(true){ + try { + Thread.sleep(10); + } catch (InterruptedException e){ + e.printStackTrace(); + } + list.add(new Picture(new Random().nextInt(1024*1024))); + } + } +} diff --git a/src/main/java/cn/whaifree/leetCode/Test.java b/src/main/java/cn/whaifree/leetCode/Test.java index 74fe730..ea737f6 100644 --- a/src/main/java/cn/whaifree/leetCode/Test.java +++ b/src/main/java/cn/whaifree/leetCode/Test.java @@ -2,10 +2,7 @@ package cn.whaifree.leetCode; import cn.whaifree.leetCode.Tree.LeetCode94; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.Map; -import java.util.PriorityQueue; +import java.util.*; import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; @@ -38,6 +35,11 @@ public class Test { private int getNumber; + + + + + public int minus() { localVarl(); int i = 1; diff --git a/src/main/java/cn/whaifree/redo/redo_24_3_1/LeetCode122.java b/src/main/java/cn/whaifree/redo/redo_24_3_1/LeetCode122.java new file mode 100644 index 0000000..a5dddce --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_3_1/LeetCode122.java @@ -0,0 +1,31 @@ +package cn.whaifree.redo.redo_24_3_1; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/1 11:16 + * @注释 + */ +public class LeetCode122 { + + @Test + public void test() { + System.out.println(new Solution().maxProfit(new int[]{7, 1, 5, 3, 6, 4})); + } + + class Solution { + public int maxProfit(int[] prices) { + int profit = 0; + for (int i = 1; i < prices.length; i++) { + int sub = prices[i] - prices[i - 1]; + if (sub > 0) { + profit += sub; + } + } + return profit; + } + } + +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_3_1/LeetCode134.java b/src/main/java/cn/whaifree/redo/redo_24_3_1/LeetCode134.java new file mode 100644 index 0000000..3579f08 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_3_1/LeetCode134.java @@ -0,0 +1,56 @@ +package cn.whaifree.redo.redo_24_3_1; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/1 12:07 + * @注释 + */ +public class LeetCode134 { + + @Test + public void test() { + System.out.println(new Solution().canCompleteCircuit(new int[]{1, 2, 3, 4, 5}, new int[]{3, 4, 5, 1, 2})); + System.out.println(new Solution().canCompleteCircuit(new int[]{5,1,2,3,4}, new int[]{4,4,1,5,1})); + + } + + class Solution { + /** + * 1. 判断能否循环 + * 2. 如果一定可以循环,找到第一个能剩下油量的 + * @param gas + * @param cost + * @return + */ + public int canCompleteCircuit(int[] gas, int[] cost) { + // 计算差值 + int length = gas.length; + int[] rent = new int[length]; + int rentGas = 0; + for (int i = 0; i < length; i++) { + rent[i] = gas[i] - cost[i]; + rentGas += rent[i]; + } + if (rentGas<0){ + return -1; + } + + int have = 0; + int resIndex = 0; + for (int i = 0; i < length; i++) { + have += rent[i]; + if (have < 0) { + // 油量不够,从下一个点开始 + resIndex = (i + 1) % length; + have = 0; + } + } + // [a,b] a内有不够用的时候,b内也有可能有不够用的时候,用index标记 + return resIndex; + } + + } +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_3_1/LeetCode376.java b/src/main/java/cn/whaifree/redo/redo_24_3_1/LeetCode376.java new file mode 100644 index 0000000..cf5f349 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_3_1/LeetCode376.java @@ -0,0 +1,50 @@ +package cn.whaifree.redo.redo_24_3_1; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/1 10:37 + * @注释 + */ +public class LeetCode376 { + + @Test + public void test() { + System.out.println(new Solution().wiggleMaxLength(new int[]{1, 7, 4, 9, 2, 5})); + } + + class Solution { + /** + * 摆动序列 + * + * @param nums + * @return + */ + public int wiggleMaxLength(int[] nums) { + if (nums.length == 1) { + return 1; + } + + // 计算两数差值 + int curSub = 0; + int preSub = 0; + + int res = 1; + + for (int i = 1; i < nums.length; i++) { + curSub = nums[i] - nums[i - 1]; + // preSub = 0 主要让第一次进入循环 + if ((curSub < 0 && preSub >= 0) || (curSub > 0 && preSub <= 0)) { + res++; + preSub = curSub; + } + } + + return res; + + } + } + +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_3_1/LeetCode45.java b/src/main/java/cn/whaifree/redo/redo_24_3_1/LeetCode45.java new file mode 100644 index 0000000..335544a --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_3_1/LeetCode45.java @@ -0,0 +1,51 @@ +package cn.whaifree.redo.redo_24_3_1; + +import org.junit.Test; + +import javax.annotation.PreDestroy; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/1 11:36 + * @注释 + */ +public class LeetCode45 { + + @Test + public void test() { + System.out.println(new Solution().jump(new int[]{2,3,1,1,4})); + } + + class Solution { + /** + * 每个index都有一个覆盖区间,每个覆盖区间之间只jump一次 + * @param nums + * @return + */ + public int jump(int[] nums) { + if (nums.length == 1) { + return 0; + } + + int maxCover = 0; // 最大覆盖区间 + int curCover = 0; // 当前覆盖区间 + int jumpCount = 0; // 跳跃数 + for (int i = 0; i < nums.length; i++) { + maxCover = Math.max(maxCover, i + nums[i]); + // 每次jump count++ + if (maxCover >= nums.length - 1) { + jumpCount++; + break; + } + if (i == curCover) { + jumpCount++; + curCover = maxCover; // 到了最后一个,[a,b] a已经执行完了,下次就是b区间中jump一次 + } + + } + return jumpCount; + } + } + +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_3_1/LeetCode51.java b/src/main/java/cn/whaifree/redo/redo_24_3_1/LeetCode51.java new file mode 100644 index 0000000..17bcb01 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_3_1/LeetCode51.java @@ -0,0 +1,92 @@ +package cn.whaifree.redo.redo_24_3_1; + +import cn.whaifree.leetCode.T; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/1 12:24 + * @注释 + */ +public class LeetCode51 { + + @Test + public void test() { + new Solution().solveNQueens(4); + } + + + + + class Solution { + + char[][] map = null; + List> res = new ArrayList<>(); + + /** + * 判断是否符合条件 + * @param n + * @return + */ + public List> solveNQueens(int n) { + map = new char[n][n]; + for (char[] chars : map) { + Arrays.fill(chars, '.'); + } + backTracking(n, 0); + return res; + } + + public void backTracking(int n,int row) { + if (n == row) { + List e = new ArrayList<>(); + for (char[] chars : map) { + StringBuilder s = new StringBuilder(); + for (char aChar : chars) { + s.append(aChar); + } + e.add(s.toString()); + } + res.add(e); + return; + } + + for (int i = 0; i < n; i++) { + if (isValid(i, row, n)) { + map[row][i] = 'Q'; + backTracking(n, row + 1); + map[row][i] = '.'; + } + } + + } + + boolean isValid(int col, int row,int n) { + + // 往上查找 + for (int i = row; i >= 0; i--) { + if (map[i][col] == 'Q') { + return false; + } + } + // 左上45 这里不检查本节点可以提升很大的效率 + for (int i = col-1, j = row-1; i >= 0 && j >= 0; i--, j--) { + if (map[j][i] == 'Q') { + return false; + } + } + // 右上45 + for (int i = col + 1, j = row - 1; i < n && j >= 0; i++, j--) { + if (map[j][i] == 'Q') { + return false; + } + } + return true; + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_3_1/LeetCode53.java b/src/main/java/cn/whaifree/redo/redo_24_3_1/LeetCode53.java new file mode 100644 index 0000000..ab8adfd --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_3_1/LeetCode53.java @@ -0,0 +1,39 @@ +package cn.whaifree.redo.redo_24_3_1; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/1 11:01 + * @注释 + */ +public class LeetCode53 { + + @Test + public void test( + ) { + System.out.println(new Solution().maxSubArray(new int[]{-2,-1})); + } + + class Solution { + /** + * 一旦加的那个区间为负,只会拖累后面的串,从下一个重加 + */ + public int maxSubArray(int[] nums) { + if (nums.length == 1) { + return nums[0]; + } + int maxSum = Integer.MIN_VALUE; + int curSum = 0; + for (int i = 0; i < nums.length; i++) { + curSum += nums[i]; + maxSum = Math.max(curSum, maxSum); + if (curSum < 0) { + curSum = 0; + } + } + return maxSum; + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_3_1/LeetCode55.java b/src/main/java/cn/whaifree/redo/redo_24_3_1/LeetCode55.java new file mode 100644 index 0000000..5250f66 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_3_1/LeetCode55.java @@ -0,0 +1,34 @@ +package cn.whaifree.redo.redo_24_3_1; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/1 11:21 + * @注释 + */ +public class LeetCode55 { + @Test + public void test() { + System.out.println(new Solution().canJump(new int[]{2,3,1,1,4})); + } + + class Solution { + public boolean canJump(int[] nums) { + if (nums.length == 1) { + return true; + } + + int maxCover = 0; + for (int i = 0; i <= maxCover; i++) { + maxCover = Math.max(maxCover, i + nums[i]); + if (maxCover >= nums.length - 1) { + return true; + } + } + return false; + } + } + +}