leetcode Best Time to Buy and Sell Stock
来源:互联网 发布:防骗大数据 编辑:程序博客网 时间:2024/05/22 13:08
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 (prices.empty()) return 0; int min = prices[0],profit = 0; int len = prices.size(); for (int i =1 ;i!= len;++i) { if (min > prices[i]) min = prices[i]; else profit = max(profit,prices[i] - min); } return profit; }};
0 0
- 【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
- LeetCode Best Time to Buy and Sell Stock
- java中的正则表达式
- Java实现快速排序
- CSDN学到的小技能
- 第五章
- 项目配置总结
- leetcode Best Time to Buy and Sell Stock
- 内联函数和宏定义的区别
- 【MFC】在MFC中PreTranslateMessage()的使用方法
- Mysql日期类型大小比较
- 二叉搜索树的后序遍历序列
- 多线程的代价
- 【我就看看不说话】真机调试 发布
- hdu1671Phone List
- 程序员面试失败的五大原因