LeetCode OJ 122. 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).
【思路】
相比较上一个题目,这个题目对交易放松了限制,可以做多笔交易,但是在进行下一笔交易之前必须先完成上一笔交易,而且相同交易只能做一次。我的思路是:如果价格在未来上涨,那么我就在当前买入。如果价格在未来下跌,我就在当前卖出。这样的结果就是把数组划分成了一段段的递增序列,在序列的最开始买入,最后卖出。举个例子:[2,1,25,4,5,6,7,2,4,5,1,5]。数组被划分成了5个递增序列,在第一个序列利润为0,第二个序列利润为24,第三个为3,第四个为3,第五个为4.总的利润是34。代码如下:
public class Solution {
public int maxProfit(int[] prices) {
int min = 0;
int sump = 0;
int mp = 0;
for (int i = 1; i < prices.length; i++) {
if (prices[i] < prices[i-1]){
sump = sump + mp;
min = i;
mp = 0;
}
else
mp = prices[i] - prices[min];
}
return sump + mp;
}
}
其实更加简单直观的代码如下:
public class Solution {
public int maxProfit(int[] prices) {
int total = 0;
for(int i = 0; i < prices.length-1; i++){
if(prices[i+1] > prices[i]) total += prices[i+1] - prices[i];
}
return total;
}
}
最后要把所有递增序列中的最大值和最小值的差值加起来,其实这个过程和上述代码的描述是相同的。
LeetCode OJ 122. Best Time to Buy and Sell Stock II的更多相关文章
- 【一天一道LeetCode】#122. Best Time to Buy and Sell Stock II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Say you ...
- leetcode:122. Best Time to Buy and Sell Stock II(java)解答
转载请注明出处:z_zhaojun的博客 原文地址 题目地址 Best Time to Buy and Sell Stock II Say you have an array for which th ...
- 【刷题-LeetCode】122 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 ...
- 【LeetCode OJ】Best Time to Buy and Sell Stock II
Problem Link: http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ We solve this prob ...
- 【LeetCode】122.Best Time to Buy and Sell Stock II 解题报告(Java & Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】122. 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 a ...
- 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 al ...
- Leetcode No.122 Best Time to Buy and Sell Stock II Easy(c++实现)
1. 题目 1.1 英文题目 You are given an array prices where prices[i] is the price of a given stock on the it ...
- 【leetcode】122.Best Time to Buy and Sell Stock II(股票问题)
You are given an integer array prices where prices[i] is the price of a given stock on the ith day. ...
随机推荐
- <poj - 2139> Six Degrees of Cowvin Bacon 最短路径问题 the cow have been making movies
本题链接:http://poj.org/problem?id=2139 Description: The cows have been making movies lately, so the ...
- bootstrap IE8 相互兼容
针对 IE8 仍然需要额外引入 Respond.js 文件(由于仍然利用了浏览器对媒体查询(media query)的支持,因此还需要做处理) <html lang="zh-cn&qu ...
- hibernate事务控制
在使用ssh中将事务委托给spring时老是出现事务不可用 经过检查,原因如下: 是因为在hibernate.cfg.xml文件中忘记进行了如下设置: hibernate.current_sessio ...
- 在ueditor编辑器的光标停留处插入内容
业务场景: 首先在ueditor编辑器中插入一段文本,然后我想在文本的某个位置(光标停留处)插入一个字符串,这个字符串是从页面的其他地方选择得来的. 注意,当我们点击ueditor编辑器以外的地方,编 ...
- matcaffe的blob维度顺序
matcaffe是caffe的matlab接口.caffe本身是C++写的,其blob的维度顺序是[N,C,H,W],N表示batchSize,C表示channel数目,H表示feature map的 ...
- Jedis实现发布订阅功能
Redis为我们提供了publish/subscribe(发布/订阅)功能.我们可以对某个channel(频道)进行subscribe(订阅),当有人在这个channel上publish(发布)消息时 ...
- git 基本的操作
查看分支:git branch 查看所有分支:git branch -a 删除分支:git branch -d <name> 创建分支:git branch <nam ...
- centos6.8安装superctl 后台管理工具
下载安装python yum install python-setuptools 从官网下载supervisor包 https://pypi.python.org/pypi/supervisor 解压 ...
- C++虚成员函数表vtable
介绍一下多态是如何实现的,关于如何实现多态,对于程序设计人员来说即使不知道也是完全没有关系的,但是对于加深对多态的理解具有重要意义,故而在此节中稍微阐述一下多态的实现机制. 在C++中通过虚成员函数表 ...
- BAPI_GOODSMVT_CREATE 移动类型311 CODE = '04' 代码
DATA: MAT_DOC LIKE BAPI2017_GM_HEAD_RET-MAT_DOC. "物料凭证编号 DATA: GMHEAD LIKE BAPI2017_GM_H ...