Best Time to Buy and Sell Stock I && II && III
题目1:Best Time to Buy and Sell Stock
Say you have an array for which the ith element is the price of a given stock on day i.
If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find
the maximum profit.
分析:
public class Solution { public int maxProfit(int[] prices) {
int size = prices.length;
if (size == 0){
return 0;
}
int maxPrice = prices[size-1];//初始化最大price
int maxMoney = 0;//初始化利润值
for (int i=size-1; i>=0; --i){
maxPrice = maxPrice > prices[i] ? maxPrice : prices[i];//假设第i天的值大于最大price,则更新最大price的值
maxMoney = maxMoney > (maxPrice - prices[i]) ? maxMoney : (maxPrice - prices[i]);//更新最大利润值
}
return maxMoney;
}
}
题目2: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).
AC代码:
public class Solution {
public int maxProfit(int[] prices) {
int profit = 0;
int size = prices.length;
if (size < 2){
return profit;
}
for (int index=1; index<size; ++index){
int value = prices[index] - prices[index-1];
if (value > 0){
profit += value;
}
}
return profit;
}
}
题目3:
Best Time to Buy and Sell Stock III
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 at most two transactions.
Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
用right[i]来表示[i,...,n-1]上的最大利润
public class Solution {
public int maxProfit(int[] prices) {
int size = prices.length;
if (size < 2)
return 0;
int[] left = new int[size];
int[] right = new int[size];
int minValue = prices[0];
int maxValue = prices[size-1];
for (int i=1; i<size; ++i){
left[i] = left[i-1] > (prices[i] - minValue) ? left[i-1] : (prices[i] - minValue);
minValue = minValue < prices[i] ? minValue : prices[i];
}
for (int i=size-2; i>=0; --i){
right[i] = right[i+1] > (maxValue - prices[i]) ? right[i+1] : (maxValue - prices[i]);
maxValue = maxValue > prices[i] ? maxValue : prices[i];
}
int profit=0;
for (int i=0; i<size; ++i){
profit = profit > (left[i] + right[i]) ? profit : (left[i] + right[i]);
}
return profit;
}
}
Best Time to Buy and Sell Stock I && II && III的更多相关文章
- [LeetCode] Best Time to Buy and Sell Stock with Cooldown 买股票的最佳时间含冷冻期
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 IV 买卖股票的最佳时间之四
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 III 买股票的最佳时间之三
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 买股票的最佳时间之二
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 买卖股票的最佳时间
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- [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 ...
- [LintCode] Best Time to Buy and Sell Stock 买卖股票的最佳时间
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- 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 ...
- 123. Best Time to Buy and Sell Stock (三) leetcode解题笔记
123. Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the pric ...
随机推荐
- 多个rs485设备怎样跟上位机通讯?
http://bbs.hcbbs.com/thread-819457-1-1.html 多个rs485设备怎样跟上位机通讯? [复制链接] |关注本帖 fdemeng 签到天数: 1228 天 ...
- 去除ArcMap连接空间数据库中多余的属性表
这个操作目前可能不具有可行性,但是为了完整性还是在下面讲一下吧.如有兴趣的小伙伴,可以按照下面的操作方式去尝试. 一.需求 去除ArcMap连接空间数据库中多余的属性表. PL/SQL中查询得到的内容 ...
- anroid源码下载和编译
本文是在Ubuntu10.10系统上进行实践的. 1 因为我们需要Android的模拟器,所以需要安装Android的SDK,并创建AVD, 可以一次命名为AVD15,AVD22,AVD23,... ...
- 使用 SVWebViewController 推出浏览器控制器
SVWebViewController 简单翻译 https://github.com/samvermette/SVWebViewController SVWebViewController is a ...
- Linux下安装Oracle的过程和涉及的知识点-系列6
16.一路安装后.会提示下面界面.此时须要用root登录下面文件夹,然后运行这两个脚本. 至此,Oracle软件的安装就已经完毕了,接下来就能够创建数据库了. 17.选择自己定义数据库: 输入数据库名 ...
- [MAC OS] NSButton tag 获取
@IBAction func switchContentLayout(_ sender: Any) { let button : NSButton = sender as! NSButton;}
- Java并发编程里的volatile。Java内存模型核CPU内存架构的对应关系
CPU内存架构:https://www.jianshu.com/p/3d1eb589b48e Java内存模型:https://www.jianshu.com/p/27a9003c33f4 多线程下的 ...
- mini-parser
https://leetcode.com/problems/mini-parser/ /** * // This is the interface that allows for creating n ...
- [转]linux sort 命令详解
原文网址:http://www.cnblogs.com/51linux/archive/2012/05/23/2515299.html 1 sort的工作原理 sort将文件的每一行作为一个单位,相互 ...
- Linux进程调度的运行队列
当Linux内核要寻找一个新的进程在CPU上运行时,必须只考虑处于可运行状态的进程,(即在TASK_RUNNING状态的进程),因为扫描整个进程链表是相当低效的,所以引入了可运行状态进程的双向循环链表 ...