LeetCode 152

Maximum Product Subarray

Find the contiguous subarray within an array (containing at least one number)
which has the largest product.

For example, given the array [2,3,-2,4],
the contiguous subarray [2,3] has the largest product = 6.

 /*************************************************************************
> File Name: LeetCode152.c
> Author: Juntaran
> Mail: Jacinthmail@gmail.com
> Created Time: Fri 29 Apr 2016 03:46:31 PM CST
************************************************************************/ /*************************************************************************
Maximum Product Subarray Find the contiguous subarray within an array (containing at least one number)
which has the largest product. For example, given the array [2,3,-2,4],
the contiguous subarray [2,3] has the largest product = 6.
************************************************************************/ #include<stdio.h> void maxAndmin( int a, int b, int c, int* max, int* min )
{
if( a > b )
{
if( a > c )
{
*max = a;
*min = b > c ? c : b;
}
else
{
*max = c;
*min = b;
}
}
else
{
if( a > c )
{
*max = b;
*min = c;
}
else
{
*max = b > c ? b : c;
*min = a;
}
}
} int maxProduct( int* nums, int numsSize )
{
int result = nums[];
int lastMax = nums[];
int lastMin = nums[];
int i;
for( i=; i<numsSize; i++ )
{
maxAndmin( nums[i], nums[i]*lastMax, nums[i]*lastMin, &lastMax, &lastMin );
result = lastMax > result ? lastMax : result;
}
printf("result is %d\n", result);
return result;
} int main()
{
int nums[] = {-, , -};
int numsSize = ;
maxProduct( nums, numsSize );
return ;
}
上一篇:Eclipse 快捷键操作和常用设置


下一篇:HTTP状态码:400\500 错误代码