Last active
April 21, 2016 02:14
-
-
Save prmichaelsen/e65d4e76132873aa8481019bc4ffc1f6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int maxProfit(int* prices, int n) { | |
int previous_delta = 0; | |
int delta = 0; | |
int max_profit = 0; | |
for ( int i = 1 ; i < n ; i ++ ) | |
{ | |
previous_delta = delta; | |
delta = prices[ i ] - prices [ i - 1 ]; | |
if ( previous_delta > 0 ) | |
delta += previous_delta; | |
if ( max_profit < delta ) | |
max_profit = delta; | |
} | |
return max_profit; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment