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 ...
随机推荐
- Is Usb Drive () ? DeviceIoControl, IOCTL_STORAGE_QUERY_PROPERTY
http://banderlogi.blogspot.com/2011/06/enum-drive-letters-attached-for-usb.html typedef enum _STORAG ...
- 网络编程_Python-网络模型.
http://xmdevops.blog.51cto.com/11144840/1861280
- iOS开发里的Bundle是个啥玩意?!
初学iOS开发的同学,不管是自己写的,还是粘贴的代码,或多或少都写过下面的代码 [[NSBundle mainBundle] pathForResource:@"someFileName&q ...
- Spring初始化完成后直接执行一个方法,初始化数据(解决方法被重复执行两次的情况)
在做WEB项目时,经常在项目第一次启动时利用WEB容器的监听.Servlet加载初始化等切入点为数据库准备数据,这些初始化数据 是系统开始运行前必须的数据,例如权限组.系统选项.默认管理员等等.但是项 ...
- 7. python 字符串格式化方法(2)
7. python 字符串格式化方法(2) 紧接着上一章节,这一章节我们聊聊怎样添加具体格式化 就是指定替换字段的大小.对齐方式和特定的类型编码,结构如下: {fieldname!conversion ...
- gflags摘记
projcet url: https://github.com/schuhschuh/gflags usage: commandline flags processing DEFINE: Defini ...
- Selenium2+python自动化52-unittest执行顺序
前言 很多初学者在使用unittest框架时候,不清楚用例的执行顺序到底是怎样的.对测试类里面的类和方法分不清楚,不知道什么时候执行,什么时候不执行. 本篇通过最简单案例详细讲解unittest执行顺 ...
- 使用 DES 算法对数据加密
DES算法 ☆提供高质量的数据保护,防止数据未经授权的泄露和未被察觉的修改 ☆具有相当高的复杂性,使得破译的开销超过可能获得的利益,同时又要便于理解和掌握 ☆DES密码体制的安全性应该不依赖于算法的保 ...
- [翻译] FlatUIKit
FlatUIKit FlatUIKit is a collection of iOS components styled with the "Flat UI" aesthetic ...
- 同步FIFO的设计
module scfifo #( , ) ( input clk, input rst_n, input wren, input rden, :] din, :] dout, output full, ...