Skip to content

Commit

Permalink
Create: 309-Best-Time-to-Buy-and-Sell-Stock-with-Cooldown.c
Browse files Browse the repository at this point in the history
  • Loading branch information
Ykhan799 authored Sep 14, 2022
1 parent fa06e20 commit 7f0cfd6
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions c/309-Best-Time-to-Buy-and-Sell-Stock-with-Cooldown.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
int maxProfit(int* prices, int pricesSize){
int sold = 0;
int hold = INT_MIN;
int rest = 0;

for (int i = 0; i < pricesSize; i++) {
int prevSold = sold;
sold = hold + prices[i];
hold = max(hold, rest - prices[i]);
rest = max(rest, prevSold);
}
return max(sold, rest);
}

// C doesn't have a built-in max function
int max(int a, int b) {
return (a > b) ? a : b;
}

0 comments on commit 7f0cfd6

Please sign in to comment.