[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: ...
随机推荐
- python基础1--基本数据类型+流程控制
一.基本数据类型 1.整型 int 就是整数 2.浮点型 float 就是小数 3.字符串 3.1.加了单引号.双引号.多引号的字符就认为是字符串 单引号和双引号没有什么区别,多引号用 ...
- Centos7下yum安装软件报错解决办法
Traceback (most recent call last): File "/usr/bin/yum", line 29, in yummain.user_main(sys. ...
- 吴裕雄--天生自然TensorFlow2教程:数学运算
import tensorflow as tf b = tf.fill([2, 2], 2.) a = tf.ones([2, 2]) a+b a-b a*b a/b b // a b % a tf. ...
- Java多人聊天室第一版
package cn.zhang.chat; import java.io.BufferedReader; import java.io.PrintWriter; /** * 所有用户均有的信息,单独 ...
- 18个Java8日期处理的实践,太有用了
专注于Java领域优质技术,欢迎关注 作者:胖先森 Java 8 推出了全新的日期时间API,在教程中我们将通过一些简单的实例来学习如何使用新API. Java处理日期.日历和时间的方式一直为社区所诟 ...
- HDU-2544 最短路 Dijkstra模板题
题目链接:https://vjudge.net/problem/HDU-2544 题意: 题目要求找到节点1到节点n之间的一条最短路 分析: Dijkstra模板题 单源最短路径,可以用dijkstr ...
- 统计web 访问日志的请求数据
tomcat日志格式 在配置文件 server.xml 中,具体参照官方文档 https://tomcat.apache.org/tomcat-8.0-doc/config/valve.html#A ...
- Maven--部署构件至 Nexus
日常开发生成的快照版本构件可以直接部署到 Nexus 中策略为 Snapshot 的宿主仓库中,项目正式发布的构件则应该部署到 Nexus 中策略为 Release 的宿主仓库中. <proje ...
- tomcat配置配置文件和war包进行分离
应用部署 war包.配置文件分离 部署主机路径规划以及tomcat中间件改造 1.新建存放war包路径 /appsystems/apps 将war包放置其中 2.新建存放配置文件路径 /apps ...
- Python登录TP-Link路由器换ip脚本
有些时候我们需要更换IP(你懂得),网络下载的拨号软件大部分是需要电脑直接链接调制解调器(猫),对于局域网用户来说就比较麻烦了,下面我们用python来实现登录路由器自动切换ip的功能 # -*- c ...