Maximum Subarray

一、题目描写叙述


就是求一个数组的最大子序列

二、思路及代码

首先我们想到暴力破解

public class Solution {
public int maxSubArray(int[] nums) {
int sum = Integer.MIN_VALUE;
for(int i=0; i<nums.length; i++)
for(int j=i+1; j<nums.length; j++)
sum = Math.min(nums[i]+nums[j], sum); return sum;
}
}

果然TLE了。于是要找到合适的时间复杂度。所以再寻找时间复杂度小于N平方的。

所以我们想到既然要求最大子序列,那么我们在扫面累加数组元素时,推断之前的元素是否小于0。假设小于0。说明不用累加了,由于会“拖累”后边求和的值。于是演变成了动态规划问题。递推公式就是maxSum=Math.max(maxSum,curSumi)。

当中 curSumi 就是到第 i 个数字时最大的和值。

public class Solution {
public int maxSubArray(int[] nums) {
int sum = nums[0], maxSum = nums[0];
for(int i=1; i<nums.length; i++) {
if(sum < 0) sum = 0; //推断之前的sum能否够利用
sum += nums[i];
maxSum = Math.max(sum, maxSum);
}
return maxSum;
}
}

Maximum Product Subarray

一、题目描写叙述

二、代码及思路

思路与上道题有所不同。这里要求是求乘积,那么乘积有个最简单的性质:负负得正;

那么假设套用上面那道题的思路,当前最小值假设是负值,假设下一个也是负值。就非常可能成为一个非常大的正值。

所以我们这里须要两个保存当前最小值和最大值的局部变量。

public class Solution {
public int maxProduct(int[] nums) {
int localMaxProduct = nums[0], localMinProduct = nums[0], maxProduct = nums[0]; for(int i=1; i<nums.length; i++) {
int copy_localMinProduct = localMinProduct;
localMinProduct = Math.min(Math.min(nums[i]*copy_localMinProduct, nums[i]*localMaxProduct), nums[i]);
localMaxProduct = Math.max(Math.max(nums[i]*copy_localMinProduct, nums[i]*localMaxProduct), nums[i]);
maxProduct = Math.max(localMaxProduct, maxProduct);
}
return maxProduct;
}
}

LeetCode_Maximum Subarray | Maximum Product Subarray的更多相关文章

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

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

  2. LeetCode: Maximum Product Subarray && Maximum Subarray &子序列相关

    Maximum Product Subarray Title: Find the contiguous subarray within an array (containing at least on ...

  3. LeetCode Maximum Product Subarray(枚举)

    LeetCode Maximum Product Subarray Description Given a sequence of integers S = {S1, S2, . . . , Sn}, ...

  4. 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大

    Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...

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

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

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

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

  7. 152. Maximum Product Subarray - LeetCode

    Question 152. Maximum Product Subarray Solution 题目大意:求数列中连续子序列的最大连乘积 思路:动态规划实现,现在动态规划理解的还不透,照着公式往上套的 ...

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

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

  9. [LeetCode]152. Maximum Product Subarray

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

随机推荐

  1. amcharts categoryAxis

    amcharts中给出的sample lineWithMultipleValueAxes,是这样的 我们对横坐标label做一些改变: var categoryAxis = chart.categor ...

  2. solr报错 ERROR SolrDispatchFilter null:ClientAbortException: java.net.SocketException: Broken pipe 原因是nginx截断了请求

    [root@localhost nginx]# lltotal 36drwx------. 2 www root 4096 Aug 13 13:25 client_body_tempdrwxr-xr- ...

  3. 2013Hulu校园招聘笔试题

    填空题:1.中序遍历二叉树,结果为ABCDEFGH,后序遍历结果为ABEDCHGF,先序遍历结果为?  FCBADEGH  如下图所示: 2.对字符串HELL0_HULU中的字符进行二进制编码,使得字 ...

  4. 微信小程序——自定义导航栏

    微信头部导航栏可能通过json配置: 但是有时候我们项目需求可能需要自定义头部导航栏,如下图所示: 现在具体说一下实现步骤及方法: 步骤: 1.在 app.json 里面把 "navigat ...

  5. 定制应用Repeater 、ListView的模版

    若干年前有个需求:客户可在管理后台给每个新闻内容栏目指定新闻的显示样式,有的可以显示新闻时间,有的则不需要.于是就有了动态模版的应用.记得当时是用 LoadControl 的方式然后 Controls ...

  6. (笔记)Linux线程编译undefined reference to 'pthread_create'

    在使用线程时,使用gcc或arm-linux-gcc编译时,会出现错误:undefined reference to 'pthread_create' 主要是以下两种原因: 1.#include &l ...

  7. 信噪比——信号加噪相关的知识

    信噪比:即Signal noise ratio , 即SNR: 它的单位为 dB, 公式为: SNR = 10lg(PS / PN), 其中 ps 表示信号的有效功率, pn 表示噪声的有效功率: 如 ...

  8. Java数组扩展

    Java中,数组初始化后如何扩展数组? 示例 以下示例显示如何在创建新并初始化数组后扩展数组. package com.yiibai; public class ExtendingArray { pu ...

  9. asp 写文件

    '写文件 Sub WriteToTextFile (FileUrl,byval Str,CharSet) set fso = Server.CreateObject("Scripting.F ...

  10. 【Hibernate步步为营】--最后的集合映射

    上篇文章具体讨论了组合对象映射的原理.它事实上指的是怎样将对象模型中的组合关系映射到关系模型中,它是通过使用Hibernate提供的<component>标签来实现的,并须要在该标签中加入 ...