Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 2ca5464

Browse files
Best time to buy and sell stock
Signed-off-by: Leo Ma <begeekmyfriend@gmail.com>
1 parent ba25cdc commit 2ca5464

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all:
2+
gcc -O2 -o test stock.c
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
5+
static inline int max(int a, int b)
6+
{
7+
return a > b ? a : b;
8+
}
9+
10+
static int maxProfit(int k, int* prices, int pricesSize)
11+
{
12+
if (pricesSize == 0) {
13+
return 0;
14+
}
15+
16+
int i, j;
17+
if (k > pricesSize / 2) {
18+
/* We can make transactions as many as we can */
19+
int total = 0;
20+
for (i = 1; i < pricesSize; i++) {
21+
if (prices[i] > prices[i - 1]) {
22+
total += prices[i] - prices[i - 1];
23+
}
24+
}
25+
return total;
26+
} else {
27+
#if 1
28+
/* DP solution - O(kn) complexity
29+
* dp[i, j] = max (dp[i, j-1], // same times transactions, but one days before.
30+
* dp[i-1, t] + prices[j] - prices[t+1]) // for all of (0 <= t < j )
31+
* // this means find max profit from previous any of days
32+
*/
33+
int **dp = malloc((k + 1) * sizeof(int *));
34+
for (i = 0; i <= k; i++) {
35+
dp[i] = malloc((pricesSize + 1) * sizeof(int));
36+
memset(dp[i], 0, (pricesSize + 1) * sizeof(int));
37+
}
38+
39+
for (i = 1; i <= k; i++) {
40+
//printf("i:%d\n", i);
41+
//printf("\tprev_profit = %d - %d\n", dp[i - 1][0], prices[0]);
42+
int prev_profit = dp[i - 1][0] - prices[0];
43+
for (j = 1; j <= pricesSize; j++) {
44+
//printf("\tj:%d\n", j);
45+
dp[i][j] = max(dp[i][j - 1], prev_profit + prices[j - 1]); /* prices[j - 1] means sell on Day j for dp[i][j] */
46+
//printf("\tdp[%d][%d] = max(%d, %d + %d)\n", i, j, dp[i][j - 1], prev_profit, prices[j - 1]);
47+
if (j < pricesSize) {
48+
//printf("\tprev_profit = max(%d, %d - %d)\n", prev_profit, dp[i - 1][j], prices[j]);
49+
prev_profit = max(prev_profit, dp[i - 1][j] - prices[j]);
50+
}
51+
}
52+
}
53+
#if 0
54+
printf(" ");
55+
for (i = 0; i < pricesSize; i++) {
56+
printf("%d ", prices[i]);
57+
}
58+
printf("\n");
59+
for (i = 0; i <= k; i++) {
60+
for (j = 0; j <= pricesSize; j++) {
61+
printf("%d ", dp[i][j]);
62+
}
63+
printf("\n");
64+
}
65+
#endif
66+
67+
return dp[k][pricesSize];
68+
#else
69+
/* local[i][j] j transactions at most and the last max profix until Day i */
70+
int *local = malloc((k + 1) * sizeof(int));
71+
/* global[i][j] j transactions at most until Day i */
72+
int *global = malloc((k + 1) * sizeof(int));
73+
memset(local, 0, (k + 1) * sizeof(int));
74+
memset(global, 0, (k + 1) * sizeof(int));
75+
for (i = 0; i < pricesSize - 1; i++) {
76+
int diff = prices[i + 1] - prices[i];
77+
for (j = k; j >= 1; j--) {
78+
int tmp1 = global[j - 1] + (diff > 0 ? diff : 0);
79+
int tmp2 = local[j] + diff;
80+
local[j] = tmp1 > tmp2 ? tmp1 : tmp2;
81+
global[j] = global[j] > local[j] ? global[j] : local[j];
82+
}
83+
}
84+
return global[k];
85+
#endif
86+
}
87+
}
88+
89+
int main(int argc, char **argv)
90+
{
91+
if (argc < 2) {
92+
fprintf(stderr, "Usage: ./test k n1 n2...\n");
93+
exit(-1);
94+
}
95+
96+
int i, count = argc - 2;
97+
int *nums = malloc(count * sizeof(int));
98+
for (i = 0; i < count; i++) {
99+
nums[i] = atoi(argv[i + 2]);
100+
}
101+
printf("%d\n", maxProfit(atoi(argv[1]), nums, count));
102+
return 0;
103+
}

0 commit comments

Comments
 (0)