算法-动态规划-买股票的最佳时机-贪心算法

动态规划

动态规划 和 分治方法类似,都是采用组合子问题的形式去求解原问题。
解题步骤是:
①定义一个状态,表示最优解的结构特征;②进行状态递推,得到递推公式;③进行初始化;④返回结果。

贪心算法

贪心算法 = 在处理堆问题时,做出当前看来最好的选择,不从整体最优解上考虑,而是取某种意义上的局部最优解;
解题步骤是:

买股票的最佳时机I

题目描述

给定一个数组 prices ,其中 prices[i] 表示股票第 i 天的价格。返回值 = 能获得最大利润。
任何时刻 最多持有一股 股票,每天都可以决定卖出 或者 买进 股票。也可以同一天购买、出售。
在每一天,你可能会决定购买或出售股票。可以明确 某天可以既不购买也不卖出。

动态规划

分析

  • 定义最优解的结构特征:dp[][]二维数组的形式;
dp[i][0] 表示第i+1天手上没有股票的最大利润;
dp[i][1] 表示第i+1天手上有股票的最大利润;
当天交易结束后,手上没有股票的情况有两种:①原本手里没有股票②手里有股票但是卖出了;
当天交易结束后,手上有股票的情况有两种:①原本手上有股票②手里没有股票但买入了股票;
  • 初始化:
dp[0][0] = 0;
dp[0][1] = prices[0];
  • 状态递推,得到递推公式;
对于第i+1天手上没有股票的最大利润:i天手上没有;i天手上有又卖出了
dp[i][0] = Math.max(dp[i-1][0],dp[i][1]+price[i])
对于第i+1天手上有股票的最大利润:i天手上有;i天手上没有又买入了
dp[i][1] = Math.max(dp[i-1][1],dp[i-1][0]-price[i])
  • 返回结果;
return dp[len-1][0];

复杂度分析

复杂度解释
时间复杂度
O(n)
由于股票价格数组有n个元素,且每一天的股票状态有两种,那么总共会有2n个状态;每次状态转换的时间是O(1),则总的时间复杂度就是O(n)
空间复杂度
O(n)
由于我们需要开辟O(n)空间存储动态规划中的所有状态;

代码1 = 普通动态规划

    public static int maxProfit(int[] prices){
        if(prices.length <2 || prices == null){
            // 因为prices长度小于2 表示长度是0或1 等于1的时候,买入是亏,不买入就是0
            return 0;
        }
        int len = prices.length;
        int[][] dp = new int[len][2];
        dp[0][0] = 0;
        dp[0][1] = -prices[0];

        for(int i = 1 ; i < len; i++){
            dp[i][0] = Math.max(dp[i-1][0],dp[i-1][1]+ prices[i]);
            dp[i][1] = Math.max(dp[i-1][1], dp[i-1][0] -prices[i]);
        }
        // 返回的值是 最后一天手中没有股票的情况
        return dp[len - 1][0];
    }

代码2 = 采用滚动数组

    public int maxProfit(int[] prices) {
        // 采用滚动数组实现,由于每一天只需要前一天的情况,那么当前计算介绍之后,直接覆盖掉前一天的结果就好;
        int len = prices.length;
        // 设置最优解状态,以及初始值
        int[] dp = {0,-prices[0]};

        // 递推公式
        for(int i = 1; i < len;i++){
            dp[0] = Math.max(dp[0],dp[1]+prices[i]);
            dp[1] = Math.max(dp[1],dp[0]-prices[i]);
        }
        // 返回结果
        return dp[0];
    }

贪心算法

分析1 找上涨的最小值和上涨的最大值
只需要找到,开始上涨的最小值和股票上涨的最大值。通过index索引去查看。
找到上涨的最小值:prices[index] >= prices[index+1] 满足index++
找到上涨的最大值:prices[index] <= prices[index+1] 满足index++

分析2 上升区间的高度和
就是求上升区间的高度和。
找到上涨的最小值和最大值之间的差 也就是a<b<c<d,那么d-a = (b-a) + (c-b) + (d-c)相邻两个数之差
x (x-1) (x-2) (x+1) (x+3) (x+6) (x-2) (x-1) 这样情况下 上涨的最小值是(x+1) 最大值是(x+6),两者之差是5
按照公式计算 相邻两个数的差,两者之间是升序,则后-前>0;如果两者之间是降序,则后-前<0

复杂度

复杂度说明
时间复杂度
O(n)
只需要遍历股票价格数组中的n个元素
空间复杂度
O(1)
需要新创建一个变量total,用于存放要输出的获利

