五道股票题总结:

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. 2019牛客暑期多校训练营(第一场)E ABBA

    题意问你有多少个长度为2*(n+m)的字符串满足A和B数量相等 且可以分割为n个AB子序列和m个BA子序列 很容易得出前n个A肯定是可以给AB的 后面的m个A给BA 所以当一个字符串满足条件时要满足任 ...

  2. python3:iterable, iterator, generator,抽象基类, itertools的使用。

    目录: iterable对象 iterator对象, 数据类型Iterator类 数据类型Generator类. 生成器表达式 collections.abc:容器的抽象基类.用于判断具体类. ite ...

  3. 05—动态sql

    1.创建表 CREATE TABLE tb_employee ( ID INT(11) PRIMARY KEY AUTO_INCREMENT, loginname VARCHAR(18), PASSW ...

  4. BZOJ2278 [Poi2011]Garbage[欧拉回路求环]

    首先研究环上性质,发现如果状态不变的边就不需要动了,每次改的环上边肯定都是起末状态不同的边且仅改一次,因为如果有一条边在多个环上,相当于没有改,无视这条边之后,这几个环显然可以并成一个大环.所以,我们 ...

  5. connect: network is unreachable

    解决方法: 在确保完善网卡驱动,以及确保将网卡驱动编译进内核后,检查 ls /etc/sysconfig/network-script/ifcfg-eth0 一.看是否在上述目录下存在ifcfg-et ...

  6. React-router的使用:标签跳转和编程式跳转

    目录: 1.demo:NavLink 标签跳转 2.标签渲染路由组件时,获取url参数 3.编程式跳转 参考文档 1)https://reacttraining.com/react-router/we ...

  7. 无法识别的配置节点 applicationSettings/* Properties.Settings 解决方法

    http://blog.csdn.net/yaoxtao/article/details/7766888 在项目中引用web service时,偶然出现 无法识别的配置节点 applicationSe ...

  8. python实现 单链表的翻转

    #!/usr/bin/env python #coding = utf-8 class Node: def __init__(self,data=None,next = None): self.dat ...

  9. Codeforces 1205C Palindromic Paths (交互题、DP)

    题目链接 http://codeforces.com/contest/1205/problem/C 题解 菜鸡永远做着变巨的梦 然而依然连div1BC题都不会做 要是那天去打cf怕是又要1题滚粗了.. ...

  10. CSS高级学习-1

    优先级 权值 标签权值为1,类权值为10,ID权值最高为100. p{color:red;} /*权值为1*/ p span{color:green;} /*权值为1+1=2*/ .warning{c ...