关闭

算法-Maximum Subarray: a simpler solution

标签: leetcodearraymax-subarysimulate
1194人阅读 评论(0) 收藏 举报
分类:

原题

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.

代码实现

    public int MaxSubArray(int[] nums) 
    {
           int premax =nums[0];
           int curmax = nums[0];
           for(int i=1;i<nums.Length;i++)
           {
               curmax = Math.Max(nums[i], nums[i] + curmax);
               premax = Math.Max(curmax, premax);
           }
           return premax;
    }

模拟

这里写图片描述

leetcode测试结果

这里写图片描述

Array
http://download.csdn.net/detail/daigualu/9829402
DP
http://download.csdn.net/detail/daigualu/9829404
Tree
http://download.csdn.net/detail/daigualu/9829406
Stack
http://download.csdn.net/detail/daigualu/9829405
及其他

leetcode-solution库

leetcode算法题目解决方案每天更新在github库中,欢迎感兴趣的朋友加入进来,也欢迎star,或pull request。https://github.com/jackzhenguo/leetcode-csharp

2
0
查看评论

Lintcode 617:Maximum Average Subarray

一开始用了最简单的遍历循环法,时间复杂度为O(N^2),然后被提醒超时,上网查了之后做了一个小改动,用一个sum数组存储num[0]到num[i]的总和,这样算num[i]到num[j] 只要算sum[j]-sum[i]就好了。 这个想法的代码如下: class Solution { public:...
  • qq_34153219
  • qq_34153219
  • 2017-02-21 20:27
  • 2683

LeetCode(53)Maximum Subarray

题目如下: 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...
  • feliciafay
  • feliciafay
  • 2014-01-29 11:00
  • 5216

Maximum Subarray -- LeetCode

原题链接: http://oj.leetcode.com/problems/maximum-subarray/  这是一道非常经典的动态规划的题目,用到的思路我们在别的动态规划题目中也很常用,以后我们称为”局部最优和全局最优解法“。 基本思路是这样的,在每一步,我们维护两个变量,...
  • linhuanmars
  • linhuanmars
  • 2014-03-20 09:24
  • 22538

[C++]LeetCode: 58 Maximum Subarray

题目: Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [...
  • cinderella_niu
  • cinderella_niu
  • 2014-12-28 12:05
  • 1017

[leetcode]Maximum Subarray (C语言)

问题描述 Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [−2,1,−...
  • zdavb
  • zdavb
  • 2015-07-05 23:41
  • 520

53. Maximum Subarray-动态规划/分治

题: Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [-...
  • u011567017
  • u011567017
  • 2016-09-04 17:30
  • 267

LeetCode53. Maximum Subarray 动态规划和分治法

题意:在一个数组中找到一个和最大的子数组。 有两种方法: 动态规划DP 分治法
  • wmdshhz0404
  • wmdshhz0404
  • 2016-09-20 23:26
  • 390

【leetcode】【53】Maximum Subarray

一、问题描述 Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array&#...
  • xiaoliucool1314
  • xiaoliucool1314
  • 2016-03-01 11:21
  • 356

LeetCode题解系列--718. Maximum Length of Repeated Subarray

描述Given two integer arrays A and B, return the maximum length of an subarray that appears in both arrays.Example 1: Input: A: [1,2,3,2,1] B: [3,2,1...
  • bowen_wu_sysu
  • bowen_wu_sysu
  • 2017-11-01 21:29
  • 622

Leetcode 644. Maximum Average Subarray II 最大平均区间2 解题报告

这道题目呢,和之前的643类型差不多,但是更难。 首先这道题我看了Solution的解法,反正不是n2的复杂度,是 n*log(max-min) 的样子吧,一开始怎么做都不对,超时。。。直到直接跑了solution的代码(java),发现原因不是代码写错了,而是python特别慢!!!! 所以不是...
  • MebiuW
  • MebiuW
  • 2017-07-27 22:41
  • 2214
    算法channel

    交流思想,注重分析,实例阐述,通俗易懂,包含但不限于:经典算法,机器学习,深度学习,LeetCode 题解,Kaggle 实战。期待您的到来!

    算法与人工智能交流群:646901659

    个人资料
    • 访问:321985次
    • 积分:7614
    • 等级:
    • 排名:第3379名
    • 原创:350篇
    • 转载:1篇
    • 译文:0篇
    • 评论:58条
    博客专栏