Move Zeroes
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 transactionsas 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).这一个系列,这次是多次交易
思路
比一次交易的思路简单啊
就是两天相减,大于0就累加,小于0不管
代码
int maxProfit(int* prices, int pricesSize) {
if(pricesSize==0) return 0;
int i,j;
int profit = 0;
for(i=0;i<pricesSize-1;i++)
{
int t = prices[i+1]-prices[i];
if(t>0)
profit+=t;
}
return profit;
}
本文介绍了一种股票交易算法,该算法能在给定的价格序列中找到最大化的利润策略。通过简单的相邻两天价格比较,若后一天价格高于前一天,则将差额计入总利润,实现了多次买卖交易的最大收益计算。
498

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