方式1:主要找上升的最小值和最大值 作差

  public static int maxProfit3(int[] prices){
        if (prices == null || prices.length < 2)
            return 0;
        int total = 0, index = 0, length = prices.length;
        while (index < length) {
            //如果股票下跌就一直找,直到找到股票开始上涨为止
            while (index < length - 1 && prices[index] >= prices[index + 1])
                index++;
            //股票上涨开始的值,也就是这段时间上涨的最小值
            int min = prices[index];
            //一直找到股票上涨的最大值为止
            while (index < length - 1 && prices[index] <= prices[index + 1])
                index++;
            //计算这段上涨时间的差值,然后累加
            total += prices[index++] - min;
        }
        return total;
    }

方式2:主要计算相邻两个数的差,Math.max(两数之差,0),表示正数取前者,负数取0

    public static int maxProfit4(int[] prices){
        if (prices == null || prices.length < 2)
            return 0;
        int total = 0;
        for(int i = 0 ; i < prices.length-1 ; i++){
            total += Math.max(prices[i+1] - prices[i],0);
        }
        return total;
    }

买股票的最佳时机II

题目描述

给定一个数组 prices ,它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格。
你只能选择 某一天 买入这只股票,并选择在 未来的某一个不同的日子 卖出该股票。设计一个算法来计算你所能获取的最大利润。
返回你可以从这笔交易中获取的最大利润。如果你不能获取任何利润,返回 0 。

暴力破解

分析
采用for循环的形式实现,外层循环表示买股票,内层循环表示卖股票,最终通过内层减去外层的值 表示最后的利润;

代码

class Solution {
    public int maxProfit(int[] prices) {
        // 双层for循环实现
        int len = prices.length;
        int max = 0;
        for(int i = 0; i<len-1;i++){
            for(int j = i+1; j< len;j++){
                max = Math.max(max,prices[j]-prices[i]);
            }
        }
        return max;
    }
}

两个变量+一次循环

分析
定义两个变量,价格的最小值minPrice = 最佳买入点,最大利润maxProfit;
初始值,前者是Integer.MAX_VALUE,后者是-1;
通过for循环遍历每天的价格prices数组,找到prices[i] < minPrice 则替换掉minPrice
再计算当前prices[i] - minPrice的值,如果大于maxProfit,则替换;

代码

class Solution {
    public int maxProfit(int[] prices) {
        int minPrice = Integer.MAX_VALUE;
        int maxProfit = -1;
        for(int i = 0 ;i < prices.length;i++){
            minPrice = Math.min(prices[i],minPrice);
            maxProfit = Math.max(prices[i]-minPrice,maxProfit);
        }
        return maxProfit;
    }
}

动态规划

分析
定义状态、初始值、状态转移方程、返回结果。
状态有两种情况:
①第i天结束,手上没有股票;第i天结束,手上有股票;
初始值:

dp[0][0] = 0;
dp[0][1] = -prices[0];

状态方程:

①第i天结束,手上没有股票;
前一天没有;前一天有,但是卖出了;
dp[i][0] = Math.max(dp[i-1][0],dp[i-1][1] +prices[i]);
②第i天结束,手上有股票
前一天有;前一天没有,直接买入;
dp[i][1] = Math.max(dp[i-1][1],-prices[i]);

返回值:

返回值dp[len-1][0]

代码

class Solution {
    public int maxProfit(int[] prices) {
        // 设置状态
        int len = prices.length;
        int[][] dp = new int[len][2];

        // 初始值
        dp[0][0] = 0;
        dp[0][1] = -prices[0];

        // 状态转移方程
        for(int i = 1 ; i < len ; i++){
            dp[i][0] = Math.max(dp[i-1][0],dp[i-1][1]+prices[i]);
            dp[i][1] = Math.max(dp[i-1][1],-prices[i]);
        }
        return dp[len-1][0];
    }
}
方法三:本质最大子序和

定义两个变量,result = 最终输出结果,sum = 子序列的差值
通过for循环遍历prices中的每一个元素i,sum = 计算prices[i] - prices[i-1]的差值,
再取result和sum的最大值赋值给result,接着判断sum是否小于0,小于则sum=0

时间复杂度O(n)由于遍历了一次,遍历次数就是prices数组的元素个数;
空间复杂度O(1)只是用了常数个变量。
方法3:最大子序和
public static int maxProfit2(int[] prices){
        if(prices.length < 2 || prices == null){
            return 0;
        }
        int  result = 0;
        int   sum = 0;
        for(int i = 1 ;i <prices.length ; i++){
            sum += (prices[i] - prices[i-1]);
            result = Math.max(sum,result);
            if(sum < 0) sum = 0;
        }
        return result;
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值