Skip to content

Commit

Permalink
Best time to buy and sell stock
Browse files Browse the repository at this point in the history
Signed-off-by: begeekmyfriend <[email protected]>
  • Loading branch information
begeekmyfriend committed Sep 18, 2017
1 parent 3c1b10e commit 2d37950
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
2 changes: 2 additions & 0 deletions 121_best_time_to_buy_and_sell_stock/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
gcc -O2 -o test stock.c
24 changes: 24 additions & 0 deletions 121_best_time_to_buy_and_sell_stock/stock.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <stdio.h>
#include <stdlib.h>

static int maxProfit(int* prices, int pricesSize)
{
int i, j, diff = 0;
for (i = 0; i < pricesSize; i++) {
for (j = i + 1; j < pricesSize; j++) {
diff = prices[j] - prices[i] > diff ? prices[j] - prices[i] : diff;
}
}
return diff;
}

int main(int argc, char **argv)
{
int i, count = argc - 1;
int *nums = malloc(count * sizeof(int));
for (i = 0; i < count; i++) {
nums[i] = atoi(argv[i + 1]);
}
printf("%d\n", maxProfit(nums, count));
return 0;
}

0 comments on commit 2d37950

Please sign in to comment.