[LeetCode 121] - 买入与卖出股票的最佳时机(Best Time to Buy and Sell Stock)
问题
假设你有一个数组,其中的第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)的更多相关文章
- [LeetCode 122] - 买入与卖出股票的最佳时机II(Best Time to Buy and Sell Stock II)
问题 假设你有一个数组,其中的第i个元素表示一只股票在第i天的价格. 设计一个算法找出最大的利润值.你可以进行任意多次的交易(即多次的卖出并买入一份股票).你不能在同一时间进行多次交易(即你必须在再次 ...
- 【LEETCODE】37、122题,Best Time to Buy and Sell Stock II
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- Leetcode之动态规划(DP)专题-121. 买卖股票的最佳时机(Best Time to Buy and Sell Stock)
Leetcode之动态规划(DP)专题-121. 买卖股票的最佳时机(Best Time to Buy and Sell Stock) 股票问题: 121. 买卖股票的最佳时机 122. 买卖股票的最 ...
- 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. ...
- 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 ...
- 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. ...
- 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 ...
- 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 ...
- [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 ...
随机推荐
- 介绍一款超实用的演讲必备工具 ZoomIt
最近在整理以前的文件时,发现躺在某个角落的一个超好的实用的小工具——ZoomIt.此工具是 SysinternalsSuite(已被MS收购)众多实用工具的一个. 相信大家在演讲时,经常需要调整屏幕文 ...
- jsp中导入导出excel,ssh框架
导入Excel:jsp中 <form action="user_importTradingMoney" enctype="multipart/form-data&q ...
- Android笔记(二):从savedIndstanceState发散
savedIndstanceState savedIndstanceState位于ActivityonCreate(Bundle savedInstanceState)方法的参数中.对这个参数的理解要 ...
- Linux下的bc计算器
bc = basic calculator scale:设置精度,默认为0 obase:设置输出进制,默认为10 ibase:设置输入进制,默认为10 原文:http://www.linuxidc.c ...
- asdasd
adasdasd asdasd asdasd asd
- python socket实例练习
Web Server是基于Socket编程,又称之为网络编程,socket是网络编程接口,socket可以建立网络连接,读数据,写数据.socket模块定义了一些常量参数,用来指定socket的的地址 ...
- 详细介绍android rom移植知识普及
详细介绍android rom移植知识普及 最近接到很多兄弟们的求助,也回答过无数个和下面这个问题类似的问题: 如何编译android 原生代码得到一个rom,然后跑到某某手机上. 鉴于很多兄弟对这块 ...
- [置顶] .net技术类面试、笔试题汇总3
今天本人从成都回到了学校,深刻认识了自己存在很多不足,在这段期间会更加努力,争取早日找到一个好工作! 41.在ASP.NET中有Button控件myButton,要是单击控件时,导航到其他页面http ...
- Android应用开发-小巫CSDN博客client之嵌入有米广告
Android应用开发-小巫CSDN博客client之嵌入有米广告 上一篇博客给大家介绍怎样集成友盟社会化组件,本篇继续带来干货,教大家怎样嵌入广告到应用中去.小巫自称专业对接30年,熟悉各大渠道SD ...
- repo简介
高通的Android JellyBean库已经建立完成,目前已经可以正常访问,可以正常提交.Repo获取代码注意:1. 首先应有一个repo脚本,并将脚本放到环境变量里,此脚本的配置方法a) 进入用户 ...