Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
class Solution {
public:
int maxProfit(vector<int> &prices) {
// Note: The Solution object is instantiated only once and is reused by each test case.
int p = 0;
for(int i = 1; i < prices.size() ; ++i) {
int delta = prices[i] - prices[i-1];
if(delta > 0 ) {
p += delta;
}
}
return p;
}
};
股票交易策略:利用价格波动获取最大利润
本文介绍了一种股票交易策略,通过频繁买入卖出以获取最大利润,但必须确保每次交易前后只持有单一股票。算法实现了一个类Solution,用于计算在给定股票价格数组中可能获取的最大利润。
410

被折叠的 条评论
为什么被折叠?



