LeetCode OJ:Best Time to Buy and Sell Stock II(股票买入卖出最佳实际II)
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
对贪心不是很了解,这题跟上一个类似的题目相差还是有点大的,没想出来,看了别人的实现。实际上思路很简单,只要一直在涨就不动,一旦发现跌了之后就将前一个值记录下来之后求出与买入时候的差值,重复进行下一轮的买入以及卖出,代码如下:
class Solution {
public:
int maxProfit(vector<int>& prices) {
int sz = prices.size();
if(!sz) return ;
int profit = ;
int start = ;
for(int i = ; i < sz; ++i){
if(prices[i] >= prices[i - ])
continue;
profit += prices[i - ] - prices[start];
start = i;
}
profit += prices[sz - ] - prices[start];
return profit;
}
};
LeetCode OJ:Best Time to Buy and Sell Stock II(股票买入卖出最佳实际II)的更多相关文章
- [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 ...
- [LeetCode OJ] Best Time to Buy and Sell Stock I
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- LeetCode OJ - Best Time to Buy and Sell Stock
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/xiezhihua120/article/details/32939749 Say you have ...
- [LeetCode] 122. Best Time to Buy and Sell Stock II 买卖股票的最佳时间 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] 123. Best Time to Buy and Sell Stock III 买卖股票的最佳时间 III
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] 188. Best Time to Buy and Sell Stock IV 买卖股票的最佳时间 IV
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] 309. Best Time to Buy and Sell Stock with Cooldown 买卖股票的最佳时间有冷却期
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】
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 买卖股票的最佳时间
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
随机推荐
- Python面向对象高级编程-_slots_
使用_slots_ 正常情况下,当定义一个class,创建一个class的实例后,可以给实例绑定任何属性和方法,这就是动态语言的灵活性.先定义class: >>> class Stu ...
- django下的csrf防御机制
CSRF 1.什么是CSRF? CSRF(Cross-site request forgery),中文名称:跨站请求伪造,也被称为:one click attack/session riding,缩写 ...
- HDFS各个进程存储在磁盘上的数据含义和注意事项
本文地址:http://www.cnblogs.com/qiaoyihang/p/6293402.html (一)Namenode的目录结构 HDFS进行初次格式化之后将会在$dfs.namenode ...
- go——基本类型
Go有许多预定义类型,这里简单把它们分为基本类型和高级类型.下面是基本类型列表: Go的基本类型共有18个,其中int和uint的实际宽度会根据计算架构的不同而不同.在386计算架构下,它的宽度为32 ...
- ArcGis 获取数据表中某字段唯一值
from:http://www.cnblogs.com/3echo/archive/2006/08/16/478094.html 1 /// <summary> ...
- Wannafly交流赛1 _A_有理数 【水】
Wannafly交流赛1 A有理数 [水] 链接:https://www.nowcoder.com/acm/contest/69/A 来源:牛客网 题目描述 有一个问题如下: 给你一个有理数v,请找到 ...
- Entity FrameWork 配置 之连接字符串隐藏或重用
C/S项目中使用EF,默认回生成app.config文件夹,软件打包安装成功之后就回生成一个对应exe.config.里面会包含配置的一些信息. 这里介绍给大家一种隐藏连接字符串的方式. 代码如下: ...
- jQuery带小图标的Tab切换焦点图
在线演示 本地下载
- Javascript何时执行
分以下两种情况: 1.HTML head部分的Javascript会在被调用的时候执行 需要调用才执行的脚本或事件触发执行的脚本放在head部分,这可以保证脚本在任何调用之前被预先加载,在页面加载完之 ...
- 20145219 《Java程序设计》实验二 Java面向对象程序设计实验报告
20145219 <Java程序设计>实验二 Java面向对象程序设计实验报告 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S. ...