leetcode121—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 (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.
Note that you cannot sell a stock before you buy one.
Example 1:
Input: [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. Not 7-1 = 6, as selling price needs to be larger than buying price.
Example 2:
Input: [7,6,4,3,1] Output: 0 Explanation: In this case, no transaction is done, i.e. max profit = 0.
想法:找到价格差最大即可
class Solution {
public:
int maxProfit(vector<int>& prices) {
;;
int minPrices = INT_MAX;
; i < prices.size() ; i++){
minPrices = min(minPrices,prices[i]);
maxPrices = max(maxPrices,prices[i]-minPrices);
}
return maxPrices;
}
};
leetcode121—Best Time to Buy and Sell Stock的更多相关文章
- Leetcode-121 Best Time to Buy and Sell Stock
#121 Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price ...
- LeetCode121: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 ...
- [LeetCode] Best Time to Buy and Sell Stock with Cooldown 买股票的最佳时间含冷冻期
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] Best Time to Buy and Sell Stock IV 买卖股票的最佳时间之四
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] 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 an al ...
- [LeetCode] Best Time to Buy and Sell Stock II 买股票的最佳时间之二
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [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 ...
- [LintCode] Best Time to Buy and Sell Stock II 买股票的最佳时间之二
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LintCode] 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 ...
随机推荐
- 四种简单的sql语句(增删改查语句)
四种简单的sql语句(增删改查语句) 一.插入语句 insert into [table] ([column],[column],[column]) values(?,?,?) 二.删除语句 dele ...
- springboot 初识
从实用主义来学习springboot的话,那我们期望的就是首先知道 1 他是个什么东西 2 我们为什么要用他,他能带来什么样的好处 3 如何快速上手 简单来讲,springboot你可以理解成spri ...
- 使用PHPExcel实现数据批量导入到数据库
此例子只使用execel2003的.xls文档,若使用的是其他版本,可以保存格式为“Execel 97-2003 工作簿(*.xls)”即.xls文件类型即可! 功能说明:只能上传Excel2003类 ...
- RaPC(rasterized polygon clipper): A discrete grid-based polygon clipping algorithm
RaPC(rasterized polygon clipper)-A discrete grid-based polygon clipping algorithm This algorithm is ...
- ISDBT中CC的处理疑问
一个针对日本的数字电视应用(ISDBT)里字幕处理有一些问题,规范文档庞大又复杂,读起来还觉得语焉不详.接手遗留项目尝试处理字幕显示的问题,边读spec边看代码,先猜测.试图理解既有逻辑,再分析问题产 ...
- Adobe Flash Builder 4.6 打开时提示Failed to create the Java Virtual Machine
最近使用actionscript来编程,用到Adobe Flash Builder 工具,之前一直用着都没事的,今天打开突然就报了这个错误,然后就打不开了 好了,不多说,直接来吧. 首先在你的Adob ...
- Ajax如何设置cookie
普通的Ajax请求很遗憾不能返回服务器端设置的cookie 如何实现不刷新页面返回服务器设置的Cookie呢? 可以使用<script>或者<image>的src属性发起一个请 ...
- 【LLVM笔记】0x00 初识LLVM 链接类型
模块结构 LLVM程序是由若干的模块(Module)组成,每个模块中包含有一些函数.全局变量和符号表. 这些模块可能由LLVM的连接器组合在一起,组合的过程将会整合这些函数和全局变量的定义,整合他们的 ...
- NodeJS自定义模块
//1.创建测试模块js文件(我这里命名为test.js) //2.添加测试方法 function test(){ console.log('Test Success!'); } //3.公开该方法到 ...
- winform中容器是如使用的
1.容器 (1)FlowLayouPanel 普通容器[内部流式布局] (2)Groupbox 带有标题的普通容器[内部普通布局,超出范围隐藏] (3)Panel 普通容器[内部普通布局,超出范围隐藏 ...