LeetCode第[53]题(Java):Maximum Subarray
题目:和最大的子序列
难度:Medium
题目内容:
Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
翻译:
给定一个整数数组nums,找到相邻的子数组(至少包含一个数字),它的总和是最大的,并返回它的和。
Example:
Input: [-2,1,-3,4,-1,2,1,-5,4],
Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6.
Follow up:
If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
我的思路:呃,没啥好思路,只会硬刚两个for,遍历所有子序列。。
我的代码:
public int maxSubArray(int[] nums) {
int max = nums[0];
for (int i = 0; i < nums.length; i++) {
int sum = 0;
for (int j = i; j < nums.length; j++) {
sum += nums[j];
max = sum > max ? sum : max;
}
}
return max;
}
我的复杂度:O(n2)
编码过程中的问题:(简单地代码有时候问题也是挺多的,可见并不能偷懒,想到方法了即使很简单也要动手写才行)
1、一开始取max 的初值为 0,然后发现当只有一个负数的时候会返回0,所以遍历取最值的时候,max或者min的初值应该取数组内部的值;
2、一开始sum的初值取的是 nums[i], j 从 i + 1 开始,然后发现最后一个元素(也是一个子序列)不会进入判断,所以遍历所有子序列的时候 j 应该是从 i 开始,sum的初值取0;
答案代码:
public int maxSubArray(int[] nums) {
int n = nums.length;
int[] dp = new int[n];
dp[0] = nums[0];
int max = dp[0];
for(int i = 1; i < n; i++){
dp[i] = Math.max(dp[i-1] + nums[i], nums[i]);
max = Math.max(max, dp[i]);
}
return max;
}
答案复杂度:O(N)
答案思路:采用动态规划的思想,新建一个数组,用它来记录以 nums[i] 结尾的序列能达到的最大值。
取 nums[i] 与 nums[i] + dp[i-1]的最大值就行(dp[i-1]如果大于零,则直接加,否则取nums[i]即为最大)
【注意并不是说dp[i]就表示以nums[i]结尾的序列内子序列能达到的最大值】
所以 dp[] 里面最大的那一个值就是要求的最大的。
优化:
因为只要知道dp的最大值即可,所以不需要把dp新建出来,只要用一个变量mem记录当前的,然后看是否比max大即可:
public int maxSubArray(int[] nums) {
int max = nums[0];
int mem = max;
for (int i = 1; i < nums.length; i++) {
mem = Math.max(mem + nums[i], nums[i]);
max = Math.max(mem, max);
}
return max;
}
扩展:如果我还想知道那个最大子序列的终始位置呢?
public int[] maxSubArray(int[] nums) {
int n = nums.length;
int[][] dp = new int[n][2];
dp[0][0] = nums[0];
int[] max = {dp[0][0], 0, 0};
for(int i = 1; i < n; i++){
if (dp[i - 1][0] < 0) {
dp[i][0] = nums[i];
dp[i][1] = i;
} else {
dp[i][0] = nums[i] + dp[i-1][0];
dp[i][1] = dp[i-1][1];
}
if (max[0] < dp[i][0]) {
max[0] = dp[i][0];
max[1] = dp[i][1];
max[2] = i;
}
}
return max;
}
算法复杂度:O(N)
max[] 三个元素分别是 max值、起始位置、终止位置
dp的下标就是终止位置了,所以再给dp加一个维度记录此终止位置对应的起始位置 dp[][]
【注意dp[i][1]的值也要根据 dp[i - 1][0] < 0 的判断而变化】
LeetCode第[53]题(Java):Maximum Subarray的更多相关文章
- 【LeetCode每天一题】Maximum Subarray(最大子数组)
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- LeetCode第[18]题(Java):4Sum 标签:Array
题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...
- LeetCode第[1]题(Java):Two Sum 标签:Array
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2
题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...
- LeetCode第[1]题(Java):Two Sum (俩数和为目标数的下标)——EASY
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- 【LeetCode】最大子阵列 Maximum Subarray(贪婪&分治)
描述: Given an integer array nums, find the contiguous subarray (containing at least one number) which ...
- LeetCode第[4]题(Java):Median of Two Sorted Arrays 标签:Array
题目难度:hard There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median ...
- 【Leetcode】【Medium】Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- LeetCode第[29]题(Java):Divide Two Integers
题目:两整数相除 难度:Medium 题目内容: Given two integers dividend and divisor, divide two integers without using ...
随机推荐
- 巨蟒python全栈开发数据库攻略2:基础攻略2
1.存储引擎表类型 2.整数类型和sql_mode 3.浮点类&字符串类型&日期类型&集合类型&枚举类型 4.数值类型补充 5.完整性约束
- 利用epoll实现异步IO
之前异步IO一直没搞明白,大致的理解就是在一个大的循环中,有两部分:第一部分是监听事件:第二部分是处理事件(通过添加回调函数的方式).就拿网络通信来说,可以先通过调用 select 模块中的 sele ...
- 接口测试工具 — jmeter(关联)
1.正则表达式 1)添加正则表达式提取器 2)提取关联词 3)填写正则表达式 4)使用关联,其他请求使用${sign2}代替变量值 2. 1)添加提取器 2)填写变量值 3)使用关联,其他请求使用${ ...
- 解决chrome在ubuntu+root模式下打不开的问题
chrome在ubuntu root模式下打不开 双击图标,chrome打不开了: 解决办法: 查看一下打开chrome浏览器的命令是什么,右键properties 发现是chromium-brows ...
- Python开发【第六章】:面向对象
编程范式 编程是程序员用特定的语法+数据结构+算法组成的代码来告诉计算机如何执行任务的过程,一个程序是程序员为了得到一个任务结果而编写的一组指令的集合,正所谓条条大路通罗马,实现一个任务的方式有很多种 ...
- Spring的IoC模式
1.依赖 依赖就是有联系,有地方使用到它就是有依赖它,一个系统不可能完全避免依赖.如果你的一个类或者模块在项目中没有用到它,恭喜你,可以从项目中剔除它或者排除它了,因为没有一个地方会依赖它.下面看一个 ...
- oracle 禁用索引
同步数据的时候 有索引会比较慢 可以暂时禁用索引 --禁用索引 ALTER INDEX PK_T_AUTH_USERROLE_ID UNUSABLE; --恢复索引ALTER INDEX UK_T_A ...
- 007-Centos 7.x 安装 Mysql 5.7.13
1. 下载mysql的repo源 CentOS 7.2的yum源中默认没有mysql,要先下载mysql的repo源 wget http://repo.mysql.com/mysql57-commun ...
- ubuntu下安装qwt
转载自:http://blog.chinaunix.net/uid-20717410-id-256767.html 相对于官方说明,特别适应于使用qt IDE开发者. //以下为引用: 虽然官网上说只 ...
- 简明python教程
linux查询python版本:python -V linux进入python:python 退出python:CTRL+D 使用源文件:helloworld.py 运行这个程序:python hel ...