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

1:遍历数组;2:每个数字获得该数字之前的最小的数字,并与当时保存的最大值相比較

    int maxProfit(vector<int> &prices){if(prices.size() <= 1){return 0;}int maxValue = 0;int minPrice = prices[0];int size = (int)prices.size();for(int i = 1; i < size; i++){if(prices[i] > minPrice){maxValue = (maxValue > prices[i] - minPrice ? maxValue : prices[i] - minPrice);}else{minPrice = prices[i];}}return maxValue;}

99_leetcode_Best Time to Buy and sell Stock相关推荐

  1. LeetCode 309. Best Time to Buy and Sell Stock with Cooldown--Java解法-卖股票系列题目

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

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

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

  3. LeetCode 122. Best Time to Buy and Sell Stock II--贪心--Java,C++,Python解法

    题目地址:Best Time to Buy and Sell Stock II - LeetCode Say you have an array for which the ith element i ...

  4. [LeetCOde][Java] Best Time to Buy and Sell Stock III

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

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

  6. 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/ 题解 经典的 暴力递归 - ...

  7. 【LeetCode】 Best Time to Buy and Sell Stock I II III IV 解题报告

    原文地址:http://liangjiabin.com/blog/2015/04/leetcode-best-time-to-buy-and-sell-stock.html Best Time to ...

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

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

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

最新文章

  1. C++11中default的使用
  2. Canny-VO: 基于几何3D-2D边缘对准的RGB-D视觉里程计
  3. 独家 | 为什么要尝试A/B测试的贝叶斯方法(附链接)
  4. Javascript 面向对象编程定义接口的一种方法
  5. keras从入门到放弃(十四)模型的保存
  6. 女人 这20种男人你永远不必等
  7. 在winform上内嵌入其它的程序
  8. 【Elasticsearch】es Timelion是Kibana中时间序列的可视化工具
  9. 代码下移快捷键_收藏细看!最全面的通达信快捷键一览
  10. php复合索引,多列复合索引的使用绕过微软sqlserver的一个缺陷
  11. linux 字符串模糊匹配,awk模糊匹配
  12. 大数据:酒店业的慧眼
  13. 第八章 云计算原理与技术
  14. 幼儿园计算机信息技术培训总结,幼儿园教师信息技术培训总结
  15. TI 毫米波雷达学习网站链接整理
  16. R语言和医学统计学(10):正态性和方差齐性检验
  17. SQL查询语句教程(浅显易懂)
  18. Ubuntu系统管理 ——使用dpkg安装、查看、卸载软件包
  19. matlab保存数据用什么指令_MATLAB文件操作及保存文件save load fopen | 学步园
  20. china变为glmre

热门文章

  1. 面试官: Redis 与 MySQL 双写一致性如何保证?
  2. 超美观的 Vue+Element 开源后台管理 UI
  3. 分布式定时任务xxl-job的常用姿势都集齐了,So Easy!
  4. 今天我们来聊一聊 Spring 中的线程安全性
  5. 人工智能论坛、AI x Science 论坛和首席智行官大会
  6. 2021 OceanBase 数据库大赛来了!
  7. 吴忠强:刷LeetCode的正确姿势!
  8. 可解释机器学习发展和常见方法!
  9. 让机器“看见”:图像数据的特征提取方法
  10. 发际线有救了!这款app可一键AI生发,拯救你的自拍焦虑