Best Time to Buy and Sell Stock II 解答
Question
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).
Solution
The key to this problem is to only consider local minimizer point and local maximizer point. And then add up sums. In this way, we avoid processing situation that transactions happen on one day.
public class Solution {
public int maxProfit(int[] prices) {
if (prices == null || prices.length < 2)
return 0;
int result = 0, localMin = prices[0], localMax = prices[0], i = 0, length = prices.length;
while (i < length) {
// To find local minimizer point
while (i < length - 1 && prices[i + 1] < prices[i])
i++;
localMin = prices[i];
// To find local maximizer point
i++;
if (i == length) {
localMax = prices[length - 1];
} else {
while (i < length - 1 && prices[i + 1] > prices[i])
i++;
if (i < length - 1)
localMax = prices[i];
else if (i == length - 1)
localMax = prices[length - 1];
}
result += (localMax - localMin);
}
return result;
}
}
Best Time to Buy and Sell Stock II 解答的更多相关文章
- [LintCode] 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 [贪心算法]
Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a ...
- 27. Best Time to Buy and Sell Stock && Best Time to Buy and Sell Stock II && Best Time to Buy and Sell Stock III
Best Time to Buy and Sell Stock (onlineJudge: https://oj.leetcode.com/problems/best-time-to-buy-and- ...
- Leetcode-122 Best Time to Buy and Sell Stock II
#122 Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the pric ...
- 【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 ...
- 31. leetcode 122. Best Time to Buy and Sell Stock II
122. Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price ...
- 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 ...
- 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 121. Best Time to Buy and Sell Stock 、122.Best Time to Buy and Sell Stock II 、309. Best Time to Buy and Sell Stock with Cooldown
121. Best Time to Buy and Sell Stock 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...
随机推荐
- unix c 10
网络常识: OSI 7层模型 TCP模型 IP和端口 IP是用来定位网络中的计算机,端口用来代表 计算机中的某个进程. IP 有点分十进制 和 十六进制的两种表示方式,底层 ...
- Android之字符串的拆分-split
字符串的拆分可以利用android的 split 来简单实现 具体看如下代码: String s3 = "Real-How-To"; String [] temp = null; ...
- C# 实现简单状态机(参考代码)
using System; namespace StateMachine2.State { public enum AnimationState { Walk = , Dead, } public a ...
- C++11里面的Lambda表达式
Lambda Expressions in C++ C++中的Lambda表达式 In Visual C++, a lambda expression—referred to as a lambda— ...
- android 推断Apk是否签名和 签名是否一致
推断Apk是否签名 用命令:jarsigner -verify -verbose -certs <apk文件> 假设有Android Debug字樣就是debug 假设已经签名: [证书的 ...
- ProFTPD“killed (signal 15)”自动退出问题解决
proftpd服务端每隔几天就重启一次,日志如下: 看起来这又像是一个仅有两行日志的无头案了.不过由于日志明确给出了退出信号“killed (signal 15)”,因此Google的话还是比较简单找 ...
- Android Eclipse Errors
1.The import org.apache.http.client; tip: cannot be resolved; resolve: Find library in your sdk and ...
- CSS兼容性常见问题总结
DIV+CSS设计IE6.IE7.FF 兼容性 DIV+CSS网页布局这是一种趋势,我也开始顺应这股趋势了,不过在使用DIV+CSS网站设计的时候,应该注意css样式兼容不同浏览器问题,特别是对完全使 ...
- Vim知识点收集
(注意: 只记录工作中实际使用的命令) 删除带有pattern的所有行 :g/pattern/d 删除不带pattern的所有行 :g!/pattern/d 匹配red和blue,无次序 ...
- CoutDownLatch 多线程同步辅助类
CountDownLatch,一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待. 主要方法 public CountDownLatch(int count); pu ...