You are given an array prices where prices[i] is the price of a given stock on the ith day.

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

You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.

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

Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.

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

Example 1:示例 1:

Input: prices = [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.

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

Example 2:示例 2:

Input: prices = [7,6,4,3,1]
Output: 0
Explanation: In this case, no transactions are done and the max profit = 0.

输入:prices = [7,6,4,3,1]
输出:0
解释:在这种情况下, 没有交易完成, 所以最大利润为 0。

Constraints:提示:

1 <= prices.length <=
0 <= prices[i] <=

C语言:

int maxProfit(int* prices, int pricesSize){int diff=0;int min=prices[0];for(int i=1;i<pricesSize;i++){if(min>prices[i])min=prices[i];elsediff=(prices[i]-min)>diff?prices[i]-min:diff;}return diff;
}

执行结果:通过

执行用时:120 ms, 在所有 C 提交中击败了43.28%的用户

内存消耗:12.6 MB, 在所有 C 提交中击败了62.32%的用户

通过测试用例:211 / 211

C语言:

int maxProfit(int* prices, int pricesSize){int diff=0;int min=prices[0];for(int i=1;i<pricesSize;i++){if(min>prices[i])min=prices[i];if(prices[i]-min>diff)diff=prices[i]-min;}return diff;
}

执行结果:通过

执行用时:124 ms, 在所有 C 提交中击败了32.14%的用户

内存消耗:12.5 MB, 在所有 C 提交中击败了89.69%的用户

通过测试用例:211 / 211

C语言:

int maxProfit(int* prices, int pricesSize){int diff=0;int min=prices[0];for(int i=1;i<pricesSize;i++){if(min>prices[i])min=prices[i];elsediff=(prices[i]-min)>diff?prices[i]-min:diff;}return diff;
}

执行结果:通过

执行用时:120 ms, 在所有 C 提交中击败了43.28%的用户

内存消耗:12.6 MB, 在所有 C 提交中击败了62.32%的用户

通过测试用例:211 / 211

C语言:

int maxProfit(int* prices, int pricesSize){int max=0;int sum=0;for(int i=1;i<pricesSize;i++){sum+=prices[i]-prices[i-1];if(sum>max)max=sum;if(sum<0)sum=0;}return max;
}

执行结果:通过

执行用时:116 ms, 在所有 C 提交中击败了61.56%的用户

内存消耗:12.8 MB, 在所有 C 提交中击败了15.39%的用户

通过测试用例:211 / 211

7        1        5        3        6        4

-6        4        -2        3        -2

4+(-2)+3=5

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. Leetcode NO.121 Best Time To Buy And Sell Stock 买卖股票时间

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

  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(股票买入卖出的最佳时间)

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

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

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

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

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

  7. 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 ...

  8. 121 Best Time to Buy and Sell Stock

    输入:一个数组prices,prices[i]表示第i天股票的价格. 输出:买卖股票的最大收益. 规则:只允许最多买一次,最多卖一次股票.如果觉得价格不合适,可以不买卖. 分析1:最先想到的是暴力搜索 ...

  9. 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 were ...

最新文章

  1. 郭为:大数据时代的企业管理挑战
  2. 【组织】请13级1/2/3班,14级1/2班 将同学们的博客地址列个清单回复(按班级来)...
  3. 数据流中的中位数,我轻敌了
  4. Java EE 7:带有Glassfish v4的JMS 2.0
  5. java的核心类库_Java核心类库,集合框架
  6. sphinx文档_使用Sphinx构建自定义文档工作流
  7. phpStudy配置站点 解决You don't have permission to access / on this server
  8. linux 版本信息 64位,Centos查看版本信息
  9. python字符串转整数_Python连接字符串和整数
  10. 2021-10-13
  11. 442.数组中重复的数据
  12. nupkg 本地安装_使用Nuget安装脱机软件包nupkg
  13. lvuaagentinstbaseroot_桌面小助手UniAgent删除指南
  14. find_calib_object算子说明
  15. TimescaleDB Continuous Aggregates介绍
  16. eureka心跳_8个Eureka优化技巧,让效率提高10倍
  17. 小议阿里云数加平台对企业有何帮助?
  18. INE深圳硅谷双线发布演讲实录 | CEOCTO羽睿-去中心化的最后一块版图
  19. WC2015 滚粗记
  20. [iOS]NSInteger int long区别

热门文章

  1. php访问腾讯云cos,腾讯云COS的一次实践
  2. java如果将信息推送到公众号_Java 推送微信公众号消息
  3. android集成测试框架,Android单元测试框架选择
  4. [游记] HEOI2018酱油记
  5. imgaug数据增强神器:第十一章 复制随机状态和使用多个增强序列
  6. 推荐最好的四款Linux/BSD防火墙
  7. 忽然之间发现自己还有胜算
  8. cooki与session区别
  9. 小学生python编程---忍者大战
  10. Excel学习笔记:P37-这是什么巫术?我弄半天的表格,结果旁边的同事弹指一挥间就全部做好了!