121买卖股票的最好时机--力扣题解

题目描述:
给定一个数组 prices ,它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格。

你只能选择 某一天 买入这只股票,并选择在 未来的某一个不同的日子 卖出该股票。设计一个算法来计算你所能获取的最大利润。

返回你可以从这笔交易中获取的最大利润。如果你不能获取任何利润,返回 0 。

在这里插入图片描述

方法一:暴力法
这是我想到的第一个思路
使用两个for循环,遍历数组的每一个元素,对于元素 i ,再依次遍历该元素后面的每一个元素,找出最大差值

class Solution {
    public int maxProfit(int[] prices) {
        int profit = 0;
        for (int i=0;i<prices.length;i++){
            for (int j=i+1;j<prices.length;j++){
// 有利润,可以卖出

                    if (prices[j]-prices[i]>profit){
                        profit = prices[j]-prices[i];
                    }
                
            }
        }

        return profit;
    }
}

但是在力扣的测试中,这个方法不能通过,原因是:超出时间限制
有一些测试用例的数组元素很多,所以超出了时间限制

分析:
这种方法需要两个for循环
时间复杂度:O(n2
空间复杂度:O(1)

方法二:
因为方法一中,执行次数太多导致查出时间限制,因此要想办法减少执行次数
一次遍历数组,记录最小元素,也就是股票价格最低点,计算每一个元素和最小元素的差值,求最大的差值

class Solution {
    public int maxProfit(int[] prices) {
            int profit = 0;
        int minPrice = prices[0];
        for (int i=0;i<prices.length;i++){
            if (prices[i]<minPrice){
                minPrice = prices[i];
            }
            else if (prices[i]-minPrice>profit){
                profit = prices[i]-minPrice;
            }
        }
        return profit;
    }
}

分析:
这种方法只需要遍历一次数组
时间复杂度:O(n)
空间复杂度:O(1)

方法三:
这是在评论区看到的一个方法,状态定义方法(来自力扣的DUNCAN用户)

class Solution {
    public int maxProfit(int[] prices) {
        int len = prices.length;
        int res = 0;
        // 前一天卖出可以获得的最大利润
        int pre = 0;
        for (int i = 1; i < len; i++) {
            // 利润差
            int diff = prices[i] - prices[i - 1];
           // 状态转移方程:第i天卖出可以获得的最大利润 = 第i-1天卖出的最大利润 + 利润差
            pre = Math.max(pre + diff, 0);
            res = Math.max(res, pre);
        }
        return res;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值