lintcode :continuous subarray sum 连续子数组之和
题目
连续子数组求和
给定一个整数数组,请找出一个连续子数组,使得该子数组的和最大。输出答案时,请分别返回第一个数字和最后一个数字的值。(如果两个相同的答案,请返回其中任意一个)
给定 [-3, 1, 3, -3, 4]
, 返回[1,4]
.
解题
法一:直接暴力,时间复杂度O(N2),时间超时
public class Solution {
/**
* @param A an integer array
* @return A list of integers includes the index of the first number and the index of the last number
*/
public ArrayList<Integer> continuousSubarraySum(int[] A) {
// Write your code here
ArrayList<Integer> result = new ArrayList<Integer>();
int max = Integer.MIN_VALUE;
for(int i = 0;i<A.length;i++){
int sum = 0;
for(int j = i;j<A.length; j++){
sum += A[j];
if(sum>max){
max = sum;
result.clear();
result.add(i);
result.add(j);
}
}
}
return result;
}
}
Java Code
然后想到有过求最大子数组的和,只是返回最大子数组对于的和,本题是返回子数组的开始和结束的下标.
根据之前做题,突然想到,通过定义一个数组,来保存子数组的右边界是该元素时候的最大和,求出所有和的最大值就是最大子数组和的最大值.
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
int max = Integer.MIN_VALUE;
int d[] = new int[nums.length];// 以i结束的元素的最大子数组之和
d[0] = nums[0];
max = d[0];
for(int i=1 ;i<nums.length ; i++){
d[i] = Math.max(d[i-1]+nums[i],nums[i]);
max = Math.max(d[i],max);
// System.out.println(d[i]);
}
return max;
}
}
Java Code
可以看出,上面的数组其实可以换成两个变量的.之前看的网上的程序就是定义两个变量的.
本题是求的最大子数组的两个边界下标.
根据上面的思想,这个数组的下标是该子数组的右下标,而数组的值是子数组的左下标.。。。。然而发现实现不了。。。
网上就找到下面的程序,很好理解,但是自己写就会写乱。。。
public class Solution {
/**
* @param A an integer array
* @return A list of integers includes the index of the first number and the index of the last number
*/
public ArrayList<Integer> continuousSubarraySum(int[] A) {
// Write your code here
ArrayList<Integer> result = new ArrayList<Integer>();
int begin = 0;
int end = 0;
int sum = A[0];
int maxsum = A[0];
int maxbegin = 0;
int maxend = 0;
for(int i = 1;i<A.length;i++){
if(sum <= 0){
if(A[i] > sum){
begin = i;
end = i;
sum = A[i];
}
if(A[i] >= maxsum){
maxbegin = i;
maxend = i;
maxsum = A[i];
}
}else{
sum +=A[i];
if(sum> 0){
end = i;
}
if(sum > maxsum){
maxbegin = begin;
maxend = i;
maxsum = sum;
}
}
}
result.add(maxbegin);
result.add(maxend);
return result;
}
}
Java Code
总耗时: 11178 ms
定义两对开始和结束的最大路径,一个用来保存当前路径的最大值得开始结束位置,一个用来保存所有路径的中最大值得开始结束位置。
当sum<0的时候 并且A[i] > sum 更新begin End sum
当A[i] >=maxsum时候更新maxbegin maxend maxsum
当sum>0 sum+=A[i]
此时若sum > 0 end 更新为当前的 i
若 sum>maxsum 时候更新maxbegin maxend maxsum
最后结束的就是答案了
class Solution:
# @param {int[]} A an integer array
# @return {int[]} A list of integers includes the index of the
# first number and the index of the last number
def continuousSubarraySum(self, A):
# Write your code here begin,end,suma = 0,0,A[0]
maxbegin,maxend,maxsum = 0,0,A[0]
for i in range(1,len(A)):
if suma < 0:
if A[i] > suma :
begin = i
end = i;
suma = A[i]
if A[i]>= maxsum:
maxbegin = i
maxend = i;
maxsum = A[i]
else:
suma +=A[i]
if suma>0:
end = i
if suma > maxsum:
maxbegin = begin
maxend = i
maxsum = suma
return [maxbegin,maxend]
Python Code
总耗时: 673 ms
lintcode :continuous subarray sum 连续子数组之和的更多相关文章
- [LintCode] Continuous Subarray Sum 连续子数组之和
Given an integer array, find a continuous subarray where the sum of numbers is the biggest. Your cod ...
- [leetcode]523. Continuous Subarray Sum连续子数组和(为K的倍数)
Given a list of non-negative numbers and a target integer k, write a function to check if the array ...
- [LeetCode] Minimum Size Subarray Sum 最短子数组之和
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- Minimum Size Subarray Sum 最短子数组之和
题意 Given an array of n positive integers and a positive integer s, find the minimal length of a suba ...
- [LeetCode] 209. Minimum Size Subarray Sum 最短子数组之和
Given an array of n positive integers and a positive integer s, find the minimal length of a contigu ...
- [LeetCode] 930. Binary Subarrays With Sum 二元子数组之和
In an array A of 0s and 1s, how many non-empty subarrays have sum S? Example 1: Input: A = [1,0,1,0, ...
- lintcode循环数组之连续子数组求和
v 题目:连续子数组求和 II 给定一个整数循环数组(头尾相接),请找出一个连续的子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的值.如果多个答案,请返回其中任意一个. ...
- [LintCode] Continuous Subarray Sum II
Given an integer array, find a continuous rotate subarray where the sum of numbers is the biggest. Y ...
- 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大
Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...
随机推荐
- 转: js操作cookie
cookie的几个概念 http://dearhappyfish.blog.163.com/blog/static/1901094152012422114753777/ js操作cookie 转:ht ...
- 微信小程序视频地址
微信小程序视频系列教程完整版,课程中用到的源码附在帖子最后. [url=http://bbs.larkapp.com/forum.php?mod=viewthread&tid=5673][b] ...
- 【面试虐菜】—— JAVA面试题(3)
1 throws与throw的区别 解析:throws和throw是异常处理时两个常见的关键字,初级程序员常常容易正确理解throw和throws的作用和区别,说明已经能比较深入理解异常处理.Thro ...
- [转] 浅谈Microsoft MVP
微软MVP,这个自1993 年开始在社群上出现的计划(MVP Award Program),目前在全球已经累积超过5,000 人,其中在台湾已经有一百多人了,包括我在内,这个计画现在已经成为以微软技术 ...
- PAT 字符串-02 删除字符串中的子串
/* 2 *PAT 字符串-02 删除字符串中的子串 3 *2015-08-09 4 作者:flx413 5 */ #include<stdio.h> #include<string ...
- hdu 1222 狼和兔子
Description There is a hill with n holes around. The holes are signed from 0 to n-1. A rabbit must h ...
- MySQL在ROW模式下通过binlog提取SQL语句
Linux基于row模式的binlog,生成DML(insert/update/delete)的rollback语句通过mysqlbinlog -v 解析binlog生成可读的sql文件提取需要处理的 ...
- 【quartz】 理论知识
属性的介绍 1.调度器属性:分别设置调度器的实例名(instanceName) 和实例 ID (instanceId).属性 org.quartz.scheduler.instanceName 可以是 ...
- ffmpeg 编码
编码可以简单理解为将连续的图片帧转变成视频流的过程.以H264为例给出编码的代码: int InitEncoderCodec(int width, int height) { auto enc = a ...
- Codeforces Round #347 (Div. 2) B. Rebus
题目链接: http://codeforces.com/contest/664/problem/B 题意: 给你一个等式,把等式左边的问号用1到n(n为等式右边的数)的数填好,使得等式成立 题解: 贪 ...