[LC] 53. Maximum Subarray
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的更多相关文章
- LN : leetcode 53 Maximum Subarray
lc 53 Maximum Subarray 53 Maximum Subarray Find the contiguous subarray within an array (containing ...
- [Leetcode][Python]53: Maximum Subarray
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 53: Maximum Subarrayhttps://leetcode.co ...
- 41. leetcode 53. Maximum Subarray
53. Maximum Subarray Find the contiguous subarray within an array (containing at least one number) w ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- [array] leetcode - 53. Maximum Subarray - Easy
leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...
- Leetcode#53.Maximum Subarray(最大子序和)
题目描述 给定一个序列(至少含有 1 个数),从该序列中寻找一个连续的子序列,使得子序列的和最大. 例如,给定序列 [-2,1,-3,4,-1,2,1,-5,4], 连续子序列 [4,-1,2,1] ...
- 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略
原题 Given an integer array nums, find the contiguous subarray (containing at least one number) which ...
- Leetcode之53. Maximum Subarray Easy
Leetcode 53 Maximum Subarray Easyhttps://leetcode.com/problems/maximum-subarray/Given an integer arr ...
- leetcode 53. Maximum Subarray 、152. Maximum Product Subarray
53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...
随机推荐
- winform集成cefSharp,与页面进行交互
/// <summary> /// 为了使网页能够与winForm交互 将 com 的可访问性设置为 true /// </summary> [System.Runtime.I ...
- 1-3.监督学习(supervised learning)
定义:监督学习指的就是我们给学习算法一个数据集,这个数据集由“正确答案”组成,然后运用学习算法,算出更多的正确答案.术语叫做回归问题 [监督学习可分为]:回归问题.分类问题.两种 例:一个学生从波特兰 ...
- JAVA课程设计——俄罗斯方块
0.负责模块为可视化界面,技术栈为 (1)异常处理 (2)多线程 (3)文件存储 (4)Java swing 1.登陆界面 我的代码 import java.awt.Color; import jav ...
- cmd定时自动弹窗命令
at 17:00 /e:m,t,w,th,f,s,su msg * 弹窗文字
- C语言-指针到底是什么?
1.指针到底是什么?(1).指针变量与普通变量的区别 指针的实质就是一个变量,他跟普通变量没有任何本质区别.指针完整的名字应该叫做指针变量,简称为指针.2.为什么需要指针?(1).指针的出现是为了实现 ...
- 20.docker 持久化存储与数据共享
1.image layer 和 container layer 的关系 image layer 是可读的 container layer 是在image layer 之上创建的 一个可读可写层 con ...
- python 运算符 取余 取商 in not in
#运算符sum = 9//2 #取商print(sum) sum = 9%2 #取余print(sum) #inname1 = '小林'name2 = '林倩'if '林' in name1: pri ...
- Android开发环境搭建以及模拟环境搭建
Android开发环境 现在主流的Android开发环境有: Eclipse + ADT + SDK Android Studio + SDK IntelliJ IDEA + SDK 现在国内大部分开 ...
- 第一行代码近期bug及解决
Android学习笔记(5)----启动 Theme.Dialog 主题的Activity时程序崩溃的解决办法https://www.cnblogs.com/dongling/p/6476308.ht ...
- mysql安装(centos7)
1.下载rpm wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm 2.安装rpm yum ...