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; } }
相关文章
- Unity 3D本地公布WebPlayer版时"Failed to download data file"解决方式
- 超时空英雄传说2复仇魔神完全攻略&秘技
- How To Bind a Combobox to a Dictionary in WPF C#
- type 'simple Class' does not conform to protocol 'Example Protocol'错误
- how to deal with "no such file error or diretory" error for a new programmer in QT creator
- English trip V2 - 10 What to Take 你需要准备什么 Teacher:Lamb Key: many & much
- 解决 Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
- Nginx: error_log & access_log & rewrite_log
- 我的2016_To Code or Not to Code: No Question
- 【前端学习之HTML&CSS】-- CSS第七篇 -- 视觉格式化模型之二 浮动
- Java重写(Override)&重载(Overload)
- (数据科学学习手札16)K-modes聚类法的简介&Python与R的实现
- Linux下监控进程的网络以及目标主机建立连接的网络流量&非交互使用iotop
- 高数 | 两个重要极限 & 经典错误 & 什么情况下求极限可以直接带入
- Qt error ------ no matching function for call to QObject::connect(QSpinBox*&, <unresolved overloaded function type>, QSlider*&, void (QAbstractSlider::*)(int))
- 关于javax.servlet.jsp.JspTagException: Don't know how to iterate over supplied "items" in <forEach>
- docker命令:docker-stack相关操作、使用 inspect 命令查看镜像详细信息、docker rm & docker rmi & docker prune 清理删除镜像的差异
- CentOS出错You don't have permission to access on this server
- 九度OJ #1437 To Fill or Not to Fil
- How to improve Java's I/O performance( 提升 java i/o 性能)
- 工具说明书 - Git: stash & clean
- 【Unity入门计划】基本概念(5)-游戏对象&组件
- 1092 To Buy or Not to Buy