Skip to content

Instantly share code, notes, and snippets.

@prmichaelsen
Last active April 21, 2016 02:14
Show Gist options
  • Save prmichaelsen/e65d4e76132873aa8481019bc4ffc1f6 to your computer and use it in GitHub Desktop.
Save prmichaelsen/e65d4e76132873aa8481019bc4ffc1f6 to your computer and use it in GitHub Desktop.
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