提交说明:更新了LeetCode337、LeetCode739、HS、LeetCode705的代码,并添加了测试用例。优化了代码结构,提高了代码的可读性和可维护性。
This commit is contained in:
parent
7ed697d929
commit
441579c86c
@ -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];
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
67
src/main/java/cn/whaifree/leetCode/Hash/LeetCode705.java
Normal file
67
src/main/java/cn/whaifree/leetCode/Hash/LeetCode705.java
Normal file
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package cn.whaifree.MonotoneStack;
|
||||
package cn.whaifree.leetCode.MonotoneStack;
|
||||
|
||||
import org.junit.Test;
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user