Question

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

For example, given the array [−2,1,−3,4,−1,2,1,−5,4],
the contiguous subarray [4,−1,2,1] has the largest sum = 6.

Solution 1 -- DP

New array dp[i] has the following meaning:

dp[i] is the largest sum of subsequence (including nums[i]).

  1. public class Solution {
  2. public int maxSubArray(int[] nums) {
  3. if (nums == null || nums.length < 1)
  4. return 0;
  5. int length = nums.length, result = nums[0];
  6. int[] dp = new int[length];
  7. dp[0] = nums[0];
  8. for (int i = 1; i < length; i++) {
  9. dp[i] = Math.max(nums[i], nums[i] + dp[i - 1]);
  10. result = Math.max(result, dp[i]);
  11. }
  12. return result;
  13. }
  14. }

Solution 2

We can only use one variable instead of array.

  1. public class Solution {
  2. public int maxSubArray(int[] nums) {
  3. if (nums == null || nums.length < 1)
  4. return 0;
  5. int length = nums.length, result = nums[0], tmp = nums[0];
  6. for (int i = 1; i < length; i++) {
  7. tmp = Math.max(nums[i], nums[i] + tmp);
  8. result = Math.max(result, tmp);
  9. }
  10. return result;
  11. }
  12. }

Maximum Subarray 解答的更多相关文章

  1. 刷题53. Maximum Subarray

    一.题目说明 题目是53. Maximum Subarray,求最长连续子序列最大和.难度是Easy! 二.我的解答 Easy的题目,居然没做出来. 后来看了用dp方法,其中dp[i]表示以第i个元素 ...

  2. [LintCode] Maximum Subarray 最大子数组

    Given an array of integers, find a contiguous subarray which has the largest sum. Notice The subarra ...

  3. 【leetcode】Maximum Subarray (53)

    1.   Maximum Subarray (#53) Find the contiguous subarray within an array (containing at least one nu ...

  4. 算法:寻找maximum subarray

    <算法导论>一书中演示分治算法的第二个例子,第一个例子是递归排序,较为简单.寻找maximum subarray稍微复杂点. 题目是这样的:给定序列x = [1, -4, 4, 4, 5, ...

  5. LEETCODE —— Maximum Subarray [一维DP]

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

  6. 【leetcode】Maximum Subarray

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

  7. maximum subarray problem

    In computer science, the maximum subarray problem is the task of finding the contiguous subarray wit ...

  8. (转)Maximum subarray problem--Kadane’s Algorithm

    转自:http://kartikkukreja.wordpress.com/2013/06/17/kadanes-algorithm/ 本来打算自己写的,后来看到上述链接的博客已经说得很清楚了,就不重 ...

  9. 3月7日 Maximum Subarray

    间隔2天,继续开始写LeetCodeOj. 原题: Maximum Subarray 其实这题很早就看了,也知道怎么做,在<编程珠玑>中有提到,求最大连续子序列,其实只需要O(n)的复杂度 ...

随机推荐

  1. deciaml(十进制浮点运算)

    # -*- coding: utf-8 -*- # ==================== #File: python #Author: python #Date: 2014 #========== ...

  2. Spring Tool Suit 在Eclipse上的安装

    登录http://spring.io/tools/sts/all 下载所需的Spring Tool Suit安装包 我用的是springsource-tool-suite-3.6.1.RELEASE- ...

  3. 《JavaScript 闯关记》之 BOM

    ECMAScript 是 JavaScript 的核心,但如果要在 Web 中使用 JavaScript,那么 BOM(浏览器对象模型)则无疑才是真正的核心.BOM 提供了很多对象,用于访问浏览器的功 ...

  4. PHP session 跨子域问题总结

    Session主要分两部分: 一个是Session数据,该数据默认情况下是存放在服务器的tmp文件下的,是以文件形式存在 另一个是标志着Session数据的Session Id,Session ID, ...

  5. SQL SERVER 清空日志

    DUMP TRANSACTION [TBNAME] WITH NO_LOGBACKUP LOG [TBNAME] WITH NO_LOGDBCC SHRINKDATABASE([TBNAME]) 1. ...

  6. Eclipse 整合cvs教程及遇到的问题

    今天看着视频教程学习cvs版本管理,现在很多企业开发或许都采用了版本管理器,因为这样可以更好的进行团退开发与代码管理,所以学习一种版本管理技术还是很重要的. 最原始的独立版本管理是由两部分组成的,一个 ...

  7. poj2243 bfs

    O - 上一个题的加强版 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     6 ...

  8. MFC串口通信

    1.串口的操作可以有两种操作方式:同步操作方式和重叠操作方式(又称为异步操作方式). 同步操作时,API函数会阻塞直到操作完成以后才能返回(在多线程方式中,虽然不会阻塞主线程,但是仍然会阻塞监听线程) ...

  9. yii2 去掉index.php的方法

    1.开启apache-rewrite 在Windows下,我们一般使用的是Administrator账号,所以启用这两项非常简单: 在[Apache安装目录]/conf/httpd.conf中找到 # ...

  10. 通过js引入当前所需要的js,css等

    在我们web前端页面中经常会用到许多外部的js,css文件,那么问题来了,怎样才能一次性引入? 通过js便可实现 走码: //创建一个init.js文件 var jsUrls = "js/j ...