LeetCode 121. Best Time to Buy and Sell Stock
2016-04-09 09:10
253 查看
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.
// The maxProfit is the current price minus the min price.
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.
// The maxProfit is the current price minus the min price.
int maxProfit(vector<int>& prices) { if(prices.size() <= 1) return 0; int maxProfit = INT_MIN; int minPriceSoFar = prices[0]; for(int i = 1; i < prices.size(); ++i) { minPriceSoFar = min(prices[i], minPriceSoFar); maxProfit = max(maxProfit, prices[i] - minPriceSoFar); } return maxProfit; }
相关文章推荐
- iOS单例的作用和使用
- java之classpath到底是什么
- 模拟实现 printf 函数
- hdoj 2546 饭卡 【转化思维+0-1背包】
- 操作系统中常用的调度算法
- 初始化顺序、==、AB交换
- 07_计算三位数中的所有“水仙花数”
- 时间复杂度和空间复杂度[数据结构]
- 数据的序列化,持久化,归档
- Emacs org-mode 预设模板与使用
- javaWeb求职之路__进阶书籍
- CSP考试 2015年09月第3题 C语言实现
- Animals
- poj1753 flip game
- hdoj 1864 最大报销额【0-1背包变形】
- NYOJ 52-无聊的小明【模拟】
- 使用Apache ant 1.9.6编译打包Web项目
- USACO numtri 数塔问题
- html(第一天,div+css)
- 【English】Taoism,Follow AJ's System