[Leetcode][JAVA] Best Time to Buy and Sell Stock I, II, III
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.
买卖股票,只能买卖一次。那么只需要简单遍历一遍,记录利润值和买入值,每次遇到更大的利润值就更新,遇到更小的买入值就更新。这样在每个day i处计算出的利润值为在第i天卖出所能得到的最大利润。不断更新这个利润,最后得到的即为最大利润值。
public int maxProfit(int[] prices) {
if(prices.length<=0)
return 0;
int buy = prices[0];
int benifit = 0;
for(int i=0;i<prices.length;i++) {
benifit = Math.max(benifit, prices[i]-buy);
buy = Math.min(buy, prices[i]);
}
return benifit;
}
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).
无限次买卖股票,看似更难,实际更简单了。只需要得到所有攀升段的总值,即为总最大利润。那么只要第二天值比第一天更贵,则把它们的差值加到总利润。
public int maxProfit(int[] prices) {
int re = 0;
for(int i=1;i<prices.length;i++) {
if(prices[i]>prices[i-1])
re += prices[i]-prices[i-1];
}
return re;
}
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).
discussion里有人提出了一个dp方法适用于k次买卖的情况,很好理解。这里就直接照搬他的思路了:
// f[k, ii] 表示直到 prices[ii] 的最大利润 在最多k次交易的情况下.
// 转移函数:f[k, ii] = max(f[k, ii-1], prices[ii] - prices[jj] + f[k-1, jj]) { jj in range of [0, ii-1] } = max(f[k, ii-1], prices[ii] + max(f[k-1, jj] - prices[jj]))
// 基本情况:f[0, ii] = 0; 0次交易将无利润
// 基本情况:f[k, 0] = 0; 如果只有一天也将无利润
public int maxProfit(int[] prices) {
if(prices.length<=1)
return 0;
int k=2;
int[][] dp = new int[k+1][prices.length];
int re = 0;
for(int i=1;i<=k;i++) {
int temp = dp[i-1][0]-prices[0];
for(int j=1;j<prices.length;j++) {
temp = Math.max(temp, dp[i-1][j]-prices[j]);
dp[i][j] = Math.max(dp[i][j-1], prices[j]+temp);
}
}
return dp[k][prices.length-1];
}
[Leetcode][JAVA] Best Time to Buy and Sell Stock I, II, III的更多相关文章
- [LeetCode][Java] 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 a ...
- [LeetCOde][Java] 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 a ...
- LeetCode:Best Time to Buy and Sell Stock I II III
LeetCode:Best Time to Buy and Sell Stock Say you have an array for which the ith element is the pric ...
- leetcode day6 -- String to Integer (atoi) && Best Time to Buy and Sell Stock I II III
1. String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully con ...
- LeetCode之“动态规划”:Best Time to Buy and Sell Stock I && II && III && IV
Best Time to Buy and Sell Stock I 题目链接 题目要求: Say you have an array for which the ith element is the ...
- [LeetCode] 递推思想的美妙 Best Time to Buy and Sell Stock I, II, III O(n) 解法
题记:在求最大最小值的类似题目中,递推思想的奇妙之处,在于递推过程也就是比较求值的过程,从而做到一次遍历得到结果. LeetCode 上面的这三道题最能展现递推思想的美丽之处了. 题1 Best Ti ...
- Best Time to Buy and Sell Stock I,II,III [leetcode]
Best Time to Buy and Sell Stock I 你只能一个操作:维修preMin拍摄前最少发生值 代码例如以下: int maxProfit(vector<int> & ...
- Best Time to Buy and Sell Stock I II III
Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a gi ...
- 解题思路:best time to buy and sell stock i && ii && iii
这三道题都是同一个背景下的变形:给定一个数组,数组里的值表示当日的股票价格,问你如何通过爱情买卖来发家致富? best time to buy and sell stock i: 最多允许买卖一次 b ...
随机推荐
- COSBench添加driver负载机
说明:Driver是COSBench测试工具中对负载机的一种标记,相当于loadrunner中的负载发生器. 在使用COSBench进行云存储性能测试时,面对强大的云服务,如果只有单个driver负载 ...
- JS语法(二)
JS变量 var 变量名 = 变量值://自己会判断什么类型 一个好的编程习惯是,在代码开始处,统一对需要的变量进行声明. var name = “xiaosan”, age = 22, addres ...
- vbs脚本要求在cmd中输入输出用StdIn ,StdOut
Dim StdIn, StdOutSet StdIn = WScript.StdInSet StdOut = WScript.StdOut Do While Not StdIn.AtEndOfStre ...
- iOS 关于僵尸对象和僵尸指针的那些事儿
引言 提到僵尸就感到一种恐怖,大家都知道“僵尸”是没有生命的,但是它确实是一种存在的类似生命体的一种生物.哈哈,当然本文的重点不是讨论“僵尸”,而是有关于ios当中经常遇到的僵尸指针(Zombie P ...
- leetcode 125
125. Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric ...
- 远程操控软件 TeamViewer for MAC 官方中文版11.0.55321
百度云:https://pan.baidu.com/s/1o77ol2y 提取密码:3tsx windows: http://download.pchome.net/internet/server/r ...
- shell_基础知识
参考: http://blog.csdn.net/kaizi318/article/details/9343551 开头程序必须以下面的行开始(必须方在文件的第一行):#!/bin/sh符号#!用来告 ...
- Centos6.x服务器配置jdk+tomcat+mysql环境
1,jdk配置 由于jdk官网的链接不直接支持wget,可以使用下面的方法下载jdk,其中jdk版本为jdk1.8.0_91: wget --no-check-certificate --no-coo ...
- Jmeter上传文件
Jmeter上传文件 一.Fiddler抓包获取表单信息 操作被测系统,上传文件,Fiddler抓包获取提交表单信息如下:
- 8.Mybatis的延迟加载
在Mybatis中的延迟加载只有resultMap可以实现,ResultMap 可以实现高级映射(association,collection可以实现一对1和一对多的映射),他们具有延迟加载的功能,r ...