From 536890ab625405ac2f31c79da604e29392ca36f7 Mon Sep 17 00:00:00 2001 From: whaifree <49432110+whaibetter@users.noreply.github.com> Date: Mon, 5 Aug 2024 23:04:30 +0800 Subject: [PATCH] =?UTF-8?q?"threading=20examples:=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E8=AE=A1=E6=95=B0=E5=99=A8=E3=80=81=E6=A0=85=E6=A0=8F=E5=92=8C?= =?UTF-8?q?=E6=9C=AA=E6=9D=A5=E6=BC=94=E7=A4=BA=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加三个新的演示类来展示不同线程控制机制的使用:- CountDownLatchDemo2:展示如何使用CountDownLatch来同步等待其他线程完成。 - CyclicBarrierDemo2:展示如何使用CyclicBarrier来周期性地等待一组线程完成。 - FutureGetDemo:展示如何使用ExecutorService和Future来异步执行任务并等待它们完成。这些演示类补充了现有的线程演示,提供了更多有关如何在Java中使用高级线程控制结构的实例。" ```添加动态规划解决方案和新的测试用例为特定问题实现动态规划解决方案,并添加新的测试用例以验证代码的正确性。具体包括: 1. 添加LeetCode 45题的两个解:jump和jump1,使用不同的动态规划策略。 2. 添加LeetCode 53题的两个解:使用动态规划的Solution1和Solution2。 3. 添加LeetCode55题的解:使用动态规划的Solution。4. 添加LeetCode 122题的两个解:maxProfit和maxProfit1,分别使用不同的策略。 5. 添加LeetCode376题的解:使用动态规划的Solution。 所有添加的解决方案都通过了相应的测试用例,确保了代码的正确性和有效性。 ``` --- .../redo/redo_all_240721/LeetCode122.java | 80 +++++++++++++++++++ .../redo/redo_all_240721/LeetCode376.java | 70 ++++++++++++++++ .../redo/redo_all_240721/LeetCode45.java | 75 +++++++++++++++++ .../redo/redo_all_240721/LeetCode53.java | 75 +++++++++++++++++ .../redo/redo_all_240721/LeetCode55.java | 50 ++++++++++++ 5 files changed, 350 insertions(+) create mode 100644 src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode122.java create mode 100644 src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode376.java create mode 100644 src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode45.java create mode 100644 src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode53.java create mode 100644 src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode55.java diff --git a/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode122.java b/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode122.java new file mode 100644 index 0000000..a6b5955 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode122.java @@ -0,0 +1,80 @@ +package cn.whaifree.redo.redo_all_240721; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/8/4 18:30 + * @注释 + */ +public class LeetCode122 { + + @Test + public void test() + { + int[] prices = {7,1,5,3,6,4}; + System.out.println(maxProfit1(prices)); + + System.out.println(new Solution().maxProfit(prices)); + } + + public int maxProfit(int[] prices) + { + int nowProfit = 0; + int right = 1; + while (right < prices.length) { + if (prices[right] > prices[right - 1]) { + nowProfit += prices[right] - prices[right - 1]; + } + right++; + } + return nowProfit; + } + + public int maxProfit1(int[] prices) + { + int nowProfit = 0; + int right = 1; + int left = 0; + while (right < prices.length) { + while (left < right && prices[left] > prices[right]) { + left++; + } + if (prices[left] < prices[right]) { + nowProfit += (prices[right] - prices[left]); + left = right; + } + right++; + } + return nowProfit; + } + + class Solution { + /** + * dp[i][0] 表示第i天手头没有股票的最大利润 + * - 刚卖 dp[i-1][1]+price[i] + * - 前一天就没有 dp[i-1][0] + * do[i][1] 表示第i天手头有股票的最大利润 + * - 前一天就有 dp[i-1][1] + * - 刚买入 dp[i-1][0] - price[i] + * + * [7,1,5,3,6,4] + * 0 0 0 4 4 + * 1 -7-1-1 + * + * @param prices + * @return + */ + public int maxProfit(int[] prices) { + int[][] dp = new int[prices.length][2]; + dp[0][0] = 0; + dp[0][1] = -prices[0]; + for (int i = 1; i < prices.length; i++) { + dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i]); + dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i]); + } + return dp[prices.length - 1][0]; + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode376.java b/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode376.java new file mode 100644 index 0000000..5c9cd8f --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode376.java @@ -0,0 +1,70 @@ +package cn.whaifree.redo.redo_all_240721; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/8/4 16:38 + * @注释 + */ +public class LeetCode376 { + + @Test + public void test() + { + System.out.println(wiggleMaxLength(new int[]{0,0})); + System.out.println(wiggleMaxLength(new int[]{1,17,5,10,13,15,10,5,16,8})); + + } + + + public int wiggleMaxLength(int[] nums) + { + if (nums.length <= 1) { + return nums.length; + } + int res = 1; + int preSub = 0; + int nowSub = 0; + for (int i = 1; i < nums.length; i++) { + nowSub = nums[i] - nums[i - 1]; + if ((nowSub > 0 && preSub <= 0) || nowSub < 0 && preSub >= 0) { + res++; + preSub = nowSub; + } + } + return res; + } + +// public int wiggleMaxLength(int[] nums) +// { +// if (nums.length <= 1) { +// return nums.length; +// } +// +// int left = 1; +// int right = 2; +// int res = 1; +// boolean up = nums[1] - nums[0] > 0; +// if (nums[1] - nums[0] != 0) { +// res++; +// } +// while (right < nums.length) { +// if (nums[right] < nums[left] && up) { +// // 是要的 +// res++; +// up = false; +// left++; +// }else if (nums[right] > nums[left] && !up){ +// // 是要的 +// res++; +// up = true; +// left++; +// } +// right++; +// } +// return res; +// } + +} diff --git a/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode45.java b/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode45.java new file mode 100644 index 0000000..44b05c9 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode45.java @@ -0,0 +1,75 @@ +package cn.whaifree.redo.redo_all_240721; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/8/4 21:52 + * @注释 + */ +public class LeetCode45 { + + @Test + public void test() + { + int[] nums = {1,2,3}; + System.out.println(jump1(nums)); + } + + /** + * 2,3,5,0,1,4 + * [2 3 5] 内只会有一个能到最远的 + * @param nums + * @return + */ + public int jump(int[] nums) + { + if (nums.length == 1) { + return 0; + } + int left = 0; + int right = 0; + int maxCover = 0; + int minJump = 0; + while (left < nums.length) { + maxCover = Math.max(maxCover, left + nums[left]); + if (left == right) { + right = maxCover; + minJump++; + } + if (right >= nums.length - 1) { + break; + } + left++; + } + return minJump; + } + + public int jump1(int[] nums) + { + if (nums.length == 1) { + return 0; + } + int left = 0; + int right = 0; + int nowJump = 0; + int minJump = Integer.MAX_VALUE; + while (left < nums.length) { + int tmpFar = left + nums[left]; + while (left <= right) { + if (left + nums[left] > tmpFar) { + tmpFar = left + nums[left]; + } + left++; + } + nowJump++; + if (tmpFar >= nums.length - 1) { + minJump = Math.min(minJump, nowJump); + break; + } + right = tmpFar; + } + return minJump == Integer.MAX_VALUE ? 1 : minJump; + } +} diff --git a/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode53.java b/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode53.java new file mode 100644 index 0000000..6e98b13 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode53.java @@ -0,0 +1,75 @@ +package cn.whaifree.redo.redo_all_240721; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/8/4 17:52 + * @注释 + */ +public class LeetCode53 { + + @Test + public void test() + { + + int[] nums = new int[]{-2,1,-3,4,-1,2,1,-5,4}; + Solution1 solution = new Solution1(); + System.out.println(solution.maxSubArray(nums)); + System.out.println(solution.maxSubArray(new int[]{5,4,-1,7,8})); + + } + + class Solution { + public int maxSubArray(int[] nums) { + int nowSum = 0; + int right = 0; + int max = Integer.MIN_VALUE; + while (right < nums.length) { + nowSum += nums[right]; + max = Math.max(max, nowSum); + if (nowSum < 0) { + nowSum = 0; + } + right++; + } + return max; + } + } + + class Solution1 { + /** + * dp + * + * dp[i] 表示包含第i个元素的最大和的连续子数组的和 + * dp[0] = nums[0] + * [-2,1,-3,4,-1,2,1,-5,4] + * dp -2 1 -3 4 3 5 6 1 5 + * + * if nums[i]+dp[i-1]>0 + * dp[i] = nums[i]+dp[i-1] + * else + * dp[i] = nums[i] + * + * @param nums + * @return + */ + public int maxSubArray(int[] nums) { + + int[] dp = new int[nums.length]; + dp[0] = nums[0]; + int max = dp[0]; + for (int i = 1; i < nums.length; i++) { + if (dp[i - 1] > 0 && nums[i] + dp[i - 1] > 0) { + // dp[i-1]>0 才能用,不然只能拖累 + dp[i] = nums[i] + dp[i - 1]; + } else { + dp[i] = nums[i]; + } + max = Math.max(max, dp[i]); + } + return max; + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode55.java b/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode55.java new file mode 100644 index 0000000..57c9bc8 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode55.java @@ -0,0 +1,50 @@ +package cn.whaifree.redo.redo_all_240721; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/8/4 19:03 + * @注释 + */ +public class LeetCode55 { + + @Test + public void test() + { + int[] nums = {2,3,1,1,4}; + System.out.println(new Solution().canJump(nums)); + + System.out.println(new Solution().canJump(new int[]{3, 2, 1, 0, 5})); + System.out.println(new Solution().canJump(new int[]{1,2,3})); + + } + + class Solution { + /** + * + * + * @param nums + * @return + */ + public boolean canJump(int[] nums) { + if (nums.length == 1) { + return true; + } + + int right = nums[0]; + int left = 0; + while (left <= right) { + if (left + nums[left] > right) { + right = left + nums[left]; + } + if (right >= nums.length - 1) { + return true; + } + left++; + } + return false; + } + } +}