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 ...
随机推荐
- 数据结构图文解析之:AVL树详解及C++模板实现
0. 数据结构图文解析系列 数据结构系列文章 数据结构图文解析之:数组.单链表.双链表介绍及C++模板实现 数据结构图文解析之:栈的简介及C++模板实现 数据结构图文解析之:队列详解与C++模板实现 ...
- LCIS
传送门 http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=726&pid=1003 分析:这道题依然是动态 ...
- 项目中CKEditor修改宽度为自适应
项目中用到CKEditor,在config.js中直接定义config.width使得宽度无法自适应,尝试了好多次后发现了一种方法: 放弃在config.js中配置宽度 在页面检查元素,找到id为ck ...
- C语言常见类型占用字节数
前言 最近笔试经常遇到c语言各类型变量所占字节数的问题,这里做一个总结好了. 类型 常见的有char.int.long.short.float.double及指针等. 字符类型 这里单只char,ch ...
- MyEclipse导入jquery-1.8.0.min.js等文件报错的解决方案
1.选中报错的jquery文件例如"jquery-1.8.0.min.js". 2.右键选择 MyEclipse-->Exclude From Validation . 3. ...
- Android开发之《常用工具及文档汇总》
GreenVPN:https://www.getgreenjsq.com/ Android开发工具.资料下载汇总:http://androiddevtools.cn/#img-size-handle- ...
- BZOJ2728: [HNOI2012]与非
线性基乱搞,非正解= = #include<cstdio> int n,m; typedef long long ll; ll l[60],j,s,t; void up(ll& i ...
- WebView与JS的几种交互
http://www.jianshu.com/p/0042d8eb67c0 最近整理了一下原生与H5之间的交互方式,简单的做个总结.OC端与JS的交互,大致有这几种:拦截协议.JavaScriptCo ...
- 11月15日下午 ajax返回数据类型为XML数据的处理
ajax返回数据类型为XML数据的处理 /*XML:可扩展标记语言 HTML:超文本标记语言 标签:<标签名></标签名> 特点: 1.必须要有一个根 2.标签名自定义 3.对 ...
- ecshop后台权限审核列表
1.权限语言包 languages\zh_cn\admin\priv_action.php //微仓 $_LANG['depot'] = '微仓管理'; $_LANG['depot_list_is_c ...