Best Time to Buy and Sell Stock
来源:互联网 发布:诺基亚e71软件 编辑:程序博客网 时间:2024/05/29 02:19
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.
class Solution {public: int maxProfit(vector<int> &prices) { int buy=0,sell=0; int max=0; int temp=0; //潜在购买点 for(int i=0;i<prices.size();i++) { if(prices[i]<=prices[temp]){ temp = i; } if(prices[i]-prices[buy]>=max){ sell=i; max = prices[i]-prices[buy]; } if(prices[i]-prices[temp]>=max){ buy = temp; max = prices[i]-prices[temp]; } } return max; }};
0 0
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time To Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- The twenty things most people die of regret
- 在word文档里修改文章时,前面的字会把后面的字覆盖掉的问题
- memmove函数如何考虑内存重叠的
- mysql数据库基本的curl语句
- OpenOCD的remote_bitbang驱动分析
- Best Time to Buy and Sell Stock
- 拨云见日,任重道远 ——第六届云计算大会感悟
- 2014年互联网IT待遇
- Smith-waterman算法 openmp+mpi实现
- 知识管理&笔记整理相关
- 我的IOCP学习笔记 - 相关类定义
- java并发编程(9)--java线程锁技术Lock&ReadWriteLock
- 《Head First 设计模式》学习笔记——适配器模式 + 外观模式
- 康永,给残酷社会的善意短信