Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.

Example:

Input: [-2,1,-3,4,-1,2,1,-5,4],
Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6. Time:O(N)
Space:O(N) //prefix
public class Solution {
/**
* @param nums: A list of integers
* @return: A integer indicate the sum of max subarray
*/
public int maxSubArray(int[] nums) {
// write your code here
if (nums == null || nums.length == 0) {
return 0;
}
int max = Integer.MIN_VALUE, min = 0, sum = 0;
for (int i = 0; i < nums.length; i++) {
sum += nums[i];
max = Math.max(max, sum - min);
min = Math.min(min, sum);
}
return max;
}
}

//dp
 class Solution:
def maxSubArray(self, nums: List[int]) -> int:
if nums is None or len(nums) == 0:
return sum_arr = [0] * len(nums)
sum_arr[0] = nums[0]
max_res = nums[0]
for i in range(1, len(nums)):
if sum_arr[i - 1] > 0:
sum_arr[i] = nums[i] + sum_arr[i - 1]
else:
sum_arr[i] = nums[i]
max_res = max(max_res, sum_arr[i])
return max_res
class Solution {
public int maxSubArray(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int sum = nums[0], res = sum;
for (int i = 1; i < nums.length; i++) {
// add sum if sum > 0
sum = Math.max(nums[i], sum + nums[i]);
res = Math.max(res, sum);
}
return res;
}
}

[LC] 53. Maximum Subarray的更多相关文章

  1. LN : leetcode 53 Maximum Subarray

    lc 53 Maximum Subarray 53 Maximum Subarray Find the contiguous subarray within an array (containing ...

  2. [Leetcode][Python]53: Maximum Subarray

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 53: Maximum Subarrayhttps://leetcode.co ...

  3. 41. leetcode 53. Maximum Subarray

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

  4. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  5. [array] leetcode - 53. Maximum Subarray - Easy

    leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...

  6. Leetcode#53.Maximum Subarray(最大子序和)

    题目描述 给定一个序列(至少含有 1 个数),从该序列中寻找一个连续的子序列,使得子序列的和最大. 例如,给定序列 [-2,1,-3,4,-1,2,1,-5,4], 连续子序列 [4,-1,2,1] ...

  7. 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略

    原题 Given an integer array nums, find the contiguous subarray (containing at least one number) which ...

  8. Leetcode之53. Maximum Subarray Easy

    Leetcode 53 Maximum Subarray Easyhttps://leetcode.com/problems/maximum-subarray/Given an integer arr ...

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

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

随机推荐

  1. PAT Advanced 1053 Path of Equal Weight (30) [树的遍历]

    题目 Given a non-empty tree with root R, and with weight Wi assigned to each tree node Ti. The weight ...

  2. 2019中国大学生程序设计竞赛(CCPC) - 网络选拔赛 A题

    A - ^&^ Bit operation is a common computing method in computer science ,Now we have two positive ...

  3. squid完全攻略

    squid完全攻略 http://blog.sina.com.cn/s/blog_7572cf8e0100rl99.html squid,nginx,lighttpd反向代理的区别 [root@loc ...

  4. Linux 基本操作学习

    Linux 学习 虚拟机 (Virtual Machine) 指通过软件模拟的具有完整硬件系统功能的,运行再一个完全隔离环境中的完整计算机系统 常用 Linux 命令 命令 对应英文 作用 ls li ...

  5. SSh三大框架的作用

    一.详细分析spring+hibernate+struts作用? 1.struts是框架的表现层,Struts是对MVC构架的具体实现 Struts的MVC三层结构: (1)视图层:Struts采用J ...

  6. [原]调试实战——使用windbg调试DLL卸载时的死锁

    原调试debugwindbg死锁deadlock 前言 最近我们的程序在退出时会卡住,调查发现是在卸载dll时死锁了.大概流程是这样的:我们的dll在加载的时候会创建一个工作线程,在卸载的时候,会设置 ...

  7. python语法基础-并发编程-进程-进程池以及回调函数

    ###############   进程池    ############## """ 进程池的概念 为什么会有进程池? 1,因为每次开启一个进程,都需要创建一个内存空间 ...

  8. C语言实现整数转字符串

    #include <stdio.h> void intToString(int N,char arr[]){ //仅支持有符号4字节的int类型,范围-2147483648 - 21474 ...

  9. CodeForces 996B World Cup(思维)

    https://codeforces.com/problemset/problem/996/B 题意: 圆形球场有n个门,Allen想要进去看比赛.Allen采取以下方案进入球场:开始Allen站在第 ...

  10. 关于mysql数据库连接异常处理

    tomcat启动错误日志关键信息: 28-Aug-2019 14:22:55.014 SEVERE [localhost-startStop-1] org.apache.catalina.core.C ...