备战秋招60天算法挑战,Day6

题目链接: https://leetcode.cn/problems/best-time-to-buy-and-sell-stock/

视频题解: https://b23.tv/MDoza2J

LeetCode 121.买卖股票的最佳时机

题目描述

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

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

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

举个例子:

输入:[7,1,5,3,6,4]
输出:5
解释:在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = 6-1 = 5 。
注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价格;同时,你不能在买入前卖出股票。

视频题解

买卖股票的最佳时机

思路来源

思路来源

思路解析

暴力枚举比较简单但时间复杂度比较高,我们直接跳过。买卖股票讲究的就是逢低买入,同时还要关注买卖的时序,要先买才能卖。

定义三个变量,buy_price表示买入价,cur_price表示当前价格,max_profit表示最大利润。

下面给出核心算法:

  1. 如果cur_price < buy_price,就更新buy_price = cur_price(符合逢低买入的思想)。否则就计算以当前价格卖出获得的利润,如果利润大于max_profit,就更新max_profit = cur_price - buy_price。进入步骤2
  2. 重复执行步骤1,直到遍历完prices数组。

根据上面的算法,遍历一遍数组,枚举股票的所有卖出价,在股票卖出时我们始终保证买入价buy_price是目前为止最低的,这是解决本题的关键,只要遍历一遍数组prices就可得到最大利润值。

C++代码

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int prices_len = prices.size();
        int buy_price = prices[0];
        int max_profit = 0;
        for (int i = 1; i < prices_len; ++i) {
            if (prices[i] < buy_price) {
                //逢低买入
                buy_price = prices[i];
            } else if (prices[i] - buy_price > max_profit) {
                //更新最大利润    
                max_profit = prices[i] - buy_price; 
            }
        }
        return max_profit;
    }
};

java代码

class Solution {
    public int maxProfit(int[] prices) {
        int prices_len = prices.length;
        int buy_price = prices[0];
        int max_profit = 0;
        for (int i = 1; i < prices_len; ++i) {
            if (prices[i] < buy_price) {
                // 逢低买入
                buy_price = prices[i];
            } else if (prices[i] - buy_price > max_profit) {
                // 更新最大利润
                max_profit = prices[i] - buy_price;
            }
        }
        return max_profit;
    }
}

python代码

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        prices_len = len(prices)
        buy_price = prices[0]
        max_profit = 0
        for i in range(1, prices_len):
            if prices[i] < buy_price:
                # 逢低买入
                buy_price = prices[i]
            elif prices[i] - buy_price > max_profit:
                # 更新最大利润
                max_profit = prices[i] - buy_price
        return max_profit

复杂度分析

时间复杂度: 整个过程只遍历一遍数组,时间复杂度为O(n)nprices数组的长度。

空间复杂度: 只使用了3个整型变量,所以空间复杂度为O(1)

  • 6
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值