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 ;
}

最新文章

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

热门文章

  1. Hibernate3注解
  2. mmap和普通文件读写的区别和比较 &amp; mmap的注意点
  3. 线程暴长~Quartz中创建Redis频繁后导致线程暴长
  4. Maven最佳实践:管理依赖
  5. iOS开发-block使用与多线程
  6. Sonatype Nexus 搭建Maven 私服
  7. ADO.NET 快速入门(十二):从 SQL Server 生成 XML 数据
  8. 在Flash Builder或者Eclipse统计代码行数的方法
  9. C++ Caption
  10. 教你使用Android SDK布局优化工具layoutopt