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

If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.

这道题相当简单,感觉达不到Medium的难度,只需要遍历一次数组,用一个变量记录遍历过数中的最小值,然后每次计算当前值和这个最小值之间的差值最为利润,然后每次选较大的利润来更新。当遍历完成后当前利润即为所求,代码如下:

C++ 解法:

class Solution {
public:int maxProfit(vector<int>& prices) {int res = 0, buy = INT_MAX;for (int price : prices) {buy = min(buy, price);res = max(res, price - buy);}return res;}
};

Java 解法:

public class Solution {public int maxProfit(int[] prices) {int res = 0, buy = Integer.MAX_VALUE;for (int price : prices) {buy = Math.min(buy, price);res = Math.max(res, price - buy);}return res;}
}

类似题目:

Best Time to Buy and Sell Stock with Cooldown

Best Time to Buy and Sell Stock IV

Best Time to Buy and Sell Stock III

Best Time to Buy and Sell Stock II

LeetCode All in One 题目讲解汇总(持续更新中...)

转载于:https://www.cnblogs.com/grandyang/p/4280131.html

[LeetCode] Best Time to Buy and Sell Stock 买卖股票的最佳时间相关推荐

  1. Leetcode NO.121 Best Time To Buy And Sell Stock 买卖股票时间

    文章目录 1.问题描述 2.测试用例 示例 1 示例 2 3.提示 4.代码 1.暴力 code 复杂度 2.动态规划 code 复杂度 1.问题描述 给定一个数组 prices ,它的第 i 个元素 ...

  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. 121 Best Time to Buy and Sell Stock 买卖股票的最佳时机

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

  4. leetcode: Best Time to Buy and Sell Stock 系列

    leetcode: Best Time to Buy and Sell Stock 系列 一系列包括: - Best Time to Buy and Sell Stock Ⅰ - Best Time ...

  5. Leetcode Best Time to Buy and Sell Stock III

    Leetcode Best Time to Buy and Sell Stock III,本算法的关键为找出其动态子结构.可以发现,序列中的最小值可以做为其的一个分割,令左边序列为left,右边的序列 ...

  6. Leetcode Best Time to Buy and Sell Stock

    Leetcode Best Time to Buy and Sell Stock 相关代码,本题使用dp算法完成,本算应该算得上一个经典的dp算法题. #include <iostream> ...

  7. LeetCode Best Time to Buy and Sell Stock II

    原题链接在这里:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ 题目: Say you have an array ...

  8. [leetcode]_Best Time to Buy and Sell Stock I II

    一个系列三道题,我都不会做,google之答案.过了两道,第三道看不懂,放置,稍后继续. 一.Best Time to Buy and Sell Stock I 题目:一个数组表示一支股票的价格变换. ...

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

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

最新文章

  1. c++ list 容器
  2. 二、安装Spark集群
  3. 读债务危机0806:2007到2011年泡沫蔓延
  4. sql优化之:数据库索引创建原则,or/in/union与索引优化,聚集索引/非聚集索引/联合索引/索引覆盖,MySQL冗余数据的三种方案,MySQL双主一致性架构优化(来源:架构师之路)
  5. python 笔记 之 函数中默认参数的影响
  6. 自定义parallelStream的thread pool
  7. 【机器学习】传统目标检测算法总结
  8. MySQL 集群方案介绍
  9. windows下的gvim配置
  10. 模拟ios_王者荣耀策划Donny:安卓IOS今年或实现互通!模拟战一周一更新
  11. 嵌入不同源的页面_嵌入式技术课程教与学(教学大纲和试卷)
  12. scala求集合相邻元素差值的最小值
  13. eclipse tomcat maven热部署
  14. Linux shell 中的那些你值得一试的小把戏
  15. android自定义组件(手机加速球+水面波动效果)
  16. 怎么简单的生成SSL证书
  17. css3中translateY、translateX的使用
  18. 图像识别技术初探(2014/2/17)
  19. 微信公众号服务器端脑图,微信公众号中隐藏的思维导图工具,帮你随时随地高效思考...
  20. 2022长三角产业区块链生态图谱 附下载

热门文章

  1. pythondd_windows7下安装python环境和django
  2. 安卓采集摄像头画面生成MP4文件
  3. pojo类不能有默认值怎么办_打印机不能打印是什么原因 打印机不能打印处理方法介绍【详解】...
  4. 我的docker随笔35:jenkins服务部署
  5. python游戏编程快速上手豆瓣_Python游戏编程快速上手(第4版)
  6. 【算法】剑指 Offer 67. 把字符串转换成整数
  7. 【ElasticSearch】es 压缩 你的 数据
  8. 【Elasticsearch】es 7.8.0 java 实现 BulkRequest 批量写入数据
  9. 【Kafka】Elasticsearch 与 Kafka 整合剖析
  10. 自定义 Git - Git 钩子