99_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 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相关推荐
- LeetCode 309. Best Time to Buy and Sell Stock with Cooldown--Java解法-卖股票系列题目
此文首发于我的Jekyll博客:zhang0peter的个人博客 LeetCode题解文章分类:LeetCode题解文章集合 LeetCode 所有题目总结:LeetCode 所有题目总结 题目地址: ...
- LeetCode 123. Best Time to Buy and Sell Stock III--Python解法--动态规划--数学题
此文首发于我的个人博客:zhang0peter的个人博客 LeetCode题解文章分类:LeetCode题解文章集合 LeetCode 所有题目总结:LeetCode 所有题目总结 题目地址:Best ...
- 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 ...
- [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 ...
- 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 ...
- 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/ 题解 经典的 暴力递归 - ...
- 【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 ...
- [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 ...
- [leetcode]_Best Time to Buy and Sell Stock I II
一个系列三道题,我都不会做,google之答案.过了两道,第三道看不懂,放置,稍后继续. 一.Best Time to Buy and Sell Stock I 题目:一个数组表示一支股票的价格变换. ...
最新文章
- C++11中default的使用
- Canny-VO: 基于几何3D-2D边缘对准的RGB-D视觉里程计
- 独家 | 为什么要尝试A/B测试的贝叶斯方法(附链接)
- Javascript 面向对象编程定义接口的一种方法
- keras从入门到放弃(十四)模型的保存
- 女人 这20种男人你永远不必等
- 在winform上内嵌入其它的程序
- 【Elasticsearch】es Timelion是Kibana中时间序列的可视化工具
- 代码下移快捷键_收藏细看!最全面的通达信快捷键一览
- php复合索引,多列复合索引的使用绕过微软sqlserver的一个缺陷
- linux 字符串模糊匹配,awk模糊匹配
- 大数据:酒店业的慧眼
- 第八章 云计算原理与技术
- 幼儿园计算机信息技术培训总结,幼儿园教师信息技术培训总结
- TI 毫米波雷达学习网站链接整理
- R语言和医学统计学(10):正态性和方差齐性检验
- SQL查询语句教程(浅显易懂)
- Ubuntu系统管理 ——使用dpkg安装、查看、卸载软件包
- matlab保存数据用什么指令_MATLAB文件操作及保存文件save load fopen | 学步园
- china变为glmre