[关闭]
@snuffles 2015-03-09T15:34:57.000000Z 字数 537 阅读 854

Best Time to Buy and Sell Stock II

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排序来选题太鸡贼了


  1. int maxProfit(int prices[], int n){
  2. int profit = 0;
  3. for (int i = 1; i < n; ++i) {
  4. if (prices[i] > prices[i-1]) {
  5. profit += prices[i]-prices[i-1];
  6. }
  7. }
  8. return profit;
  9. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注