Question

152. Maximum Product Subarray

Solution

题目大意:求数列中连续子序列的最大连乘积

思路:动态规划实现,现在动态规划理解的还不透,照着公式往上套的,这个问题要注意正负,需要维护两个结果

Java实现:

  1. public int maxProduct(int[] nums) {
  2. if (nums.length == 1) return nums[0];
  3. // 定义问题:状态及对状态的定义
  4. // 设max[i]表示数列中第i项结尾的连续子序列的最大连乘积
  5. // 求max[0]...max[n]中的最大值
  6. // 状态转移方程
  7. // max[0] = nums[0]
  8. // max[i] = Max.max(max[i-1] * nums[i], nums[i])
  9. int[] max = new int[nums.length];
  10. int[] min = new int[nums.length];
  11. for (int i = 0; i < nums.length; i++) {
  12. max[i] = min[i] = nums[i];
  13. }
  14. int product = nums[0];
  15. for (int i = 1; i < nums.length; i++) {
  16. if (nums[i] < 0) {
  17. max[i] = Math.max(min[i - 1] * nums[i], max[i]);
  18. min[i] = Math.min(max[i - 1] * nums[i], min[i]);
  19. product = Math.max(max[i], product);
  20. } else {
  21. max[i] = Math.max(max[i - 1] * nums[i], max[i]);
  22. min[i] = Math.min(min[i - 1] * nums[i], min[i]);
  23. product = Math.max(max[i], product);
  24. }
  25. }
  26. return product;
  27. }

别人的实现

  1. int maxProduct(int A[], int n) {
  2. // store the result that is the max we have found so far
  3. int r = A[0];
  4. // imax/imin stores the max/min product of
  5. // subarray that ends with the current number A[i]
  6. for (int i = 1, imax = r, imin = r; i < n; i++) {
  7. // multiplied by a negative makes big number smaller, small number bigger
  8. // so we redefine the extremums by swapping them
  9. if (A[i] < 0)
  10. swap(imax, imin);
  11. // max/min product for the current number is either the current number itself
  12. // or the max/min by the previous number times the current one
  13. imax = max(A[i], imax * A[i]);
  14. imin = min(A[i], imin * A[i]);
  15. // the newly computed max value is a candidate for our global result
  16. r = max(r, imax);
  17. }
  18. return r;
  19. }

关于动态规划

Ref

什么是动态规划?动态规划的意义是什么 - 知乎提问

152. Maximum Product Subarray - LeetCode的更多相关文章

  1. leetcode 53. Maximum Subarray 、152. Maximum Product Subarray

    53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...

  2. 求连续最大子序列积 - leetcode. 152 Maximum Product Subarray

    题目链接:Maximum Product Subarray solutions同步在github 题目很简单,给一个数组,求一个连续的子数组,使得数组元素之积最大.这是求连续最大子序列和的加强版,我们 ...

  3. 【刷题-LeetCode】152 Maximum Product Subarray

    Maximum Product Subarray Given an integer array nums, find the contiguous subarray within an array ( ...

  4. [LeetCode] 152. Maximum Product Subarray 求最大子数组乘积

    Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...

  5. LeetCode 152. Maximum Product Subarray (最大乘积子数组)

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

  6. [LeetCode]152. Maximum Product Subarray

    This a task that asks u to compute the maximum product from a continue subarray. However, you need t ...

  7. Maximum Product Subarray——LeetCode

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

  8. 【LeetCode】152. Maximum Product Subarray 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双重循环 动态规划 参考资料 日期 题目地址:htt ...

  9. Java for LeetCode 152 Maximum Product Subarray

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

随机推荐

  1. 使用Ansible部署openstack平台

    使用Ansible部署openstack平台 本周没啥博客水了,就放个云计算的作业上来吧(偷个懒) 案例描述 1.了解高可用OpenStack平台架构 2.了解Ansible部署工具的使用 3.使用A ...

  2. 一步步搭建物联网系统——无处不在的CSS

    无处不在的CSS 或许你觉得CSS一点儿也不重要,而事实上,如果说HTML是建筑的框架,CSS就是房子的装修.那么Javascript呢,我听到的最有趣的说法是小三--还是先让我们回到代码上来吧. C ...

  3. idea 提示错误: 找不到或无法加载主类

    首先检查自己的jdk 配置是否正确,检查好遍发现没有问题,但是项目就是运行不起来...... 重启idea,问题解决.

  4. SourceMonitor的安装

    SourceMonitor 本词条缺少名片图,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧! 中文名 SourceMonitor 软件大小 1743KB 软件语言 英文 软件类别  国外软件 ...

  5. node的两种随起随用静态服务器搭建

      一. anywhere Anywhere是一个随启随用的静态服务器,它可以随时随地将你的当前目录变成一个静态文件服务器的根目录. 1.确定电脑上安装了node.js 2.在当前所在项目文件夹下输入 ...

  6. Android实现秒开效果

    0x01 创建SplashActivity 新建一个Activity,取名为SplashActivity 0x02 新建资源 在res/drawable下新建一个splash.xml文件和名为ig_s ...

  7. 基于Nginx实现反向代理

    一.nginx的简介 Nginx 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务 其特点是占有内存少,并发能力强,事实上nginx的并发能力在同类型的网页服 ...

  8. Spring Boot-@Conditional注解以及衍生注解@ConditionalOnBean

    @Conditional:判断@Conditional指定的条件是否成立,如果成立才会给容器中添加组件,配置类里面的内容才会生效 我们发现有很多的自动配置类,但是这些自动配置类都有指定的条件,必须满足 ...

  9. 基于Vue开发的门户网站展示和后台数据管理系统

    基于Vue的前端框架有很多,这几年随着前端技术的官方应用,总有是学不完的前端知识在等着我们,一个人的精力也是有限,不可能一一掌握,不过我们学习很大程度都会靠兴趣驱动,或者目标导向,最终是可以以点破面, ...

  10. 帝国CMS只备份栏目和模板的方法

    方法一:不备份所有帝国cms数据内容表 我们知道帝国cms有8大模型,分别是 1.新闻系统数据表 ( phome_ecms_news )2.下载系统数据表 ( phome_ecms_download  ...