122. Best Time to Buy and Sell Stock II

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).

假设有一个数组,它的第i个元素是一个给定的股票在第i天的价格。设计一个算法来找到最大的利润。你可以完成尽可能多的交易(多次买卖股票)。然而,你不能同时参与多个交易(你必须在再次购买前出售股票)。

1     public int maxProfit(int[] prices) {
2     int total = 0;
3     for (int i=0; i< prices.length-1; i++) {
4         if (prices[i+1]>prices[i]) total += prices[i+1]-prices[i];
5     }
6     
7     return total;        
8     }

 

相关题目:53. Maximum Subarray

posted @ 2017-10-14 21:16  daniel456  阅读(128)  评论(0编辑  收藏  举报