LeetCode122—Best Time to Buy and Sell Stock II
2016-04-21 10:47
411 查看
LeetCode122—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 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).
不限买卖次数,但是每天只能一次操作。
分析
这题就简单多了,既然不限买卖次数,那么只要第i+1天的价格比第i天的价格要高,这样就可以交易。代码
class Solution { public: int maxProfit(vector<int>& prices) { int curMax = 0; for (int i = 1; i < prices.size(); i++) { if (prices[i] - prices[i - 1] > 0) curMax = curMax + (prices[i] - prices[i - 1]); } return curMax; } };
相关文章推荐
- Json概述以及python对json的相关操作
- 206. Reverse Linked List
- CSS中position的absolute如何相对于父元素的位置进行定位
- java中Executor、ExecutorService、ThreadPoolExecutor介绍
- ViewPager+Fragment取消预加载功能
- 简单的基于YCrCb颜色空间的人脸检测(肤色)
- vs中如何使用NuGet
- jdk8 tomcat7
- 使用Post方式提交数据到Tomcat服务器的方法
- 自定义导航控制器返回按钮
- 关于http协议在windos 的vs下面正常运行,在Mac下的xcode运行不报错,但不成功的问题
- 过TP保护与解除游戏驱动保护
- 在幼儿园管理系统中,批量导入(file)想美化成一个按钮来实现。
- java的Timer时间
- android 三种定位方式
- 使用反射通过字符串动态调用函数
- Springx b 界面
- python unicode 转中文 遇到的问题 爬去网页中遇到编码的问题
- IOS开发--上传图片
- 203. Remove Linked List Elements