leetcode 股票系列
五道股票题总结:
121 买卖股票的最佳时机
122 买卖股票的最佳时机
124 买卖股票的最佳时机4
309 最佳股票买卖含冷冻期
714 买卖股票的最佳时机含有手续费
121 买卖股票的最佳时机
给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。
如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润。
注意你不能在买入股票前卖出股票。
示例 1:
输入: [7,1,5,3,6,4]
输出: 5
解释: 在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = 6-1 = 5 。
注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价格。
示例 2:
输入: [7,6,4,3,1]
输出: 0
解释: 在这种情况下, 没有交易完成, 所以最大利润为 0。
// Solution 1 dp[i] 代表以i结尾的最大利润
class Solution {
int[] dp ;
public int maxProfit(int[] prices) {
if(prices.length == 0 )
return 0;
dp = new int [prices.length + 2];
Arrays.fill(dp,0);
int min = prices[0];
for( int i=1; i<prices.length; i++){
if(prices[i-1] < min) {
min = prices[i-1];
}
dp[i] = prices[i] - min;
}
int ans = 0 ;
for(int i=0; i<prices.length; i++){
ans = Math.max(ans, dp[i]);
}
return ans;
}
};
122 买卖股票的最佳时机
给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。
设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易(多次买卖一支股票)。
注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
示例 1:
输入: [7,1,5,3,6,4]
输出: 7
解释: 在第 2 天(股票价格 = 1)的时候买入,在第 3 天(股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。
随后,在第 4 天(股票价格 = 3)的时候买入,在第 5 天(股票价格 = 6)的时候卖出, 这笔交易所能获得利润 = 6-3 = 3 。
示例 2:
输入: [1,2,3,4,5]
输出: 4
解释: 在第 1 天(股票价格 = 1)的时候买入,在第 5 天 (股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。
注意你不能在第 1 天和第 2 天接连购买股票,之后再将它们卖出。
因为这样属于同时参与了多笔交易,你必须在再次购买前出售掉之前的股票。
示例 3:
输入: [7,6,4,3,1]
输出: 0
解释: 在这种情况下, 没有交易完成, 所以最大利润为 0。
class Solution {
public int maxProfit(int[] prices) {
// solution 1
// int Length = prices.length;
// if(Length == 0 ){
// return 0;
// }
// int[] buy = new int[Length];
// int[] s1 = new int[Length];
// int[] sell = new int[Length];
// int[] s2 = new int[Length];
// buy[0] = -prices[0];
// s1[0] = -prices[0];
// for(int i=1; i<Length;i++){
// buy[i] = Math.max(s2[i-1], sell[i-1]) - prices[i];
// s1[i] = Math.max(s1[i-1],buy[i-1]);
// sell[i] = Math.max(buy[i-1], s1[i-1]) + prices[i];
// s2[i] = Math.max( s2[i-1], sell[i-1]);
// }
// return Math.max( sell[Length-1], s2[Length-1]);
//solution 2
int Length = prices.length;
if(Length == 0 ){
return 0;
}
int ans = 0 ;
for(int i=1 ; i< Length; i++){
if( prices[i] > prices[i-1] ){
ans += (prices[i] - prices[i-1]);
}
}
return ans ;
}
}
124 买卖股票的最佳时机4
给定一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格。
设计一个算法来计算你所能获取的最大利润。你最多可以完成 k 笔交易。
注意: 你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
示例 1:
输入: [2,4,1], k = 2
输出: 2
解释: 在第 1 天 (股票价格 = 2) 的时候买入,在第 2 天 (股票价格 = 4) 的时候卖出,这笔交易所能获得利润 = 4-2 = 2 。
示例 2:
输入: [3,2,6,5,0,3], k = 2
输出: 7
解释: 在第 2 天 (股票价格 = 2) 的时候买入,在第 3 天 (股票价格 = 6) 的时候卖出, 这笔交易所能获得利润 = 6-2 = 4 。
随后,在第 5 天 (股票价格 = 0) 的时候买入,在第 6 天 (股票价格 = 3) 的时候卖出, 这笔交易所能获得利润 = 3-0 = 3 。
class Solution {
public int maxProfitInfinte(int[] prices) {
int Length = prices.length;
if(Length == 0 ){
return 0;
}
int ans = 0 ;
for(int i=1 ; i< Length; i++){
if( prices[i] > prices[i-1] ){
ans += (prices[i] - prices[i-1]);
}
}
return ans ;
}
public int maxProfit(int k, int[] prices) {
int Length = prices.length;
if(Length == 0 || Length == 1 ){
return 0;
}
if( k > Length >> 1 ){
return maxProfitInfinte( prices);
}
int[] buy = new int[Length];
int[] sell = new int[Length];
for(int j=1; j<=k; j++) {
buy[j] = Integer.MIN_VALUE;
}
// buy[j]代表进行了j词交易,最后一次为买时能获取的最大利润
for(int i=0;i<Length;i++){
for (int j = 1; j<=k; j++) {
buy[j] = Math.max(buy[j], sell[j-1] - prices[i]);
sell[j] = Math.max(buy[j] + prices[i], sell[j]);
}
}
return sell[k] ;
}
}
309 最佳股票买卖含冷冻期
给定一个整数数组,其中第 i 个元素代表了第 i 天的股票价格 。
设计一个算法计算出最大利润。在满足以下约束条件下,你可以尽可能地完成更多的交易(多次买卖一支股票):
你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
卖出股票后,你无法在第二天买入股票 (即冷冻期为 1 天)。
示例:
输入: [1,2,3,0,2]
输出: 3
解释: 对应的交易状态为: [买入, 卖出, 冷冻期, 买入, 卖出]
class Solution {
public int maxProfit(int[] prices) {
// int length = prices.length;
// if(length == 0 ){
// return 0 ;
// }
// int[] buy = new int[length];
// int[] sell = new int[length];
// int[] none = new int[length];
// int[] sequeze = new int[length];
// buy[0] = -prices[0];
// sell[0] = 0;
// none[0] = -prices[0] ;
// sequeze[0] = 0 ;
// for(int i=1; i<length; i++ ){
// buy[i] = sequeze[i-1] - prices[i] ;
// none[i] = Math.max(none[i-1] , buy[i-1] ) ;
// sell[i] = Math.max( buy[i-1] , none[i-1] ) + prices[i] ;
// sequeze[i] = Math.max(sequeze[i-1], sell[i-1]) ;
// }
// return Math.max(sequeze[ length -1 ] , sell[ length -1 ]);
}
}
714 买卖股票的最佳时机含有手续费
给定一个整数数组 prices,其中第 i 个元素代表了第 i 天的股票价格 ;非负整数 fee 代表了交易股票的手续费用。
你可以无限次地完成交易,但是你每次交易都需要付手续费。如果你已经购买了一个股票,在卖出它之前你就不能再继续购买股票了。
返回获得利润的最大值。
示例 1:
输入: prices = [1, 3, 2, 8, 4, 9], fee = 2
输出: 8
解释: 能够达到的最大利润:
在此处买入 prices[0] = 1
在此处卖出 prices[3] = 8
在此处买入 prices[4] = 4
在此处卖出 prices[5] = 9
总利润: ((8 - 1) - 2) + ((9 - 4) - 2) = 8.
注意:
0 < prices.length <= 50000.
0 < prices[i] < 50000.
0 <= fee < 50000.
class Solution {
public int maxProfit( int[] prices, int fee) {
int Length = prices.length;
if(Length == 0 || Length == 1 ){
return 0;
}
int[] buy = new int[Length];
int[] sell = new int[Length];
buy[0] = -prices[0];
//sell[i] 代表以i物品结束交易的获取的最大利润
//buy[i] 代表买第i个物品获取的最大利润
for(int i=1 ; i<Length;i++){
buy[i] = Math.max( buy[i-1],sell[i-1] - prices[i]) ;
sell[i] = Math.max( sell[i-1] , buy[i-1] + prices[i] - fee );
}
return sell[Length-1] ;
}
}
leetcode 股票系列的更多相关文章
- LeetCode——single-number系列
LeetCode--single-number系列 Question 1 Given an array of integers, every element appears twice except ...
- Leetcode算法系列(链表)之删除链表倒数第N个节点
Leetcode算法系列(链表)之删除链表倒数第N个节点 难度:中等给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点.示例:给定一个链表: 1->2->3->4-&g ...
- Leetcode算法系列(链表)之两数相加
Leetcode算法系列(链表)之两数相加 难度:中等给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字.如果,我们将 ...
- [leetcode]BestTimetoBuyandSellStock买卖股票系列问题
问题1: If you were only permitted to complete at most one transaction (ie, buy one and sell one share ...
- [Leetcode] Sum 系列
Sum 系列题解 Two Sum题解 题目来源:https://leetcode.com/problems/two-sum/description/ Description Given an arra ...
- LeetCode 笔记系列16.3 Minimum Window Substring [从O(N*M), O(NlogM)到O(N),人生就是一场不停的战斗]
题目:Given a string S and a string T, find the minimum window in S which will contain all the characte ...
- leetcode股票问题方法收集 转载自微信公众号labuladong
一.穷举框架首先,还是一样的思路:如何穷举?这里的穷举思路和上篇文章递归的思想不太一样. 递归其实是符合我们思考的逻辑的,一步步推进,遇到无法解决的就丢给递归,一不小心就做出来了,可读性还很好.缺点就 ...
- [leetcode] 股票问题
参考文章: [1] 团灭 LeetCode 股票买卖问题 [2] Most consistent ways of dealing with the series of stock problems 其 ...
- LeetCode 笔记系列13 Jump Game II [去掉不必要的计算]
题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...
随机推荐
- java动态代理框架
java动态代理是一个挺有意思的东西,他有时候可以被使用的很灵活.像rpc的调用,调用方只是定义的一个接口,动态代理让他匹配上对应的不同接口:mybatis内部的实现,编码时,只是实 ...
- 02—mybatis的基本用法01
深入mybatis的配置文件(mybatis-config.xml) MyBatis的配置文档结构 顶层configuration 配置 properties 属性 settings 设置 typ ...
- RHEL8 创建本地YUM存储库
yum 的好处及本地yum的好处不在本文讨论范畴,本文针对rhel8中的新功能yum做简要介绍和配置,在 RHEL 8中分为两个存储库: BaseOS 应用程序流(AppStream) BaseOS中 ...
- Linux防CC攻击脚本
多数CC攻击在web服务器日志中都有相同攻击的特征,我们可以根据这些特征过滤出攻击的ip,利用iptables来阻止 #!/bin/bash #by LinuxEye #BLOG: http://bl ...
- Codeforces Round #509 (Div. 2) F. Ray in the tube(思维)
题目链接:http://codeforces.com/contest/1041/problem/F 题意:给出一根无限长的管子,在二维坐标上表示为y1 <= y <= y2,其中 y1 上 ...
- 洛谷P3935 Calculation [数论分块]
题目传送门 格式难调,题面就不放了. 分析: 实际上这个就是这道题的升级版,没什么可讲的,数论分块搞就是了. Code: //It is made by HolseLee on 18th Jul 20 ...
- HDU 3374 exkmp+字符串最大最小表示法
题意 找到一个字符串中最先出现的最小(大)表示位置,和最小(大)表示串出现次数 分析 用最小(大)表示法求出最先出现的最小(大)表示位置,然后将串长扩两倍用exkmp找出现次数. Code #incl ...
- Linux命令行学习日志-ps ax
当我们需要查询某个运行中的进程的时候,这个命令就显得很有用了,可以查看当前进程的PID和状态(S代表睡眠,SW代表睡眠和等待,R表示运行中) ps ax //查看当前运行中的进程
- 如何优雅地防止MLE(for linux)
赛前最后一天模拟赛又有小伙伴MLE了--这里就讲一下如何较为精确地获取程序运行时间和空间. 资源统计当然是操作系统统计的最精确.所以可以这样写(noilinux实测通过,windows下应该不行): ...
- CF280C
CF280C ZR补题计划 题意: 一棵有根树,每次选择一个未删除的节点,然后删除它和它的子树内的点,问期望删多少次可以把整个树删完 解析: 显然,通过题面,我们可以知道对于一个点对 $ (u,v) ...