leecode更新

This commit is contained in:
markilue 2023-02-08 13:23:26 +08:00
parent ad8da1d5ee
commit c893db74ad
7 changed files with 221 additions and 12 deletions

View File

@ -13,7 +13,7 @@ import org.junit.Test;
* 返回 你能获得的 最大 利润
* @Version: 1.0
*/
public class MaxProfit {
public class T04_MaxProfit {
@Test
public void test(){
int[] prices = {7, 1, 5, 3, 6, 4};

View File

@ -14,7 +14,7 @@ import org.junit.Test;
* 判断你是否能够到达最后一个下标
* @Version: 1.0
*/
public class CanJump {
public class T05_CanJump {
@Test
public void test(){

View File

@ -2,9 +2,6 @@ package com.markilue.leecode.greedy;
import org.junit.Test;
import java.util.ArrayList;
import java.util.HashSet;
/**
* @BelongsProject: Leecode
* @BelongsPackage: com.markilue.leecode.greedy
@ -16,7 +13,7 @@ import java.util.HashSet;
* 你的目标是使用最少的跳跃次数到达数组的最后一个位置
* @Version: 1.0
*/
public class Jump {
public class T06_Jump {
@Test
public void test() {

View File

@ -0,0 +1,113 @@
package com.markilue.leecode.greedy.second;
import org.junit.Test;
/**
*@BelongsProject: Leecode
*@BelongsPackage: com.markilue.leecode.greedy.second
*@Author: markilue
*@CreateTime: 2023-02-08 11:10
*@Description:
* TODO 力扣122题 买卖股票的最佳时机II:
* 给你一个整数数组 prices 其中 prices[i] 表示某支股票第 i 天的价格
* 在每一天你可以决定是否购买和/或出售股票你在任何时候 最多 只能持有 一股 股票你也可以先购买然后在 同一天 出售
* 返回 你能获得的 最大 利润
*@Version: 1.0
*/
public class T04_MaxProfit {
@Test
public void test() {
int[] prices = {7, 1, 5, 3, 6, 4};
System.out.println(maxProfit1(prices));
}
/**
* 贪心思路:可以把当前的利润分为昨天有股票今天卖了(prices[i]>lastPrice)和昨天有股票今天买了(prices[i]<lastPrice)因为他可以当天买当天卖
*/
public int maxProfit(int[] prices) {
int profit = 0;
int lastPrice = prices[0];
for (int i = 1; i < prices.length; i++) {
if (prices[i] > lastPrice) {
profit += prices[i] - lastPrice;
}
//无论卖不卖都需要重置当前
lastPrice = prices[i];
}
return profit;
}
/**
* dp思路:可以把今天今天的收益分为 今天手里有股票 今天手里没股票 两者的最大值
* TODO dp五部曲:
* 1)dp定义:dp[i][0]表示第i天手里没股票的收益dp[i][1]表示第i天手里有股票的收益
* 2)dp状态转移方程:
* 1.dp[i][0] 手里没有股票的收益昨天没有今天也没有 昨天有今天卖了
* dp[i][0]=max(dp[i-1][0],dp[i-1][1]+prices[i])
* 2.dp[i][1] 手里有股票的收益: 昨天有今天没买 昨天有今天买了
* dp[i][1]=max(dp[i-1][1],dp[i-1][0]-prices[i])
* 3)dp初始化:dp[0][0]=0;dp[0][1]=-prices[0]
* 4)dp遍历顺序:从前往后
* 5)dp举例推导: prices = [7,1,5,3,6,4]为例
* [0 1]
* 7: 0 -7
* 1: 0 -1
* 5: 4 -1
* 3: 4 1
* 6: 7 1
* 4: 7 3
*
*/
public int maxProfit1(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];
}
/**
* 一维dp
* @param prices
* @return
*/
public int maxProfit2(int[] prices) {
int dp0=0;
int dp1=-prices[0];
for (int i = 1; i < prices.length; i++) {
dp0=Math.max(dp0,dp1+prices[i]);
dp1=Math.max(dp1,dp0-prices[i]);
}
return dp0;
}
/**
* 官方最快:贪心算法可能快在不用赋值lastNum 0ma
* @param prices
* @return
*/
public int maxProfit3(int[] prices) {
int ans = 0;
for(int i=1; i< prices.length; i++){
if(prices[i] > prices[i-1]){
ans += (prices[i] - prices[i-1]);
}
}
return ans;
}
}

View File

@ -0,0 +1,44 @@
package com.markilue.leecode.greedy.second;
import org.junit.Test;
/**
*@BelongsProject: Leecode
*@BelongsPackage: com.markilue.leecode.greedy.second
*@Author: markilue
*@CreateTime: 2023-02-08 11:43
*@Description:
* TODO 力扣55题 跳跃游戏:
* 给定一个非负整数数组 nums 你最初位于数组的 第一个下标
* 数组中的每个元素代表你在该位置可以跳跃的最大长度
* 判断你是否能够到达最后一个下标
*@Version: 1.0
*/
public class T05_CanJump {
@Test
public void test(){
int[] nums={2,0};
System.out.println(canJump(nums));
}
/**
* 思路:事实上就是计算在能到达的范围内的最大值
* @param nums
* @return
*/
public boolean canJump(int[] nums) {
if (nums.length == 1) return true;
int maxIndex = 0;
for (int i = 0; i < nums.length - 1; i++) {
if (i > maxIndex) return false;//遍历到了没法到的地方了还没有找到则到不了了
maxIndex = Math.max(maxIndex, i + nums[i]);
if (maxIndex >= nums.length - 1) return true;//可以遍历到了,直接返回
}
return false;//遍历完了也没有返回ture则返回false
}
}

View File

@ -0,0 +1,55 @@
package com.markilue.leecode.greedy.second;
import org.junit.Test;
import java.util.jar.JarEntry;
/**
*@BelongsProject: Leecode
*@BelongsPackage: com.markilue.leecode.greedy.second
*@Author: markilue
*@CreateTime: 2023-02-08 12:00
*@Description:
* TODO 力扣45题 跳跃游戏II:
* 给定一个长度为 n 0 索引整数数组 nums初始位置为 nums[0]
* 每个元素 nums[i] 表示从索引 i 向前跳转的最大长度换句话说如果你在 nums[i] 你可以跳转到任意 nums[i + j] :
* 0 <= j <= nums[i]
* i + j < n
* 返回到达 nums[n - 1] 的最小跳跃次数生成的测试用例可以到达 nums[n - 1]
*@Version: 1.0
*/
public class T06_Jump {
@Test
public void test(){
// int[] nums = {2, 3, 1, 1, 4};
// int[] nums = {2, 0,1,2};
int[] nums = {1, 1,1,1};
System.out.println(jump(nums));
}
/**
* 思路:题设说一定能到;首先用curCover记录当前能覆盖到的位置maxIndex记录最大的如果i大过curCover的话就需要加一步
* @param nums
* @return
*/
public int jump(int[] nums) {
if(nums.length==1)return 0;
int maxIndex=0;//最大能覆盖的位置
int curCover=0;//当前没变时能覆盖的位置
int step=0;
for (int i = 0; i < nums.length; i++) {
if(i>curCover) {
step++;
curCover=maxIndex;
}
maxIndex=Math.max(maxIndex,i+nums[i]);
if(maxIndex>=nums.length-1)return step+1;//从这里直接跨到最后
}
return step+1;
}
}