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 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).
这道题是Best Time to Buy and Sell Stock I问题的第二个版本,题目整体的要求没有变,和I一样,只是在这里不再只限一次交易,此题中可以多次交易,只是买之前必须先卖出去,且初始手里没有股票。
在这道题中由于买卖的次数是不做限制的,而且是不买卖股票是不收手续费的,所以只要第二天的价格比第一天低我们就可以买,比如:
数组是[7,8,9,1,2,3,2,1]
在这个数组中我们可以7买入,9卖出,再1买入,3卖出,这就是我们能找到的最大收益了。
但是这和我提到的第二天比第一天低我们就买有什么关系呢?
7买9卖,我们可以这么看,
第一天,价格为7,第二天价格8>7,所以我们7买入,花了7;
第二天,价格为8,首先把第一天买的买了,然后再看9>8,然后又8买入,没赚;
第三天,价格为9,把第二天8买的买了,然后再看1<9,不买了,赚了2;
以此类推。。。
所以这道题说白了就是然你找到递增的子数列,然后所有递增子数列最大-最小的和就是最大收益。
代码如下(java):
public class Solution {
public int maxProfit(int[] prices) {
if(prices == null || prices.length == 0)return 0;
int profit = 0;
for(int i=1; i<prices.length;i++){
if(prices[i]>prices[i-1]){
profit += prices[i]-prices[i-1];
}
} return profit;
}
}
LeetCode——Best Time to Buy and Sell Stock II (股票买卖时机问题2)的更多相关文章
- LeetCode——Best Time to Buy and Sell Stock III (股票买卖时机问题3)
问题: 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 I (股票买卖时机问题1)
问题: Say you have an array for which the ith element is the price of a given stock on day i. If you w ...
- 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 @ 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 ...
随机推荐
- java高新技术-枚举
1.什么是枚举 枚举是jdk1.5后才增加的新特性 用枚举就是要规定一个新的类型,那么要用这个类型的值就必须是我规定的那些值.如果不是那些值,编译器就会报错,好处是编译时就会做出判断 2.用普通类模拟 ...
- BZOJ 3230: 相似子串
3230: 相似子串 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 1485 Solved: 361[Submit][Status][Discuss ...
- PathGradientBrush类进行渐变颜色的填充
private void Form1_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; GraphicsPath gp ...
- 用 Python、 RabbitMQ 和 Nameko 实现微服务
用 Python. RabbitMQ 和 Nameko 实现微服务 原创 07-17 17:57 首页 Linux中国 "微服务是一股新浪潮" - 现如今,将项目拆分成多个独立的. ...
- ssh保持链接
修改/etc/ssh/sshd_config配置文件 ClientAliveInterval 300(默认为0), 参数的是意思是每5分钟,服务器向客户端发一个消息,用于保持连接,使用service ...
- Python之路【第二十篇】Tornado框架
Tornado Tornado是使用Python编写的一个强大的.可扩展的Web服务器.它在处理严峻的网络流量时表现得足够强健,但却在创建和编写时有着足够的轻量级,并能够被用在大量的应用和工具中. 我 ...
- Beanutils基本用法
Beanutils用了魔术般的反射技术,实现了很多夸张有用的功能,都是C/C++时代不敢想的.无论谁的项目,始终一天都会用得上它.我算是后知后觉了,第一回看到它的时候居然错过. 1.属性的动态gett ...
- 都别说工资低了,我们来一起写简单的dom选择器吧!
前言 我师父(http://www.cnblogs.com/aaronjs/)说应当阅读框架(jquery),所以老夫就准备开始看了 然后公司的师兄原来写了个dom选择器,感觉不错啊!!!原来自己从来 ...
- C#模拟鼠标键盘控制其他窗口(一)
编写程序模拟鼠标和键盘操作可以方便的实现你需要的功能,而不需要对方程序为你开放接口.比如,操作飞信定时发送短信等.我之前开发过飞信耗子,用的是对飞信协议进行抓包,然后分析协议,进而模拟协议的执行,开发 ...
- @property 参数
/* 1.set方法内存管理相关的参数 * retain : release旧值,retain新值(适用于OC对象类型) * assign : 直接赋值(默认,适用于非OC对象类型) * copy : ...