首页 > 其他分享 >day49 121. 买卖股票的最佳时机 |

day49 121. 买卖股票的最佳时机 |

时间:2023-04-19 10:22:54浏览次数:40  
标签:int 股票 121 length 最佳时机 所得 day49 prices dp

给定一个数组 prices ,它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格。

你只能选择 某一天 买入这只股票,并选择在 未来的某一个不同的日子 卖出该股票。设计一个算法来计算你所能获取的最大利润。

返回你可以从这笔交易中获取的最大利润。如果你不能获取任何利润,返回 0 。

如果第i天持有股票即dp[i][0], 那么可以由两个状态推出来

  • 第i-1天就持有股票,那么就保持现状,所得现金就是昨天持有股票的所得现金 即:dp[i - 1][0]
  • 第i天买入股票,所得现金就是买入今天的股票后所得现金即:-prices[i]

那么dp[i][0]应该选所得现金最大的,所以dp[i][0] = max(dp[i - 1][0], -prices[i]);

如果第i天不持有股票即dp[i][1], 也可以由两个状态推出来

class Solution {
    public int maxProfit(int[] prices) {
        if (prices == null || prices.length == 0) return 0;
        int length = prices.length;
        int[][] dp = new int[length][2];
        int res = 0;
        dp[0][0] = -prices[0];
        dp[0][1] = 0;

        for (int i = 1; i < length; i++) {
            dp[i][0] = Math.max(dp[i - 1][0], -prices[i]);
            dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] + prices[i]);
        }
        return dp[length - 1][1];
    }
}

给你一个整数数组 prices ,其中 prices[i] 表示某支股票第 i 天的价格。

在每一天,你可以决定是否购买和/或出售股票。你在任何时候 最多 只能持有 一股 股票。你也可以先购买,然后在 同一天 出售。

返回 你能获得的 最大 利润 。

如果第i天持有股票即dp[i][0], 那么可以由两个状态推出来

  • 第i-1天就持有股票,那么就保持现状,所得现金就是昨天持有股票的所得现金 即:dp[i - 1][0]
  • 第i天买入股票,所得现金就是昨天不持有股票的所得现金减去 今天的股票价格 即:dp[i - 1][1] - prices[i]
class Solution {
    public int maxProfit(int[] prices) {
        int n = prices.length;
        int[][] dp = new int[n][2];
        dp[0][0] = 0;
        dp[0][1] = -prices[0];

        for (int i = 1; i < n; i++) {
            dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i]);    // 第 i 天,没有股票
            dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i]); 
        }
        return dp[n - 1][0];
    }
}

 

标签:int,股票,121,length,最佳时机,所得,day49,prices,dp
From: https://www.cnblogs.com/libertylhy/p/17332360.html

相关文章

  • day49(2023.4.18)
    1.MySQL事务 2.使用事务 3.事务的并发问题 4.事务的隔离级别 5.用户管理 6.使用Navicat创建用户  7.使用Navicat分配权限8.测试一下分配好的权限 9.删除用户 10.数据的导出 11.分页查询  day49(2023.4.18)......
  • 2-209-(LeetCode-121) 买卖股票的最佳时机
     1.题目 https://leetcode.cn/problems/best-time-to-buy-and-sell-stock/121. 买卖股票的最佳时机 2.解法2.1解法一:动态规划  2.2解法二:非动态规划 if(prices.length<2){return0;}intmin=prices[0];......
  • Another Crisis UVA - 12186
      #include<iostream>#include<cstring>#include<vector>#include<algorithm>usingnamespacestd;constintN=1e5+3;intf[N],n,K;vector<int>g[N];voiddfs(intx){ intb[N],tot; tot=0; if(g[x].size()==0)......
  • UVa 11210 Chinese Mahjong (模拟&枚举&回溯)
    11210-ChineseMahjongTimelimit:3.000secondshttp://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=2151Mahjong()isagameofChineseoriginusuallyplayedbyfourpersonswithtilesresemblingdominoesandbearing......
  • 力扣 121. 买卖股票的最佳时机 122. 买卖股票的最佳时机 II
    121.买卖股票的最佳时机给定一个数组 prices ,它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格。你只能选择 某一天 买入这只股票,并选择在 未来的某一个不同的日子 卖出该股票。设计一个算法来计算你所能获取的最大利润。返回你可以从这笔交易中获......
  • Sum of Consecutive Prime Numbers UVA - 121
     #include<iostream>#include<cstring>#include<cmath>#include<algorithm>usingnamespacestd;constintM=1e4+33;intb[M],c[M],tot;ints[M];voidinit(inttop){memset(b,1,sizeofb);b[1]=0;inti,j;......
  • 1212
    STM32中BOOT0BOOT1设置(问题:程序下载进去但无法运行)默认BOOT0接10K接地,BOOT1接  10K接地实际如果BOOT0不接10K到地,会导致程序能下载进去,但是无法运行情况......
  • Sum of Different Primes UVA - 1213
     选择K个质数,使它们的和等于N。问有多少种方案?例如,n=24,k=2时有3种方案:5+19=7+17=11+13=24 #include<iostream>#include<cstring>#include<cmath>#include<algorithm>usingnamespacestd;constintM=1e6+33;intb[M],c[M],tot;intn,m,f[2000][20];......
  • 1210. 穿过迷宫的最少移动次数
    题目链接:1210.穿过迷宫的最少移动次数参考:还在if-else?一个循环处理六种移动!代码classSolution{private:staticconstexprintmov[3][3]={{1,0,0},{0,1,0},{0,0,1}};//下、右、旋转public:intminimumMoves(vector<vector<int>>&grid){......
  • Keil Error L121: Improper Fixup解决
    参考链接:ErrorL121:ImproperFixup(silabs.com)主要问题应该是程序太大,可以尽量缩小程序大小,实在不行的话改为Large即可。从小型2K改为大型64K,不再报错。 ......