文章目录

  • 1.问题描述
  • 2.测试用例
    • 示例 1
    • 示例 2
  • 3.提示
  • 4.代码
    • 1.暴力
      • code
      • 复杂度
    • 2.动态规划
      • code
      • 复杂度

1.问题描述

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

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

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

2.测试用例

示例 1
输入:[7,1,5,3,6,4]
输出:5
解释:在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = 6-1 = 5 。注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价格;同时,你不能在买入前卖出股票。
示例 2
输入:prices = [7,6,4,3,1]
输出:0
解释:在这种情况下, 没有交易完成, 所以最大利润为 0。

3.提示

  • 1 <= prices.length <= 105
  • 0 <= prices[i] <= 104

4.代码

1.暴力
code
public int maxProfitWithForce(int[] prices) {int maxProfit = 0;for (int i = 1; i < prices.length - 1; i++) {for (int j = i; j < prices.length; j++) {maxProfit = Math.max(prices[j] - prices[i], maxProfit);}}return maxProfit;
}
复杂度
* 时间复杂度 O(n^2)
* 空闲复杂度 O(1)
2.动态规划
code
public int maxProfitWithDp(int[] prices) {int maxProfit = 0;int minNum = Integer.MAX_VALUE;for (int i = 0; i < prices.length; i++) {minNum = Math.min(minNum, prices[i]);maxProfit = Math.max(maxProfit, prices[i] - minNum);}return maxProfit;
}
复杂度
* 时间复杂度 O(n)
* 空间复杂度 O(1)

Leetcode NO.121 Best Time To Buy And Sell Stock 买卖股票时间相关推荐

  1. 121 Best Time to Buy and Sell Stock 买卖股票的最佳时机

    假设你有一个数组,其中第 i 个元素是一支给定股票第 i 天的价格. 如果您只能完成最多一笔交易(即买入和卖出一股股票),则设计一个算法来找到最大的利润. 示例 1: 输入: [7, 1, 5, 3, ...

  2. 121. Best Time to Buy and Sell Stock买卖股票的最佳时机

    You are given an array prices where prices[i] is the price of a given stock on the ith day. 给定一个数组 p ...

  3. [LeetCode] Best Time to Buy and Sell Stock 买卖股票的最佳时间

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  4. leetcode 121.Best Time to Buy and Sell Stock 股票买卖最佳时间

    题目: Say you have an array for which the ith element is the price of a given stock on day i. If you w ...

  5. LeetCode 121 Best Time to Buy and Sell Stock(股票买入卖出的最佳时间)

    翻译 话说你有一个数组,其中第i个元素表示在第i天的股票价格.如果你被只被允许最多一次交易(例如,买入然后卖出一个股票),设计一个算法并找出最大利润. 原文 Say you have an array ...

  6. 【贪心 和 DP + 卖股票】LeetCode 121. Best Time to Buy and Sell Stock

    LeetCode 121. Best Time to Buy and Sell Stock Solution1:我的答案 动态规划和贪心不要区分的那么明显嘛~~~ class Solution { p ...

  7. 【LeetCode】309. Best Time to Buy and Sell Stock with Cooldown 最佳买卖股票时机含冷冻期(Medium)(JAVA)

    [LeetCode]309. Best Time to Buy and Sell Stock with Cooldown 最佳买卖股票时机含冷冻期(Medium)(JAVA) 题目地址: https: ...

  8. [LeetCode]Buy and Sell Stocks 买卖股票问题

    LeetCode上关于买卖股票的问题一共有五道,题号分别为121,122,123,188,309. 此类问题的基本描述即给出一个序列prices[],prices[i]代表第i天股票的价格. 如果当天 ...

  9. 【LeetCode】309. Best Time to Buy and Sell Stock with Cooldown 解题报告(Python C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

最新文章

  1. 设计模式 | 适配器模式及典型应用
  2. Linux按键输入实验(体验一下输入驱动,实际开发使用input子系统处理)
  3. LeetCode 893 Groups of Special-Equivalent Strings 解题报告
  4. java设计模式中的动态代理
  5. We7 从这里开始---we7的功能和特点(3)
  6. 微信云支付so seay
  7. 01 - vulhub - ActiveMq - CVE-2015-5254
  8. dbf解析_DBF文件格式分析.doc
  9. Smart Thief 问题
  10. 菜学C++ Day55 OJ题目1151桐桐数
  11. 计算机动漫游戏与制作,计算机动漫与游戏制作标准规范.doc
  12. TIM1_CH1N和TIM1_CH1的区别和控制
  13. win10下java的下载、安装和配置环境教程,超级详细
  14. 科研伦理与学术规范-期末考试答案
  15. 金誉半导体:MOS管耗尽型和增强型是什么意思?
  16. 【N32G457】基于RT-Thread和N32G457的墨水屏日历
  17. 开发webgl应用时,cesium快速定位相机角度、相机位置的方法
  18. Android自定义的下拉列表框控件
  19. 探寻Beacon技术
  20. version libmysqlclient_18 not defined in file libmysqlclient.so.18 with link time reference

热门文章

  1. linux oracle用户解锁
  2. java入门书籍,linux资料
  3. ceph存放mysql备份_Kubernetes持久化Ceph存储
  4. java实现踢下线用户_java中如何踢人下线?封禁某个账号后使其会话立即掉线!...
  5. lineheight使图片多行文字垂直居中
  6. 传神助力进博会语言服务全覆盖 ,专业译员、翻译神器齐上阵
  7. python wav文件音频频谱图音量分析,静音截取
  8. 正交补集(Orthogonal Complements)
  9. Java初级面试题!!自己出几道面试题,感觉很有意思
  10. emui11鸿蒙更新名单,华为emui11系统什么时候可以升级_emui11更新名单