问题

假设你有一个数组,其中的第i个元素表示一只股票在第i天的价格。

如果只允许你完成一次交易(即买入并卖出股票一次),设计一个找出最大利润的算法。

初始思路

和122一样,基于买入与卖出股票的最佳时机III中的分析很容易得出答案。由于只允许进行一次交易,本题更加简单,我们只需按III中的方法不断更新最大利润即可。

 class Solution {
public:
int maxProfit(std::vector<int> &prices)
{
return CaculateProfit(prices).profit;
} private:
struct Profit
{
Profit() : profit(), buyPrice(-), buyDay(), sellDay()
{
} int profit;
int buyPrice;
int buyDay;
int sellDay;
}; Profit CaculateProfit(std::vector<int> &prices)
{
Profit currentProfit;
Profit maxProfit; for(int day = ; day < prices.size(); ++day)
{
if(currentProfit.buyPrice == -)
{
currentProfit.buyPrice = prices[day];
currentProfit.buyDay = day;
continue;
} currentProfit.profit = prices[day] - currentProfit.buyPrice;
currentProfit.sellDay = day; if(currentProfit.profit < )
{
currentProfit.buyPrice = prices[day];
currentProfit.buyDay = day;
currentProfit.profit = ;
} if(currentProfit.profit > maxProfit.profit)
{
maxProfit = currentProfit;
}
} return maxProfit;
}
};

maxProfit

[LeetCode 121] - 买入与卖出股票的最佳时机(Best Time to Buy and Sell Stock)的更多相关文章

  1. [LeetCode 122] - 买入与卖出股票的最佳时机II(Best Time to Buy and Sell Stock II)

    问题 假设你有一个数组,其中的第i个元素表示一只股票在第i天的价格. 设计一个算法找出最大的利润值.你可以进行任意多次的交易(即多次的卖出并买入一份股票).你不能在同一时间进行多次交易(即你必须在再次 ...

  2. 【LEETCODE】37、122题,Best Time to Buy and Sell Stock II

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

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

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

  4. 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. ...

  5. 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 ...

  6. 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. ...

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

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

  8. 121. Best Time to Buy and Sell Stock (一) leetcode解题笔记

    121. Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of ...

  9. [LeetCode] 121. Best Time to Buy and Sell Stock 买卖股票的最佳时间

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

随机推荐

  1. 【转】文件同步软件FreeFileSync

    原文网址:http://imcn.me/html/y2012/9855.html FreeFileSync 是一款开源的文件夹比较和同步工具,可用于 Win 和 Lin 平台,最近发布了 5.0 版本 ...

  2. delphi编写dll心得, 谢谢原作者的分享。转

    delphi编写dll心得 1.每个函数体(包括exports和非exports函数)后面加 'stdcall;', 以编写出通用的dll2.exports函数后面必须加'export;'(放在'st ...

  3. HDU_2018——母牛产小牛的问题,递推

    Problem Description 有一头母牛,它每年年初生一头小母牛.每头小母牛从第四个年头开始,每年年初也生一头小母牛.请编程实现在第n年的时候,共有多少头母牛?   Input 输入数据由多 ...

  4. Codeforces Round #300 F - A Heap of Heaps (树状数组 OR 差分)

    F. A Heap of Heaps time limit per test 3 seconds memory limit per test 512 megabytes input standard ...

  5. socket中select的使用源码

    下面的代码来自IBM学习网站,是学习socket通信和select使用的一个很好的源码. server.c  服务器端 #include <stdio.h> #include <st ...

  6. java中paint repaint update 之间的关系

    最近总结了一下java中的paint,repaint和updata三者之间的关系,首先咱们都知道用paint方法来绘图,用repaint重绘,用update来写双缓冲.但是他们之间是怎么来调用的呢,咱 ...

  7. wpf下拉框不能多选的原因

    <dxe:ComboBoxEdit Margin="0"  Height="25" Width="65" VerticalAlignm ...

  8. [置顶] 对于最新的Android病毒,Smack完全可以抵御

    我写的有关Smack和Android系统结合的技术博客,希望有志之士可以参透其中奥妙,Smack作为Linux内核安全模块,已经可以移植到Android系统中,如果大家弄清我写的Smack安全策略,可 ...

  9. Linux驱动设备中的并发控制

    一.基本概念 二.中断屏蔽 三.原子操作 四.自旋锁 五.信号量 六.互斥体 七.自旋锁与信号量的比较 Linux设备驱动中必须解决的一个问题是多个进程对共享资源的并发访问,并发的访问会导致竞态,即使 ...

  10. linux od命令

    用户通常使用od命令查看特殊格式的文件内容.通过指定该命令的不同选项可以以十进制.八进制.十六进制和ASCII码来显示文件.od命令系统默认的显示方式是八进制,这也是该命令的名称由来(Octal Du ...