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 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...
随机推荐
- 【HDU1856】More is better(并查集基础题)
裸并查集,但有二坑: 1.需要路径压缩,不写的话会TLE 2.根据题目大意,如果0组男孩合作的话,应该最大的子集元素数目为1.所以res初始化为1即可. #include <iostream&g ...
- Android图形合成和显示系统---基于高通MSM8k MDP4平台
介绍了Android SurfaceFlinger层次以下的图形合成和显示系统,主要基于高通MSM8k MDP4x平台. 做为Android Display专题.SurfaceFlinger的详细介绍 ...
- jquery 自动跳出列表
先上效果图:当鼠标经过相亲会自动弹出取最新的10条数据
- discuz!NT 常用操作
一.编辑模版,需在后台模版管理里编辑并提交,这样系统能批量更改相关模版.如:更改登录. 二.config.a 表明:config表示配置文件,a表示配置节名称为a,需在路径 upload_files\ ...
- HDU4907小技巧
原题http://acm.hdu.edu.cn/showproblem.php?pid=4907 Task schedule Time Limit: 2000/1000 MS (Java/Others ...
- 成语接龙(dfs)
成语接龙 Time Limit: 1000 MS Memory Limit: 32768 K Total Submit: 92(17 users) Total Accepted: 23(14 user ...
- EffectiveC#7--选择恒定的原子值类型数据
1.恒定类型就是一但它们被创建,它们(的值)就是固定的. 恒定类型可以很好的在基于哈希代码的集合上工作.以Object.GetHashCode()方法返回的值,对同一个实例是必须相同的 2.一个客户类 ...
- HTML与CSS入门——第一章 理解Web的工作方式
知识点: 1.万维网的简史 2."网页"的含义,以及该术语不能反映所涉及的所有内容的原因 3.如何从你的个人计算机进入别人的浏览器 4.选择Web托管提供商的方法 5.不同的Web ...
- UILable文本常见属性说明
1.text:设置标签显示文本. 2.attributedText:设置标签属性文本. NSString *text = @"first"; NSMutableAttributed ...
- 跨平台渲染框架尝试 - Texture管理
纹理是渲染器重要的资源,也是比较简单的资源.本文将详细讨论纹理资源的管理. 在资源管理概述中提到,资源就是一堆内存和CPU与GPU的访问权限.纹理管理在资源管理之上,要负责如何使用者一堆内存构造纹理对 ...