From 441579c86ce0c5875668fe3603c3f638f8691ec3 Mon Sep 17 00:00:00 2001 From: whaifree <49432110+whaibetter@users.noreply.github.com> Date: Sun, 14 Apr 2024 22:16:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E8=AF=B4=E6=98=8E=EF=BC=9A?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BA=86LeetCode337=E3=80=81LeetCode739?= =?UTF-8?q?=E3=80=81HS=E3=80=81LeetCode705=E7=9A=84=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=EF=BC=8C=E5=B9=B6=E6=B7=BB=E5=8A=A0=E4=BA=86=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E7=94=A8=E4=BE=8B=E3=80=82=E4=BC=98=E5=8C=96=E4=BA=86=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E7=BB=93=E6=9E=84=EF=BC=8C=E6=8F=90=E9=AB=98=E4=BA=86?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E7=9A=84=E5=8F=AF=E8=AF=BB=E6=80=A7=E5=92=8C?= =?UTF-8?q?=E5=8F=AF=E7=BB=B4=E6=8A=A4=E6=80=A7=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cn/whaifree/interview/HS/HS.java | 73 ++++++++++++++++++- .../whaifree/leetCode/Hash/LeetCode705.java | 67 +++++++++++++++++ .../MonotoneStack/LeetCode739.java | 2 +- .../redo/redo_24_4_13/LeetCode337.java | 39 ++++++++++ 4 files changed, 176 insertions(+), 5 deletions(-) create mode 100644 src/main/java/cn/whaifree/leetCode/Hash/LeetCode705.java rename src/main/java/cn/whaifree/{ => leetCode}/MonotoneStack/LeetCode739.java (97%) diff --git a/src/main/java/cn/whaifree/interview/HS/HS.java b/src/main/java/cn/whaifree/interview/HS/HS.java index ab22078..6cf7c49 100644 --- a/src/main/java/cn/whaifree/interview/HS/HS.java +++ b/src/main/java/cn/whaifree/interview/HS/HS.java @@ -49,12 +49,9 @@ public class HS { public static void main(String[] args) { double[] doubles = {3, 2, 6, 5, 1, 3}; System.out.println(get_max_profit(10000, 6, doubles, 2)); - } /** - * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 - * * 根据输入计算最大收益 * @param M double浮点型 初始资金 * @param N int整型 历史价格天数 @@ -62,7 +59,7 @@ public class HS { * @param K int整型 最大允许交易次数 * @return double浮点型 */ - public static double get_max_profit (double M, int N, double[] historyPrices, int K) { + public static double get_max_profit1 (double M, int N, double[] historyPrices, int K) { // write code here // dp[0] 表示当天手里没有股票的手里的钱 // - 没有钱有两种 @@ -94,4 +91,72 @@ public class HS { return res; } + /** + * 根据输入计算最大收益 + * @param M double浮点型 初始资金 + * @param N int整型 历史价格天数 + * @param historyPrices double浮点型一维数组 N天历史价格 + * @param K int整型 最大允许交易次数 + * @return double浮点型 + */ + public static double get_max_profit (double M, int N, double[] historyPrices, int K) { + /** + * dp[i][j] 表示第i天手里持有的钱 j为1-2K,分别奇数时表示未持有 偶数时表示持有 + * 初始化:dp[0][持有\偶数] = M/history[i] 表示di 第0天持有的股票数量 + * + */ + + // 表示手里有股票时的手里有的钱 + double[][] buy = new double[N][K]; + // 表示手里没有股票时的手里的钱, sell【i】【j】表示第i天,手里有已经没有第j支股票 + double[][] sell = new double[N][K]; + for (int i = 0; i < K; i++) { + sell[0][i] = M; + } + + /** + * 未持有 + * - 前一天就未持有 + * - 当天卖出 + * 持有 + * - 前一天就持有 + * - 当天买入 + */ + for (int i = 1; i < N; i++) { + // 当天有股票 + buy[i][0] = Math.max(buy[i - 1][0], -historyPrices[i]); + // 当天没有股票 + sell[i][0] = Math.max(sell[i - 1][0], buy[i - 1][0] + buy[i - 1][0] * historyPrices[i]); + for (int j = 1; j < K; j++) { + buy[i][j] = Math.max(buy[i - 1][j], sell[i - 1][j] - historyPrices[i]); + sell[i][j] = Math.max(sell[i - 1][j], buy[i - 1][j] + buy[i - 1][j] * historyPrices[i]); + } + } + + + return 1; + + } + + public static int maxProfit(int M, int N, int K, int[] historyPrices) { + int[][] buy = new int[N][K + 1]; + int[][] sell = new int[N][K + 1]; + + buy[0][0] = -M; + + for (int i = 1; i < N; i++) { + buy[i][0] = Math.max(buy[i-1][0], -historyPrices[i]); + sell[i][0] = Math.max(sell[i-1][0], buy[i-1][0] + historyPrices[i]); + + for (int j = 1; j <= K; j++) { + buy[i][j] = Math.max(buy[i-1][j], sell[i-1][j-1] - historyPrices[i]); + sell[i][j] = Math.max(sell[i-1][j], buy[i-1][j] + historyPrices[i]); + } + } + + return sell[N-1][K]; + } + + + } diff --git a/src/main/java/cn/whaifree/leetCode/Hash/LeetCode705.java b/src/main/java/cn/whaifree/leetCode/Hash/LeetCode705.java new file mode 100644 index 0000000..d61d140 --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Hash/LeetCode705.java @@ -0,0 +1,67 @@ +package cn.whaifree.leetCode.Hash; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/4/14 13:15 + * @注释 + */ +public class LeetCode705 { + + @Test + public void test() + { + MyHashSet myHashSet = new MyHashSet(); + myHashSet.add(1); // set = [1] + myHashSet.add(2); // set = [1, 2] + System.out.println(myHashSet.contains(1)); // 返回 True + System.out.println(myHashSet.contains(3)); // 返回 False ,(未找到) + System.out.println(myHashSet.contains(2)); + myHashSet.remove(2); + System.out.println(myHashSet.contains(2)); // 返回 False ,(已移除) + + } + + /** + * int 表示4字节 32位 每一位代表一个位置 + * 1000000/32 40000表示桶 + */ + class MyHashSet { + + int[] bucket = null; + + public MyHashSet() { + bucket = new int[40000]; + } + + public void add(int key) { + int bkLoc = key / 32; + int ex = key % 32; + int num = bucket[bkLoc]; + // 如num=4 (100) 1<<1= 010 进行|操作后变成 110 + bucket[bkLoc] = num | (1 << ex); + } + + public void remove(int key) { + int bkLoc = key / 32; + int ex = key % 32; + int num = bucket[bkLoc]; + // 如num=7 (111) 1<<1= 010 进行~后变成101 & 操作后变成 101 + bucket[bkLoc] = num & ~(1 << ex); + } + + public boolean contains(int key) { + int bkLoc = key / 32; + int ex = key % 32; // 偏移 + int num = bucket[bkLoc]; // 数字 + return ((num >> ex) & 1) == 1; + // 如num=7 (111) 1<<1= 010 进行~后变成101 & 操作后变成 101 +// int i = num & (1 << ex); +// return 1 << ex == i; + } + + } + +} diff --git a/src/main/java/cn/whaifree/MonotoneStack/LeetCode739.java b/src/main/java/cn/whaifree/leetCode/MonotoneStack/LeetCode739.java similarity index 97% rename from src/main/java/cn/whaifree/MonotoneStack/LeetCode739.java rename to src/main/java/cn/whaifree/leetCode/MonotoneStack/LeetCode739.java index c31c588..5166d71 100644 --- a/src/main/java/cn/whaifree/MonotoneStack/LeetCode739.java +++ b/src/main/java/cn/whaifree/leetCode/MonotoneStack/LeetCode739.java @@ -1,4 +1,4 @@ -package cn.whaifree.MonotoneStack; +package cn.whaifree.leetCode.MonotoneStack; import org.junit.Test; diff --git a/src/main/java/cn/whaifree/redo/redo_24_4_13/LeetCode337.java b/src/main/java/cn/whaifree/redo/redo_24_4_13/LeetCode337.java index 4661cf5..533e4db 100644 --- a/src/main/java/cn/whaifree/redo/redo_24_4_13/LeetCode337.java +++ b/src/main/java/cn/whaifree/redo/redo_24_4_13/LeetCode337.java @@ -1,5 +1,8 @@ package cn.whaifree.redo.redo_24_4_13; +import cn.whaifree.leetCode.model.TreeNode; +import org.junit.Test; + /** * @version 1.0 * @Author whai文海 @@ -7,4 +10,40 @@ package cn.whaifree.redo.redo_24_4_13; * @注释 */ public class LeetCode337 { + @Test + public void test() { + + Solution solution = new Solution(); + System.out.println(solution.rob(TreeNode.constructTreeByArray(3,2,3,null,3,null,1))); + } + + + class Solution { + /** + * 0 表示不选该点最大收益 + * 1 表示选择该点的最大收益 + * @param root + * @return + */ + public int rob(TreeNode root) { + int[] res = dpIn(root); + return Math.max(res[0], res[1]); + } + + public int[] dpIn(TreeNode root) { + if (root == null) { + return new int[]{0, 0}; + } + int[] dp = new int[2]; + int[] left = dpIn(root.left); + int[] right = dpIn(root.right); + + // 不选择本点,那么子节点随便选择,只要收益大 + dp[0] = Math.max(left[0], left[1]) + Math.max(right[0], right[1]); + // 选择了本点,子节点就不能选 + dp[1] = left[0] + right[0] + root.val; + return dp; + } + } + }