求股票的最大利润
2013-05-20 17:04
337 查看
给定表示股票具体价格的一序列数据(一个公司在某个市场的股票)。给定N天的数据序列,决定什么时候买入和卖掉能获得最大的利润。(假设只能买一只股票)。一只股票的价格在一天内不变,同一天不能同时买入和卖掉。
You have a sequence of data which tells about daily prices of a stock (of a company in some market). Given the sequence for N such days tell when should one buy and sell to maximize the profit. (for simplicity Assume you can buy only
1 stock). Prices of stock is same for a single day and you cannot buy and sell on the same day.
只能买卖一次。
Edit: You have to buy once only and sell once only. (I also misunderstood Q during interview that we have to tell sequence of buying and selling but it was not the question)
遍历价格序列,维护目前为止最低价格,使用a[i]-min更新最大利润,时间复杂度为O(n).
You have a sequence of data which tells about daily prices of a stock (of a company in some market). Given the sequence for N such days tell when should one buy and sell to maximize the profit. (for simplicity Assume you can buy only
1 stock). Prices of stock is same for a single day and you cannot buy and sell on the same day.
只能买卖一次。
Edit: You have to buy once only and sell once only. (I also misunderstood Q during interview that we have to tell sequence of buying and selling but it was not the question)
遍历价格序列,维护目前为止最低价格,使用a[i]-min更新最大利润,时间复杂度为O(n).
#include <vector> using namespace std; int maxProfit(vector<int>& a) { if (a.size() == 0) { return 0; } int min = a[0]; int max_Profit = 0; int tmp = 0; for (int i = 1;i<a.size();i++) { tmp = a[i] - min; max_Profit = max(max_Profit,tmp); if (a[i]<min) { min = a[i]; } } return max_Profit; }
相关文章推荐
- 买股票最大利润
- [LeetCode]121. Best Time to Buy and Sell Stock(求近期股票能获得的最大利润)
- 携程校招2017编程题之股票最大利润
- (LeetCode)Best Time to Buy and Sell Stock -- 求股票买卖的最大利润
- 剑指Offer-63:股票的最大利润
- leetcode_122. Best Time to Buy and Sell Stock II 多次买卖股票,求交易的最大利润
- 股票最大利润
- 假如一个数组存储了一个股票,在一天交易窗口内各时间点的股票价格(正整数),只允许一次买入和一次卖出,请提供一个算法,计算出通过买入和卖出可以得到的最大利润
- 股票买卖之多次交易最大利润
- 给你一串股票价格,找出买点和卖点,使得利润最大
- 说你有一个数组,其中第i个元素是第i天给定股票的价格。设计一个算法来找到最大的利润,最多可以完成两个交易。
- 假如一个数组存储了一个股票,在一天交易窗口内各时间点的股票价格(正整数),只允许一次买入和一次卖出,请提供一个算法,计算出通过买入和卖出可以得到的最大利润
- LeetCode-714:Best Time to Buy and Sell Stock with Transaction Fee (带有抛售费用的股票最大利润) -- medium
- 给你一串股票价格,找出买点和卖点,使得利润最大。
- 面试题:股票的最大利润
- [面试]给你一串股票价格,找出买点和卖点,使得利润最大。(Amazon 面试题)
- LeetCode - 股票交易的最大利润
- 最大股票收益问题(数组最大差问题)
- java 动态规划判断股票最大盈利问题
- 股票开盘的最大成交额-----一道不错的贪心算法题目