LeetCode 121 Best Time to Buy and Sell Stock
来源:互联网 发布:三国志9优化伴侣说明 编辑:程序博客网 时间:2024/05/22 10:44
Best Time to Buy and Sell Stock
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.
题意:在数组中找两个数使得差的绝对值最大,但是保证第二个数大于等于第一个数。 难度EASY。
class Solution {public: int maxProfit(vector<int>& prices) { int ans = 0; int minn; for(int i=0; i < prices.size(); i++) { int x = prices[i]; if(i==0) { minn = x; continue; } if( x > minn) ans = max(ans, x - minn); else minn = min(minn, x); } return ans; }};
0 0
- LeetCode 121: Best Time to Buy and Sell Stock
- LeetCode(121)Best Time to Buy and Sell Stock
- LeetCode 121 Best Time to Buy and Sell Stock
- LeetCode: Best Time to Buy and Sell Stock [121]
- LeetCode 121 Best Time to Buy and Sell Stock
- [leetcode 121] Best Time to Buy and Sell Stock
- Leetcode NO.121 Best Time to Buy and Sell Stock
- LeetCode 121 Best Time to Buy and Sell Stock
- LeetCode(121) Best Time to Buy and Sell Stock (Java)
- leetcode-121 Best Time to Buy and Sell Stock
- leetcode || 121、Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock - LeetCode 121
- leetcode#121 Best Time to Buy and Sell Stock
- Leetcode 121 Best Time to Buy and Sell Stock
- Leetcode #121 Best Time to Buy and Sell Stock
- leetcode 121: Best Time to Buy and Sell Stock
- leetcode: (121) Best Time to Buy and Sell Stock
- 121 Best Time to Buy and Sell Stock [Leetcode]
- 数组指针和指针数组的区别
- 各种变换滤波和噪声的类型和用途总结
- HttpServletRequest
- 【输入法相关论文】
- 归并排序
- LeetCode 121 Best Time to Buy and Sell Stock
- Python(13):格式化输出
- C++ string 类 部分成员函数实现(实现COW copy-on-write+实现智能指针(有极大bug版本))
- 深度学习之k-近邻算法识别手写图片
- compile amule
- Android数据安全之RSA加密(非对称加密)
- CentOS6.8升级Python2.6到Python2.7并安装pip
- shell脚本报错:-bash: xxx: /bin/sh^M: bad interpreter: No such file or directory
- Android中使用TextView实现微博@功能