五道股票题总结:

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 股票系列的更多相关文章

  1. LeetCode——single-number系列

    LeetCode--single-number系列 Question 1 Given an array of integers, every element appears twice except ...

  2. Leetcode算法系列(链表)之删除链表倒数第N个节点

    Leetcode算法系列(链表)之删除链表倒数第N个节点 难度:中等给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点.示例:给定一个链表: 1->2->3->4-&g ...

  3. Leetcode算法系列(链表)之两数相加

    Leetcode算法系列(链表)之两数相加 难度:中等给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字.如果,我们将 ...

  4. [leetcode]BestTimetoBuyandSellStock买卖股票系列问题

    问题1: If you were only permitted to complete at most one transaction (ie, buy one and sell one share ...

  5. [Leetcode] Sum 系列

    Sum 系列题解 Two Sum题解 题目来源:https://leetcode.com/problems/two-sum/description/ Description Given an arra ...

  6. 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 ...

  7. leetcode股票问题方法收集 转载自微信公众号labuladong

    一.穷举框架首先,还是一样的思路:如何穷举?这里的穷举思路和上篇文章递归的思想不太一样. 递归其实是符合我们思考的逻辑的,一步步推进,遇到无法解决的就丢给递归,一不小心就做出来了,可读性还很好.缺点就 ...

  8. [leetcode] 股票问题

    参考文章: [1] 团灭 LeetCode 股票买卖问题 [2] Most consistent ways of dealing with the series of stock problems 其 ...

  9. LeetCode 笔记系列13 Jump Game II [去掉不必要的计算]

    题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...

随机推荐

  1. mysql乐观锁总结和实践(转载)

    原文地址:http://chenzhou123520.iteye.com/blog/1863407 乐观锁介绍: 乐观锁( Optimistic Locking ) 相对悲观锁而言,乐观锁假设认为数据 ...

  2. LVM卷管理

    一.LVM是做什么的 LVM ( Logical Volume Manager ,逻辑卷管理器)LVM 是建立在磁盘和分区之上的一个逻辑层,用来提高磁盘分区管理的灵活性.LVM 可以对磁盘分区按照组的 ...

  3. json.loads 报错 json.decoder.JSONDecodeError

    json.loads报json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes 出现这个错误其实只 ...

  4. spring定时任务的配置式与注解式

    在定时任务配置文件中添加内容: <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi=&q ...

  5. For 循环的嵌套与九九乘法表

    ㈠通过程序,在页面中输入如下图形 * * * * * * * * * * * * * * * * * * * * * * * * *  代码如下: //向body中输入一个内容 //document. ...

  6. 初入SG-UAP

    初入SG-UAP SpriderMan 关注 2019.06.19 14:10 字数 1130 阅读 10评论 0喜欢 0 初次接触SG-UAP,将自己的见解以文字形式记录下来,希望能对初入的伙伴们有 ...

  7. Luogu P1903 [国家集训队]数颜色 or 维护队列

    标准的带修莫队...咕到了现在$qwq$ 莫队是对询问排序来优化复杂度的(不带修就是对询问区间$[l,r]$排序).. 那么现在带修了,我们再可以维护一个时间维度$tm$:对于每个询问,每次回答前先检 ...

  8. mac使用php-version切换PHP版本

    在开发过程中,有时候我们的程序对某个php版本有着极为重要的限制,特别是大型项目. 因此,我们就需要切换多个php版本来满足我们的需求. 我们使用php-version来达到这个目的. 首先我们先使用 ...

  9. Codeforces 126B. Password(KMP,DP)

    Codeforces 126B. Password 题意:一个字符串,找出最长的子串t,它既是前缀又是后缀,还出现在中间.输出t,不存在则输出Just a legend. 思路:利用KMP算法处理出n ...

  10. 记一次antlr错误:ANTLR Tool version 4.5.3 used for code generation does not match the current runtime version 4.7.2ANTLR

    场景:重构spark 2.1版本的sql语法.因此 需要使用antlr: 前期准备:idea安装了antlr插件(antlr的4.7.2版本) 因此在maven工程中添加了antlr的依赖: < ...