Leetcode —— Best Time to Buy and Sell Stock
来源:互联网 发布:四川广电网络官网 编辑:程序博客网 时间:2024/05/22 17:09
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) { if (0 == prices.size()) { return 0; } int minPrice = prices[0]; int maxProfit = 0; for (vector<int>::const_iterator iter = prices.begin() + 1; iter != prices.end(); ++iter) { minPrice = min(minPrice, *(iter)); maxProfit = max(maxProfit, *(iter) - minPrice); } return maxProfit; }};
0 0
- Leetcode —— Best Time to Buy and Sell Stock
- LeetCode——Best Time to Buy and Sell Stock
- LeetCode——Best Time to Buy and Sell Stock
- leetcode——Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock——LeetCode
- leetcode——Best Time to Buy and Sell Stock
- LeetCode——Best Time to Buy and Sell Stock
- LeetCode—Best Time to Buy and Sell Stock
- 【LeetCode】Best Time to Buy and Sell Stock & Best Time to Buy and Sell Stock
- DP——Best Time to Buy and Sell Stock
- Algorithms—121.Best Time to Buy and Sell Stock
- LeetCode121—Best Time to Buy and Sell Stock
- LeetCode122—Best Time to Buy and Sell Stock II
- LeetCode123—Best Time to Buy and Sell Stock III
- LeetCode188—Best Time to Buy and Sell Stock IV
- Leetcde——121 Best Time to Buy and Sell Stock && 123 Best Time to Buy and Sell Stock
- [LeetCode] [LeetCode]Best Time to Buy and Sell Stock
- leetcode——309——Best Time to Buy and Sell Stock with Cooldown
- Oracle 11G在用EXP 导出时,空表不能导出解决
- jquery.validate----身份证号码验证
- Leetcode_powx-n(c++ and python version)
- maven(8)--创建项目工厂和设置权限
- Java学习系列(七)Java面向对象之集合框架详解(上)
- Leetcode —— Best Time to Buy and Sell Stock
- WCF的发布
- 关于ALSA的小结
- 使用Opengl绘制字体
- TI C66x DSP 系统events及其应用 - 3
- JOptionPane类提示框的一些常用的方法
- Auto-increment 会在新记录插入表中时生成一个唯一的数字。
- cocos2d-x安装和配置
- 苹果打包问题汇总