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. AppCompatActivity与toolbar的结合
  2. 奥比中光大白(3D结构光)摄像头测试发现对着灯光过曝问题
  3. redis 底层数据结构 压缩列表 ziplist
  4. JDATA绝对语义识别挑战大赛-季军方案
  5. 关闭窗体后,进程仍然在运行的问题重现与解决
  6. 在C#中使用SerialPort类实现串口通信 遇到多线程问题
  7. bzoj 3232 01分数规划+最大权封闭子图判定
  8. 解决“A problem has been encountered while loading the setup components. Canceling setup.”的问题...
  9. MySQL进阶操作之视图
  10. javascript在IE和Firefox中兼容性问题
  11. WPF 使用值转换器进行绑定数据的转换IValueConverter
  12. 基于新浪微博评论的情感分析
  13. PTA-哥尼斯堡的“七桥问题” (20 分)
  14. 11月,匆匆而过,留下了遗憾(亚洲赛广州站)
  15. 最好的Google表格插件
  16. 如履薄冰:Redis 懒惰删除的巨大牺牲
  17. 借助 PrivateLink 与 EMQX Cloud 建立安全可靠的连接
  18. ae合成设置快捷键_AE模板 三秒速成的抖音加关注动画模板
  19. 机器学习之红楼梦作者判断(贝叶斯分类)
  20. php cms带专题,phpcms标签模板及专题模板的制作

热门文章

  1. 用JScript ActiveXObject ,打开word时 显示“word无法启动转换器mswrd632 wpc”
  2. 基于SSM的中学学生学籍管理系统设计与实现
  3. OSChina 周六乱弹 —— 这才是高考留给我们的财富
  4. HOOK API技术
  5. Android调用程序读取RTF文件
  6. likeshop多商户商城系统发布新版本-v2.2.1
  7. 根据url获取文件信息
  8. 首款国内自主研发四核——华为MediaPad 10 FHD首发评测
  9. Name or service not known异常处理方法总结
  10. 【附源码】Java计算机毕业设计高校奖学金评定管理系统(程序+LW+部署)