作者:姑获_626 | 来源:互联网 | 2023-07-07 21:59
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).
Accept: 6ms
思路:
- 同 Stock I,先求diff,然后将 diff[i] > 0的全部加起来即可。
int diff[100000];
int maxProfit(int prices[], int n){
for (int i = 0; i 0) {
total += diff[i];
}
} // for
return total;
}