LeetCode: Best Time to Buy and Sell Stock II 解题报告
Best Time to Buy and Sell Stock II
Question Solution
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).
解:
既然要最大化利润,我们应该拿下股票所有的升值部分。从开始到最后股票可能有升有降,那么,我们把每一个升值的区间的利润加在一起就是最大值。
也就是每次比较当天和前一天的股票值,如果是上升,就加上这个差值即可。
public class Solution {
public int maxProfit(int[] prices) {
if (prices == null) {
return 0;
}
int maxProfit = 0;
int len = prices.length;
for (int i = 1; i < len; i++) {
int dif = prices[i] - prices[i - 1];
if (dif > 0) {
maxProfit += dif;
}
}
return maxProfit;
}
}
LeetCode: Best Time to Buy and Sell Stock II 解题报告的更多相关文章
- LeetCode: Best Time to Buy and Sell Stock III 解题报告
Best Time to Buy and Sell Stock IIIQuestion SolutionSay you have an array for which the ith element ...
- 【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 ...
- 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 买股票的最佳时间之二
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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 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 a ...
- [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 ...
随机推荐
- LNMP分离式部署实例[转]
很多人在练习部署LNMP环境的时候,大都数是部署在同一个虚拟机上面的.但是实际工作中,我们一般都是分离部署的. 今天我就用3台虚拟机,部署下LNMP环境.以供参考! 网络拓扑图: 首先准备3台虚拟机: ...
- UIView的transform
iOS开发UIView的transform属性详解 本文主要是详解iOS开发UIView的transform属性 CGAffineTransform实际上是一个矩阵 | a, b, 0 | | c ...
- cron执行service
在Cron的环境下,是没有定义路径的,所以,service xxx start等等要使用绝对路径 => /sbin/service xxx start service的路径可以用whereis ...
- Inside i++
i++.++i.i=i+1.效率怎么样?看过一本书上说,i++比i=i+1好的地方是因为i=i+1中的那个1要占用一个寄存器,所以速度没有i++快,于是我想验证一下.另外,以前听说过Java中的“i= ...
- SharePoint自动化部署,利用SPSD工具包
目录 怎样使用SPSD 配置Environment XML文件 PowerShell激活Feature 上篇博客讲了利用PowerShell导出.导入AD中的Users.这篇介绍简单介绍一下SPSD ...
- proguard的简单配置说明
#需要转换的jar文件路径-injars 'D:\fs-np.jar'#转换后的jar文件名称-outjars 'D:\fs-np-sec.jar' #关联的第三方jar-libraryjars 'C ...
- 怎么在eclipse中查到这个类用的是哪个jar的类和Eclipse 编译错误 Access restriction:The type *** is not accessible due to restriction on... 解决方案
找到了一个办法,你先按F3,然后点击Change Attached Source..按钮,在弹出的框里有个路径,我的路径是D:/SNFWorkSpace/JAR/~importu9.jar,然后你去引 ...
- 安装ganglia过程中出现错误 perl(RRDp) is needed by rrdtool-1.2.30-1.el5.rf.x86_64
用rpm -ivh *.rpm安装ganglia的rpm包,然后出现下面的错误: warning: rrdtool-1.2.30-1.el5.rf.x86_64.rpm: Header V3 DSA/ ...
- 7 款顶级的开源 Web 分析软件
Web 分析无非就是 Web 流量的测量.但它并不限于测量网络流量,还包括: 分析 数据采集 为了了解和优化网页而上报网络数据 Google Analytics是最广泛使用的基于云的网络分析服务.不过 ...
- Tcp超时修改
Linux 建立 TCP 连接的超时时间分析 tags: linux | network Linux 系统默认的建立 TCP 连接的超时时间为 127 秒,对于许多客户端来说,这个时间都太长了, 特别 ...