LeetCode---Best Time to Buy and Sell Stock
来源:互联网 发布:虚拟主机销售php系统 编辑:程序博客网 时间:2024/05/12 12:52
题目大意:给出一个数组,数组中第i个元素表示第i天股票的价格,最多只允许交易一次求出最大利润。
算法思想:
找出数组中最低的股票价格作为买入价格和最高的股票价格最为抛售价格,保证买入价格的时间在抛售价格的时间之后。
代码如下:
class Solution {public: int maxProfit(vector<int>& prices) { if(prices.size()<2) return 0; int profit=0; int save=prices[0]; for(int i=1;i<prices.size();++i){ profit=max(profit,(prices[i]-save)); save=min(save,prices[i]); } 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
- python 的日志logging模块学习
- JAV面试知识点总结
- DateUtil中 format.setLenient(false);不接受13月份错误值
- 日志级别
- Object类中的方法介绍
- LeetCode---Best Time to Buy and Sell Stock
- HDU 1756 (计算几何 点在多边形内)
- 架构师之路
- C/C++ 中的0长数组(柔性数组)
- js中dom的document对象
- 基于Java+Selenium+Appium集成Web、安卓和IOS自动化测试框架的设计
- iOS 时时更新时间
- 第23讲 项目5:我的加班费
- tag、untag,pvid,trunk的关系