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 ...
随机推荐
- 小小c#算法题 - 6 - 快速排序 (QuickSort)
快速排序是排序算法中效率比较高的一种,也是面试常被问到的问题. 快速排序(Quick Sort)是对冒泡排序的一种改进.它的基本思想是,通过一趟排序将待排记录分割成独立的两部分,其中一部分记录的关键字 ...
- Object—C 块在函数中作为参数时的分析
暂时对这个有了一些粗浅的理解,记下来一边后面学习时学习,改正. 先举个例子: A类: .h文件: @interface A : NSObject - (void)Paly1:(void (^)(do ...
- PHP框架学习思路
希望可以帮助到正在学习的PHPer
- 洛谷P1607 [USACO09FEB]庙会班车Fair Shuttle
P1607 [USACO09FEB]庙会班车Fair Shuttle 题目描述 Although Farmer John has no problems walking around the fair ...
- C# Stack堆栈的使用方法
堆栈(Stack)代表了一个后进先出的对象集合.当您需要对各项进行后进先出的访问时,则使用堆栈.当您在列表中添加一项,称为推入元素,当您从列表中移除一项时,称为弹出元素. Stack 类的方法和属性 ...
- Nacos深入浅出(十)
基本上到第9篇,整个请求的一套就结束了,感觉这里跳跳绕绕很多东西,下面我们来做个总结:从Nacos配置平台修改,到Client请求更新,事件触发去取值返回给客户端,整个过程感觉只分析到了4.5层的深度 ...
- 3、kubernetes应用快速入门190625
一.kubernetes应用入门 1.kubectl命令 Basic Commands create Create a resource from a file or from stdin. expo ...
- PHPExcel探索之旅---阶段四 导入文件
步骤就是:实例化excel读取对象=> 加载excel文件 => 读取excel文件(全部读取.逐行读取) <?php header("Content Type :text ...
- Luogu P2455 [SDOI2006]线性方程组 真•高斯消元板子
果然如Miracle学长所说...调了一天...qwq..还是过不了线下的Hack upd after 40min:刚刚过了 就是多了一个判无解的操作... 当系数都为0,且常数项不为0时,即为无解. ...
- 从左到右的滑块-Au3
说明:使用selenium自动化登陆时会弹出滑块,做人机识别,使用Au3模拟鼠标移动轨迹--可验证通过 #include <MsgBoxConstants.au3> #include &l ...