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 ...
随机推荐
- java多线程知识点汇总(一)多线程基础
1.什么叫多线程程序? 答:一个进程至少有一个线程在运行,当一个进程中出现多个线程时,就称这个应用程序是多线程应用程序. java编写的程序都是多线程的,因为最少有俩线程,main主线程和gc线程. ...
- icanhazip.com 使你在不论什么地方知道你的公网IP地址
icanhazip.com 使你在不论什么地方知道你的公网IP地址 icanhazip.com是一个网址,你在浏览器中输入这个网址,你就能得到你的公网IP地址了. 我在Linux下一般使用curl i ...
- 使用开源库 SDWebImage 异步下载缓存图片(持续更新)
source https://github.com/rs/SDWebImage APIdoc http://hackemist.com/SDWebImage/doc Asynchronous im ...
- 仿LOL项目开发第八天
仿LOL项目开发第八天 by 草帽 这节我们继续上节所讲的内容,上节我们初始化好了LoginWindow,当我们点击确认选择服务器按钮的时候,就发送服务器id给游戏服务器. 这里就开始涉及到客户端需要 ...
- 读取siftgeo格式文件的matlab程序
% This function reads a siftgeo binary file %读取siftgeo格式的二进制文件 % % Usage: [v, meta] = siftgeo_read ( ...
- 【tyvj五月有奖赛 暨Loi 55 Round #1】
解题报告: 傻逼错误天天犯QAQ 第一题:简单DP,f[i][j]表示第 i 道题选 j 的最大得分,可以从f[i-1][j-1],f[i-1][j],f[i-1][j+1]转移过来,其实是可以滚动数 ...
- 判断大小端的方法(java和c++)
首先我们给出大小端的定义: 小端:较高的有效字节存放在较高的的存储器地址,较低的有效字节存放在较低的存储器地址. 大端:较高的有效字节存放在较低的存储器地址,较低的有效字节存放在较高的存储器地址. 将 ...
- ransom-note
https://leetcode.com/problems/ransom-note/ public class Solution { public boolean canConstruct(Strin ...
- Informatica 常用组件Lookup缓存之二 使用永久查找高速缓存
可以将"查找"转换配置为使用非永久或永久高速缓存.基于"查找高速缓存永久"属性的会话成功后,PowerCenter 将保存或删除查找高速缓存文件. 如果查找表在 ...
- Rotate List leetcode java
题目: Given a list, rotate the list to the right by k places, where k is non-negative. For example: Gi ...