Leetcode之动态规划(DP)专题-714. 买卖股票的最佳时机含手续费(Best Time to Buy and Sell Stock with Transaction Fee)

股票问题:

121. 买卖股票的最佳时机

122. 买卖股票的最佳时机 II

123. 买卖股票的最佳时机 III

188. 买卖股票的最佳时机 IV

309. 最佳买卖股票时机含冷冻期

714. 买卖股票的最佳时机含手续费


给定一个整数数组 prices,其中第 i 个元素代表了第 i 天的股票价格 ;非负整数 fee 代表了交易股票的手续费用。

你可以无限次地完成交易,但是你每次交易都需要付手续费。如果你已经购买了一个股票,在卖出它之前你就不能再继续购买股票了。

返回获得利润的最大值。

示例 1:

输入: prices = [1, 3, 2, 8, 4, 9], fee = 2
输出: 8
解释: 能够达到的最大利润:
在此处买入 prices[0] = 1
在此处卖出 prices[3] = 8
在此处买入 prices[4] = 4
在此处卖出 prices[5] = 9
总利润: ((8 - 1) - 2) + ((9 - 4) - 2) = 8.

注意:

  • 0 < prices.length <= 50000.
  • 0 < prices[i] < 50000.
  • 0 <= fee < 50000.

DP含义:

dp[i][0]表示第i天没有股票时的最大利润,没有股票的原因可能是:

  • 第i-1天就没有,第i天没有买入
  • 第i-1天有股票,第i天把它卖了

dp[i][1]表示持有股票时的最大利润,持有股票的原因可能有:

  • 第i-1天就有,第i天没有卖出
  • 第i-1天没股票,第i天买入了

初始条件:

dp[0][0]=0,因为第0天没有买入,所以利润为0.

dp[0][1]=-prices[0],第0天买入了第0支股票,利润为0-prices[0]

返回值:

dp[prices.length-1][0],因为要求最后手里不得持有股票,所以返回不持有股票时的利润最大值。

状态转移方程:

dp[i][0] = Math.max(dp[i-1][0],prices[i]+dp[i-1][1]-fee);
dp[i][1] = Math.max(dp[i-1][1],dp[i-1][0]-prices[i]);

class Solution {
public int maxProfit(int[] prices, int fee) {
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],prices[i]+dp[i-1][1]-fee);
dp[i][1] = Math.max(dp[i-1][1],dp[i-1][0]-prices[i]);
}
return dp[prices.length-1][0];
}
}

简化一下:

class Solution {
public int maxProfit(int[] prices, int fee) {
int cash = 0;
int hold = -prices[0];
for (int i = 1; i < prices.length; i++) {
cash = Math.max(cash,hold+prices[i]-fee);
hold = Math.max(hold,cash-prices[i]);
}
return cash; }
}

Leetcode之动态规划(DP)专题-714. 买卖股票的最佳时机含手续费(Best Time to Buy and Sell Stock with Transaction Fee)的更多相关文章

  1. [Swift]LeetCode714. 买卖股票的最佳时机含手续费 | Best Time to Buy and Sell Stock with Transaction Fee

    Your are given an array of integers prices, for which the i-th element is the price of a given stock ...

  2. Leetcode之动态规划(DP)专题-122. 买卖股票的最佳时机 II(Best Time to Buy and Sell Stock II)

    Leetcode之动态规划(DP)专题-122. 买卖股票的最佳时机 II(Best Time to Buy and Sell Stock II) 股票问题: 121. 买卖股票的最佳时机 122. ...

  3. Leetcode之动态规划(DP)专题-123. 买卖股票的最佳时机 III(Best Time to Buy and Sell Stock III)

    Leetcode之动态规划(DP)专题-123. 买卖股票的最佳时机 III(Best Time to Buy and Sell Stock III) 股票问题: 121. 买卖股票的最佳时机 122 ...

  4. Leetcode之动态规划(DP)专题-188. 买卖股票的最佳时机 IV(Best Time to Buy and Sell Stock IV)

    Leetcode之动态规划(DP)专题-188. 买卖股票的最佳时机 IV(Best Time to Buy and Sell Stock IV) 股票问题: 121. 买卖股票的最佳时机 122. ...

  5. Java实现 LeetCode 714 买卖股票的最佳时机含手续费(动态规划 || 迭代法)

    714. 买卖股票的最佳时机含手续费 给定一个整数数组 prices,其中第 i 个元素代表了第 i 天的股票价格 :非负整数 fee 代表了交易股票的手续费用. 你可以无限次地完成交易,但是你每次交 ...

  6. LeetCode——714. 买卖股票的最佳时机含手续费.

    给定一个整数数组 prices,其中第 i 个元素代表了第 i 天的股票价格 :非负整数 fee 代表了交易股票的手续费用. 你可以无限次地完成交易,但是你每次交易都需要付手续费.如果你已经购买了一个 ...

  7. leetcode 714. 买卖股票的最佳时机含手续费

    继承leetcode123以及leetcode309的思路,,但应该也可以写成leetcode 152. 乘积最大子序列的形式 class Solution { public: int maxProf ...

  8. LeetCode 122. 买卖股票的最佳时机 II(Best Time to Buy and Sell Stock II)

    题目描述 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你可以尽可能地完成更多的交易(多次买卖一支股票). 注意:你不能同时参与多笔交易(你 ...

  9. 【LeetCode】714、买卖股票的最佳时机含手续费

    Best Time to Buy and Sell Stock with Transaction Fee 题目等级:Medium 题目描述: Your are given an array of in ...

随机推荐

  1. BZOJ 3744 Gty的妹子序列 做法集结

    我只会O(nnlogn)O(n\sqrt nlogn)O(nn​logn)的 . . . . 这是分块+树状数组+主席树的做法O(nnlogn)O(n\sqrt nlogn)O(nn​logn) 搬来 ...

  2. BZOJ 3439: Kpm的MC密码 (trie+dfs序主席树)

    题意 略 分析 把串倒过来插进trietrietrie上, 那么一个串的kpmkpmkpm串就是这个串在trietrietrie上对应的结点的子树下面的所有字符串. 那么像 BZOJ 3551/354 ...

  3. vue 日期格式化过滤器

    formateDate日期格式化,写在公共的js中: export function formateDate(date, fmt){ if ('/(y+)/'.test(fmt){ fmt = fmt ...

  4. SecureFX中文目录乱码问题解决方案

    1.点击菜单栏中Options 2.找到General下的Configuration Paths并点击 3.在我的电脑打开 右面视图Configuration data is stored in th ...

  5. 小米 oj 纯位数

     纯位数 序号:#101难度:非常难时间限制:2000ms内存限制:20M 描述 在数学中,所谓"纯位数"是指由相同位元重复而组成的自然数.比如在十进制中,1,22,333,555 ...

  6. jQuery系列(二):jQuery的选择器

    css中的选择器有:

  7. [python之ipython] jupyter notebook在云端服务器上开启,本地访问

    本地ssh到云端: ssh username@xxx.xxx.xxx.xxx -L127.0.0.1:7777:127.0.0.1:8888 把云端的8888端口映射到本地的7777端口 云端运行指令 ...

  8. tensorflow实现siamese网络 (附代码)

    转载自:https://blog.csdn.net/qq1483661204/article/details/79039702 Learning a Similarity Metric Discrim ...

  9. 数据结构实验之链表六:有序链表的建立(SDUT 2121)

    #include <bits/stdc++.h> using namespace std; struct node { int data; struct node *next; }; in ...

  10. python 字符串模板

    from string import Template print(type(Template)) mystr = Template("hi,$name 你是$baby") pri ...