[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: ...
随机推荐
- centos通过yum安装php
1.添加php的yum软件仓库 sudo rpm -Uvh https://mirror.webtatic.com/yum/el6/latest.rpm 2.安装php相关软件,执行过程中全部选择ye ...
- (转载)(DescriptionResource Path Location Type The superclass "javax.servlet.http.HttpServlet" was not foun
eclipse环境下如何配置tomcat 打开Eclipse,单击"Window"菜单,选择下方的"Preferences". 单击"Server&q ...
- Springboot整合Junit单元测试
1.在pom.xml中添加junit环境的依赖 <dependency> <groupId>org.springframework.boot</groupId> & ...
- Django框架(九):视图(二) HttpRequest对象、HttpResponse对象
1. HttpRequest对象 服务器接收到http协议的请求后,会根据报文创建HttpRequest对象,这个对象不需要我们创建,直接使用服务器构造好的对象就可以.视图的第一个参数必须是HttpR ...
- 一线大厂的分布式唯一ID生成方案是什么样的?
本人免费整理了Java高级资料,涵盖了Java.Redis.MongoDB.MySQL.Zookeeper.Spring Cloud.Dubbo高并发分布式等教程,一共30G,需要自己领取.传送门:h ...
- ASP.NET ZERO 学习 导航菜单
定义PageNames和PermissionName PageNames : Web/App_Start/Navigation/PageNames.cs public const string Das ...
- JQuery获取当前屏幕的高度宽度的实现代码
<script type="text/javascript"> $(document).ready(function() { alert($(window).heigh ...
- js中要声明变量吗?
你好,js语言是弱类型语言,无需申明即可直接使用,默认是作为全局变量使用的.建议:在function里时应使用var 申明变量,这样改变量仅仅只在function的生存周期内存在,不会污染到,全局控件 ...
- tensorflow函数解析:Session.run和Tensor.eval
原问题链接: http://stackoverflow.com/questions/33610685/in-tensorflow-what-is-the-difference-between-sess ...
- python编程:从入门到实践----第四章>操作列表
一.遍历整个列表 1-1.假设有一个魔术师名单,需要将其中每个魔术师的名字都打印出来. # 用for循环来打印魔术师名单中的名字 magicians=['alice','david','carolin ...