题目:
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.

Example 1:
Input: [7, 1, 5, 3, 6, 4]
Output: 5

max. difference = 6-1 = 5 (not 7-1 = 6, as selling price needs to be larger than buying price)

Example 2:
Input: [7, 6, 4, 3, 1]
Output: 0

In this case, no transaction is done, i.e. max profit = 0.

思路:
买卖股票问题,毕竟股票专业操作员,要知道卖肯定是在买之后。设置最小的索引min,最大利润max。如果当前位置的股票价格比min处小,更新min值 ;如果此时的利润大于max,更新max值。

代码:

class Solution {
public:int maxProfit(vector<int>& prices) {int min=0,max=0;//设置最小的索引min,最大利润maxfor(int i=1;i<prices.size();++i){if(prices[i]<prices[min]){//如果当前位置的股票价格比min处小,更新min值 min=i;}else if(prices[i]-prices[min]>max){如果此时的利润大于max,更新max值max=prices[i]-prices[min];}}return max;}
};

LeetCode 121. Best Time to Buy and Sell Stock相关推荐

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

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

  2. Leetcode——121. Best Time to Buy and Sell Stock

    题目原址 https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/ 题目描述 Say you have an ...

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

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

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

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

  6. [LeetCode]122. Best Time to Buy and Sell Stock II

    [LeetCode]122. Best Time to Buy and Sell Stock II 题目描述 思路 I的后续 将数组分为几个小部分, 划分标准是 [i] < [i - 1](划分 ...

  7. LeetCode 123. Best Time to Buy and Sell Stock III--Python解法--动态规划--数学题

    此文首发于我的个人博客:zhang0peter的个人博客 LeetCode题解文章分类:LeetCode题解文章集合 LeetCode 所有题目总结:LeetCode 所有题目总结 题目地址:Best ...

  8. leetcode 714. Best Time to Buy and Sell Stock with Transaction Fee | 714. 买卖股票的佳最时机含手续费(递归->傻缓存->dp)

    题目 https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/ 题解 经典的 暴力递归 - ...

  9. 【DP + 卖股票】LeetCode 714. Best Time to Buy and Sell Stock with Transaction Fee

    LeetCode 714. Best Time to Buy and Sell Stock with Transaction Fee Solution1: 参考网址:http://www.cnblog ...

最新文章

  1. 关于C#中的DLLImport (引)
  2. 企业安全体系建设方案设计(内附案例)
  3. 邮件服务器 文件服务器,搭建邮件、终端和文件服务器应用方案_服务器_服务器x86服务器-中关村在线...
  4. python 对excel文件进行分词并进行词频统计_python 词频分析
  5. MySQL 服务挂了 CPU 消耗接近 100% 你知道怎么回事吗???
  6. java linux runtime 正则表达式_Java学习之正则表达式
  7. 20200119:(leetcode)回文数(3种解法)
  8. win8 附件数据库失败解决方案《1》
  9. CentOS 7中 rsync 备份数据使用实例
  10. 【译】x86程序员手册13-第5章 内存管理
  11. [转载]运维自动化201009
  12. linux vi 文件查找,linux几种常见的文件内容查找和替换命令
  13. Java 每半年就会更新一次新特性,再不掌握就要落伍了:Java17 的新特性
  14. 60+张PPT,教你如何搭建Spark离线数仓!
  15. 如何获取到javasript链接的网址
  16. m基于QPSK调制解调的无线图像传输matlab仿真,包括扩频解扩均衡等模块
  17. word设置章标题与节标题
  18. 【数据库】数据库编程
  19. zbb20181224 win7 下的open live writer代码插件
  20. MATLAB读RGB888数据显示图片

热门文章

  1. python最简分数_592. 分数加减法运算(Python)
  2. linux那些事之page table
  3. 2021.08.26学习内容 Win10+GeForce GTX1650安装NVIDIA显卡驱动及CUDA11.4+cuDNN8.2
  4. 【pytorch】torch.nn.Identity()
  5. 经典网络分析 - Very Deep Convolutional Networks for Large-Scale Image Recognition(VGG)
  6. vue Cli 环境删除与重装 - 版本文档
  7. HTML复古游戏官网模板
  8. 左右黑白极简滚动个人主页模板
  9. web服务器检测工具
  10. 校园兼职跑腿源码+网页版(非小程序)