算法最优解 算法最优解
首页
目录
赞助
GitHub (opens new window)
首页
目录
赞助
GitHub (opens new window)
  • 数据结构基础

  • 数组

    • 两数之和
    • 买卖股票的最佳时机
      • 题目描述
      • 视频讲解
      • 思路解析
      • C++代码
      • java代码
      • python代码
      • 复杂度分析
    • 存在重复元素
    • 除自身以外数组的乘积
    • 最大子数组和
    • 和为 K 的子数组
    • 乘积最大子数组
    • 轮转数组
    • 寻找旋转排序数组中的最小值
    • 搜索旋转排序数组
    • 盛最多水的容器
    • 接雨水
    • 三数之和
    • 移动零
  • 位运算

  • 动态规划

  • 图

  • 区间

  • 链表

  • 矩阵

  • 字符串

  • 树

  • 堆

  • 逻辑思维

  • 目录
  • 数组
华南溜达虎
2024-07-08
目录

买卖股票的最佳时机

题目链接: 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, 因为卖出价格需要大于买入价格;同时,你不能在买入前卖出股票。

# 视频讲解

建议大家点击视频跳转到b站买卖股票的最佳时机 (opens new window)观看,体验更佳!

# 思路解析

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

定义三个变量,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),n为prices数组的长度。

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

上次更新: 2024/07/13, 21:23:13
两数之和
存在重复元素

← 两数之和 存在重复元素→

Theme by Vdoing | Copyright © 2024-2024 华南溜达虎 | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式