LeetCode 152
2024-08-24 17:50:41
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 ;
}
最新文章
- IE兼容性问题汇总【持续更新中】
- Android实现透明式状态栏
- 【NOIP2010】引水入城
- [实变函数]3.2 可测集 (measurable set)
- ubuntu共享文件夹给virtualbox
- 李洪强漫谈iOS开发[C语言-027]-自增与自减运算符
- zip压缩解压缩 项目icsharpcode-SharpZipLib-e012155
- Baidu Map Web API 案例
- ubuntu 下 apache+tomcat整合_(mod-jk方法)[转]
- Java-将多线程停止的两种方法
- 2015年第六届蓝桥杯javaB组 试题 答案 解析
- #20175204 张湲祯 2018-2019-2《Java程序设计》第六周学习总结
- 微信小程序的桌面图标问题
- linux设置时间显示格式和系统版本
- Run-time code to create charts:
- shell expect的简单用法【转】
- [转载]CMMI之功能点估算法:EI、EQ和EO
- 在你的 Rails App 中开启 ETag 加速页面载入同时节省资源
- Merge into用法
- @JoinColumn 详解