Skip to content

Commit d5a9a9c

Browse files
Refine
Signed-off-by: begeekmyfriend <begeekmyfriend@gmail.com>
1 parent 1316268 commit d5a9a9c

File tree

2 files changed

+31
-29
lines changed

2 files changed

+31
-29
lines changed

053_maximum_subarray/max_subarray.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,16 @@ static int partition(int *nums, int lo, int hi)
3737
static int maxSubArray(int* nums, int numsSize)
3838
{
3939
#if 1
40-
int i, len = 0, max = INT_MIN;
40+
int i, sum = 0, max = INT_MIN;
4141
for (i = 0; i < numsSize; i++) {
42-
len += nums[i];
43-
/* Calculate maximum each time in loop */
44-
max = len > max ? len : max;
45-
if (len < 0) {
46-
len = 0;
42+
/* dp indicates the optimical result of nums[0...i]
43+
* dp[i] = max(dp[i-1], 0) + nums[i] */
44+
if (sum < 0) {
45+
sum = nums[i];
46+
} else {
47+
sum += nums[i];
4748
}
49+
max = sum > max ? sum : max;
4850
}
4951
return max;
5052
#else

152_maximum_product_subarray/subarray.c

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
1+
#include <limits.h>
12
#include <stdio.h>
23
#include <stdlib.h>
34

4-
static int maxProduct(int* nums, int numsSize)
5+
static inline int min(int a, int b)
56
{
6-
if (numsSize == 0) {
7-
return 0;
8-
}
7+
return a < b ? a : b;
8+
}
9+
10+
static inline int max(int a, int b)
11+
{
12+
return a > b ? a : b;
13+
}
914

10-
int i, global_max = nums[0], product;
11-
int local_min = 1, local_max = 1;
15+
static int maxProduct(int* nums, int numsSize)
16+
{
17+
int i, maximum = INT_MIN;
18+
int dp_min = 1;
19+
int dp_max = 1;
20+
/* dp records the min or max result of subarray nums[0...i] */
1221
for (i = 0; i < numsSize; i++) {
13-
if (nums[i] > 0) {
14-
product = local_max * nums[i];
15-
global_max = product > global_max ? product : global_max;
16-
local_max *= nums[i];
17-
local_min *= nums[i];
18-
} else if (nums[i] < 0) {
19-
product = local_min * nums[i];
20-
global_max = product > global_max ? product : global_max;
21-
int tmp = local_max;
22-
local_max = product > 1 ? product : 1;
23-
local_min = tmp * nums[i];
24-
} else {
25-
global_max = global_max > 0 ? global_max : 0;
26-
local_max = 1;
27-
local_min = 1;
28-
}
22+
int prev_min = dp_min;
23+
int prev_max = dp_max;
24+
dp_min = min(nums[i], min(prev_min * nums[i], prev_max * nums[i]));
25+
dp_max = max(nums[i], max(prev_min * nums[i], prev_max * nums[i]));
26+
maximum = max(dp_max, maximum);
2927
}
3028

31-
return global_max;
29+
return maximum;
3230
}
3331

3432
int main(int argc, char **argv)
3533
{
34+
35+
3636
int i, count = argc - 1;
3737
int *nums = malloc(count * sizeof(int));
3838
for (i = 0; i < count; i++) {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy