五道股票题总结:

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. PAT Advanced 1155 Heap Paths (30 分)

    In computer science, a heap is a specialized tree-based data structure that satisfies the heap prope ...

  2. LoadRunner(4)

    一.LoadRunner工具的组成 1.VuGen 虚拟用户脚本生成器 脚本好比:武器 VuGen好比:兵工厂 VU好比:士兵 2.Controller 压力调度控制台 好比:总指挥部 3.Analy ...

  3. 一步步实现ArcMenu效果

    先来看一下最终要实验的效果: 是不是跟国外的一款Path的菜单效果类似,这里的动画采用补间动画去实现,正而操练一下补间动画. 布局和子视图的测量处理: 新建一自定义View继承ViewGroup: 然 ...

  4. Python&Selenium 数据驱动【unittest+ddt+json+HTMLTestRunner】

    一.摘要 本博文将介绍Python和Selenium做自动化测试的时候,基于unittest框架,借助ddt模块使用json文件作为数据文件作为测试输入,最后借助著名的HTMLTestRunner.p ...

  5. 编译teamtalk遇到的问题

    一.编译log4cxx遇到的问题 1.error: narrowing conversion 这是在gcc-6下面一个官方的错误 解决方法 https://issues.apache.org/jira ...

  6. loj515 「LibreOJ β Round #2」贪心只能过样例[bitset+bool背包]

    由于bitset极其不熟练且在实际题目中想不起来运用它来优化,于是练了几道题. 这题是一个分组的bool背包,每组必须选一个,暴力的话是$O(n^5)$. 如果dp数组不要一维滚动的话,有两种枚举方法 ...

  7. css3 制作圆环进度条

    引子 移动端做一个 loadiing 加载的图标,跟以往沿用的都不太一样,是一个圆环进度条,圆环进度条也就罢了,还得能用百分比控制. CSS3 实现圆环 demo 刚开始写这个圆环的时候是参照帖子上给 ...

  8. mysql自增主键清零方法

    MySQL数据库自增主键归零的几种方法 如果曾经的数据都不需要的话,可以直接清空所有数据,并将自增字段恢复从1开始计数: truncate table table_name; 1 当用户没有trunc ...

  9. Redis:RedisHelper(5)

    /// <summary> /// Redis 助手 /// </summary> public class RedisHelper { /// <summary> ...

  10. JavaScript数组的简单介绍

    ㈠对象分类 ⑴内建对象 ⑵宿主对象 ⑶自定义对象   ㈡数组(Array) ⑴简单介绍 ①数组也是一个对象 ②它和我们普通对象功能类似,也是用来存储一些值的 ③不同的是普通对象是使用字符串作为属性名的 ...