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; + } + } +}