Best Time to Buy and Sell Stock
来源:互联网 发布:2017年公务员网络培训 编辑:程序博客网 时间:2024/06/06 01:10
Say you have an array for which the ith element is the price of a given stock on dayi.
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.
Solution:
class Solution {public: int maxProfit(vector<int>& prices) { int len = prices.size(); if(!len) return 0; int index = 0, maxprofit = 0; for(int i = 0; i < len; ++i) { if(prices[i] < prices[index]) index = i; if(prices[i] - prices[index] > maxprofit) { maxprofit = prices[i] - prices[index]; } } return maxprofit; }};
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
- 解压.tar.bz2文件
- 整数幂的求值
- 数据库连接池技术-DBCP连接池 配置
- strstr()函数工作原理与自写实现
- CImageList
- Best Time to Buy and Sell Stock
- 编程之美:第三章 结构之法 3.4从无头单链表中删除节点
- MATLAB 求图像的极大值极小值,平均值
- 【排序】希尔排序模板
- DesignPattern之Command
- 黑马程序员——多线程2
- hdu 2795 Billboard 线段树
- 编程之美: 第三章 结构之法 3.5最短摘要的生成
- 两种生成全排列序列的方法