Leetcode:72 Best Time to Buy and Sell Stock
来源:互联网 发布:中易广告联盟v9源码 编辑:程序博客网 时间:2024/06/08 06:32
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 res = 0; if(prices.empty()) return res; int min = prices[0]; for(int i = 0; i < prices.size(); i++) { if(prices[i] < min) min = prices[i]; if(prices[i] - min > res) res = prices[i] - min; } return res; }};
0 0
- Leetcode:72 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 with Python】 Best Time to Buy and Sell Stock
- [LeetCode]Best Time to Buy and Sell Stock
- [LeetCode]Best Time to Buy and Sell Stock II
- LeetCode Best Time to Buy and Sell Stock
- LeetCode Best Time to Buy and Sell Stock II
- LeetCode Best Time to Buy and Sell Stock III
- Leetcode: Best time to buy and sell a stock I
- Leetcode: Best Time to Buy and Sell Stock II
- LeetCode Best Time to Buy and Sell Stock III
- [Leetcode] Best Time to Buy and Sell Stock
- [Leetcode] Best Time to Buy and Sell Stock II
- [Leetcode] Best Time to Buy and Sell Stock III
- LeetCode : Best Time to Buy and Sell Stock II
- [leetcode] Best Time to Buy and Sell Stock
- leetcode 16: Best Time to Buy and Sell Stock III
- 黑马程序员——IOS学习笔记(类和对象)
- JAVA面试题
- eclipse waring undefined attribute name 解决
- 结构型模式:代理模式
- C++ 读写MySQL经典 (转载)
- Leetcode:72 Best Time to Buy and Sell Stock
- 提交.a文件到svn
- 闪光点
- 使用morphia实现对mongodb的复杂group查询
- kmalloc使用相关
- nginx代理后java获取真实ip解决办法
- Android 蓝牙4.0 BLE 理解
- oracle dblink 创建
- PHP的单引号和双引号