File tree Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments