@snuffles
2015-03-09T07:34:57.000000Z
字数 537
阅读 898
leetcode
Say you have an array for which the ith element(第i个元素) 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).
第一次编译没通过是多打了个括号...总是用IDE产生依赖了
第二次AC,不得不说,按照Accpetence排序来选题太鸡贼了
int maxProfit(int prices[], int n){
int profit = 0;
for (int i = 1; i < n; ++i) {
if (prices[i] > prices[i-1]) {
profit += prices[i]-prices[i-1];
}
}
return profit;
}