Skip to content

Commit beed143

Browse files
committed
buy and sell stock II
1 parent 0ba640f commit beed143

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
// Say you have an array for which the ith element is the price of a given stock on day i.
7+
8+
// Design an algorithm to find the maximum profit. You may complete as many transactions as you like
9+
// (ie, buy one and sell one share of the stock multiple times).
10+
// However, you may not engage in multiple transactions at the same time
11+
// (ie, you must sell the stock before you buy again).
12+
namespace Array.Lib
13+
{
14+
public class BestTime
15+
{
16+
public int MaxProfit(int[] prices) {
17+
int totalProfit = 0;
18+
for(int i=0;i<prices.Length-1;i++)
19+
{
20+
if(prices[i+1]>prices[i])
21+
totalProfit += prices[i+1] - prices[i];
22+
}
23+
return totalProfit;
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)