LeetCode Best Time to Buy and Sell Stock II (简单题)
题意:
股票买卖第2题。给出每天的股票价格,每次最多买一股,可以多次操作,但是每次在买之前必须保证身上无股票。问最大的利润?
思路:
每天的股票价格可以看成是一条曲线,能卖掉就卖掉,那么肯定是在上升的时候就可以卖掉,但是在不卖的时候要保证自己身上的那只股票的价格是最低价买进的。
class Solution {
public:
int maxProfit(vector<int>& prices)
{
int pre=, ans=;
for(int i=; i<prices.size(); i++)
{
if( pre< && pre<prices[i] )
{
ans+=prices[i]-pre;
pre=prices[i];
}
else
pre=min(pre,prices[i]);
}
return ans;
}
};
AC代码
LeetCode Best Time to Buy and Sell Stock II (简单题)的更多相关文章
- LEETCODE —— Best Time to Buy and Sell Stock II [贪心算法]
Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a ...
- Algorithm - 贪心算法使用场景 ( LEETCODE —— Best Time to Buy and Sell Stock II)
先看一道leetcode题: Best Time to Buy and Sell Stock II Say you have an array for which the ith element is ...
- LeetCode: Best Time to Buy and Sell Stock II 解题报告
Best Time to Buy and Sell Stock IIQuestion SolutionSay you have an array for which the ith element i ...
- [LeetCode] Best Time to Buy and Sell Stock II 买股票的最佳时间之二
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- LeetCode——Best Time to Buy and Sell Stock II (股票买卖时机问题2)
问题: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
- [leetcode]Best Time to Buy and Sell Stock II @ Python
原题地址:https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ 题意: Say you have an array ...
- [LeetCode] Best Time to Buy and Sell Stock II
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- LeetCode: Best Time to Buy and Sell Stock II [122]
[题目] Say you have an array for which the ith element is the price of a given stock on day i. Design ...
- LeetCode——Best Time to Buy and Sell Stock II
Description: Say you have an array for which the ith element is the price of a given stock on day i. ...
- [Leetcode] Best time to buy and sell stock ii 买卖股票的最佳时机
Say you have an array for which the i th element is the price of a given stock on day i. Design an a ...
随机推荐
- matlab 矩阵运算技巧
1.a=a(:) 作用:将矩阵转化成列向量 a=[a11 a12 a13 a=[a11 a21 a12 a22 a13 a23]^T a21 a22 a23] ...
- C# EventHandler委托事件小结--百度
最近遇到一个委托的问题,+=这个符号 this.Activated += new EventHandler(Form1_Activated);//Form1_Activated为方法名12 这个语句拆 ...
- PCB生产企业自动化立体仓库/智能仓库库系统WMS/WCS解决方案
PCB生产企业自动化立体仓库/智能仓库库系统WMS/WCS解决方案 自动化立体仓库智能仓储系统WMS/WCS重要性调查 调查1(物流成本占总生产成本比例数据)1979年英国的第一次调查表明,在从原材料 ...
- win10移动热点问题
1.问题(win10移动热点相关) 具体描述: win10通过网线连接上网,打开移动热点后手机无法连接. 如下图所示,win10打开热,然后进入设置界面设置wlan名称和密码,手机填好密码,连接热点发 ...
- emmet中的用法
CSS Abbreviations Link VALUES LINK Emmet is about more than just HTML elements. You can inject value ...
- Python网络编程(一)
最近在啃<python核心编程(第三版)>,感觉这本书并不是特别的友好,虽然有基于python3提出的改进代码:但是整书的基准感觉还是在python2.7.所以python3的代码中还是有 ...
- Docker从入门到实战(四)
Docker基础 一:Docker基本操作 一般情况安装Docker之后系统会自动创建一个Docker的用户组,如果没有创建可以手动创建groupadd docker把当前非root用户加入group ...
- spring boot与 spring.factories
spring boot启动加载过程 META-INF下面的spring.factories 解析@Configuration https://www.jianshu.com/p/346cac67bfc ...
- 【ElasticSearch+NetCore 第一篇】在Windows上安装部署ElasticSearch和ElasticSearch-head
ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.Elasticsearch是用Java开发的,并作为Apach ...
- HttpResponseCache的使用缓存cache
为什么要用cache? 我们可以通过传递类似上次更新时间这样的参数来制定查询某些数据.同样,在下载图片的时候,server那边最好能够减少图片的大小,而不是让我们下载完整大小的图片. 之前我们在软件开 ...