当前位置: 首页 > 编程笔记 >

通过最多两次买卖股票获得最大利润

伍昱
2023-03-14
本文向大家介绍通过最多两次买卖股票获得最大利润,包括了通过最多两次买卖股票获得最大利润的使用技巧和注意事项,需要的朋友参考一下

在事务中,一个买主分别在早上和晚上买卖股票。如果一天最多允许两次事务。第二个事务只能在第一个事务完成后才能开始。如果给出了股票价格,则找到买方可以赚到的最大利润。

输入输出

Input:
A list of stock prices. {2, 30, 15, 10, 8, 25, 80}
Output:
Here the total profit is 100. As buying at price 2 and selling at price 30.
so profit 28. Then buy at price 8 and sell it again at price 80.
So profit 72. So the total profit 28 + 72 = 100

算法

findMaxProfit(pricelist, n)

输入-所有价格的列表,列表中的物品数量。

输出-最大利润。

Begin
   define profit array of size n and fill with 0
   maxPrice := pricelist[n-1]          //last item is chosen

   for i := n-2 down to 0, do
      if pricelist[i] > maxPrice, then
         maxPrice := pricelist[i]
      profit[i] := maximum of profit[i+1] and maxProfit – pricelist[i]
   done

   minProce := pricelist[0]           //first item is chosen
   for i := 1 to n-1, do
      if pricelist[i] < minPrice, then
         minPrice := pricelist[i]
      profit[i] := maximum of profit[i-1] and (profit[i]+(pricelist[i] - minPrice))
   done

   return profit[n-1]
End

示例

#include<iostream>
using namespace std;

int max(int a, int b) {
   return (a>b)?a:b;
}

int findMaxProfit(int priceList[], int n) {
   int *profit = new int[n];
   for (int i=0; i<n; i++)            //initialize profit list with 0
      profit[i] = 0;

   int maxPrice = priceList[n-1];        //initialize with last element of price list

   for (int i=n-2;i>=0;i--) {
      if (priceList[i] > maxPrice)
         maxPrice = priceList[i];

      profit[i] = max(profit[i+1], maxPrice - priceList[i]);     //find the profit for selling in maxPrice
   }

   int minPrice = priceList[0];            //first item of priceList as minimum

   for (int i=1; i<n; i++) {
      if (priceList[i] < minPrice)
         minPrice = priceList[i];

      profit[i] = max(profit[i-1], profit[i] + (priceList[i]- minPrice) );
   }

   int result = profit[n-1];
   return result;
}

int main() {
   int priceList[] = {2, 30, 15, 10, 8, 25, 80};
   int n = 7;
   cout << "Maximum Profit = " << findMaxProfit(priceList, n);
}

输出结果

Maximum Profit = 100
 类似资料:
  • 本网站提出的问题:https://www.interviewbit.com/problems/best-time-to-buy-and-sell-stocks-iii/ 假设你有一个数组,其中第i个元素是给定股票在第i天的价格。 设计一个算法来寻找最大利润。您最多可以完成两笔交易。 注意:你不能同时进行多笔交易(即,你必须在再次购买之前卖出股票)。 我的解决方案: 我的想法是跟踪所有利润(我以当地

  • 有一个经典的面试问题,即利润最大化,允许一次交易、n次交易和k次交易购买股票。 有人问我一个类似的问题,但有一个扭曲的限制:你可以购买一只股票任意次数(任何一天不超过一个单位),但你不能在卖出股票后购买。 这有一个引理,你只卖一次。 例:70409011080100 选项1:B卖出=130 选项2:B X B卖出=120 老问题 https://www.geeksforgeeks.org/stoc

  • 题目链接 Leetcode:121. Best Time to Buy and Sell Stock 题目描述 可以有一次买入和一次卖出,买入必须在前。求最大收益。 解题思路 使用贪心策略,假设第 i 轮进行卖出操作,买入操作价格应该在 i 之前并且价格最低。 // java public int maxProfit(int[] prices) { if (prices == null |

  • 尝试解决这个问题:假设您有一个数组,其中第i个元素是给定股票在第i天的价格。 设计一个算法来寻找最大利润。您最多可以完成两笔交易。 解决方案:我正在做的是分而治之的方法。 dp[i][j]是ith和jth day之间的最大利润。计算如下: dp[i][j]=max(dp[i][j],max(prices[i]-prices[j],dp[k][j],dp[i][k1]),其中k小于i且大于j。 现在

  • 本文向大家介绍基于java计算买卖股票的最佳时机,包括了基于java计算买卖股票的最佳时机的使用技巧和注意事项,需要的朋友参考一下 这篇文章主要介绍了基于java计算买卖股票的最佳时机,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 问题: 可以将问题转化为如下图所示,即求多个累计的收入差 分析: 如果当前位置i的价格比i+1的价格高,则当前不是买

  • 给你n天的股票价格。通过股票交易获得最大利润。你每天最多只能交易一次:每天你可以选择购买一只股票,或者卖出一只股票(如果你有),或者放弃当天的交易,什么都不做。 给定,返回 解释: 你可以在第一天和第二天购买,在第三天和第四天出售。 利润:-1-2109=16 给定,返回 解释: 你可以在第二天买入,第四天卖出。 利润:-5 10=5 困难的部分是,你可以进行连续的买入和/或卖出,这意味着一旦你持

  • 有人给了我一个课本上的问题,我弄不懂。它是: 假设您有一只股票STOK,您打算将所有资金投资一个月(天),到月底您无法持有任何股票。您有钱。对于任何一天STOK的价格都是,并且在任何一天您都可以买卖股票。但是,您一天可以买卖多少股票是有限制的(买卖是一样的)。如果您愿意,您可以购买非整数单位的股票,以便于计算。鉴于这些功能,您如何安排购买计划以最大化您的利润? 天真的解决方案:每天在以下限制条件下

  • 我了解用交易费购买和出售股票的最佳时间的解决方案,以及与股票出售相关的其他5个问题。我只想深入了解如何在问题中提出这种递归关系。 我读过https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/discuss/108870/most-consistent-ways-of-dealing-wi