lintcode :最大子数组
题目:
给定一个整数数组,找到一个具有最大和的子数组,返回其最大和。
给出数组[−2,2,−3,4,−1,2,1,−5,3],符合要求的子数组为[4,−1,2,1],其最大和为6
子数组最少包含一个数
要求时间复杂度为O(n)
解题:
通过率37%,一定是暴力,时间复杂度O(N3)刷出来的成绩。。。
动态规划求解,维基百科,
下面程序半个暴力吧,时间复杂度O(n2)
Java程序:
public class Solution {
/**
* @param nums: A list of integers
* @return: A integer indicate the sum of max subarray
*/
public int maxSubArray(ArrayList<Integer> nums) {
// write your code
int maxsum = Integer.MIN_VALUE;
for(int i = 0;i<nums.size();i++){
int sum = 0;
for(int j=i;j<nums.size();j++){
sum+=nums.get(j);
maxsum = Math.max(sum,maxsum);
}
}
return maxsum;
}
}
总耗时: 3227 ms
下面看到一个可以时间复杂度是O(N),但是只能对最大子数组的和大于0的时候才可以,,,但是最大子数组的和是负的,最大的那个负数就是答案了。
Java程序:
public class Solution {
/**
* @param nums: A list of integers
* @return: A integer indicate the sum of max subarray
*/
public int maxSubArray(ArrayList<Integer> nums) {
// write your code
int maxsum = Integer.MIN_VALUE;
int sum = 0;
for(int i = 0;i<nums.size();i++){
if ( sum < 0 ){
sum = 0;
}
sum += nums.get(i);
maxsum = Math.max(maxsum, sum);
}
return maxsum;
}
}
总耗时: 1497 ms
Python程序:
class Solution:
"""
@param nums: A list of integers
@return: An integer denote the sum of maximum subarray
"""
def maxSubArray(self, nums):
# write your code here
if nums==None:
return 0
maxsum = -11111110
sum = 0
for i in range(len(nums)):
if sum<0:
sum=0
sum+=nums[i]
maxsum = max(sum,maxsum)
return maxsum
总耗时: 246 ms
动态规划求解:
Python程序:
class Solution:
"""
@param nums: A list of integers
@return: An integer denote the sum of maximum subarray
"""
def maxSubArray(self, nums):
# write your code here
max_ending_here = max_so_far = nums[0]
for x in nums[1:]:
max_ending_here = max(x, max_ending_here + x)
max_so_far = max(max_so_far , max_ending_here)
print x,max_ending_here,max_so_far
return max_so_far
上面的max_ending_here是包括当前位置时候的最大值,mas_so_far现阶段的最大值。这里理解的不是很透彻。。。
如:
| nums | -2 | 2 | -3 | 4 | -1 | 2 | 1 | -5 | 3 |
| max_ending_here | -2 | 2 | -1 | 4 | 3 | 5 | 6 | 1 | 4 |
| max_so_far | -2 | 2 | 2 | 4 | 4 | 5 | 6 | 6 | 6 |
Java程序:
public class Solution {
/**
* @param nums: A list of integers
* @return: A integer indicate the sum of max subarray
*/
public int maxSubArray(ArrayList<Integer> nums) {
// write your code
int max_ending_here = nums.get(0);
int max_so_far = nums.get(0);
for( int i =1 ;i<nums.size(); i++) {
max_ending_here = Math.max( nums.get(i) , nums.get(i) + max_ending_here );
max_so_far = Math.max( max_so_far, max_ending_here);
}
return max_so_far;
}
}
lintcode :最大子数组的更多相关文章
- lintcode 最大子数组III
题目描述 给定一个整数数组和一个整数 k,找出 k 个不重叠子数组使得它们的和最大.每个子数组的数字在数组中的位置应该是连续的. 返回最大的和. 注意事项 子数组最少包含一个数 样例 给出数组 [-1 ...
- lintcode:最大子数组差
题目 最大子数组差 给定一个整数数组,找出两个不重叠的子数组A和B,使两个子数组和的差的绝对值|SUM(A) - SUM(B)|最大. 返回这个最大的差值. 样例 给出数组[1, 2, -3, 1], ...
- lintcode:最大子数组II
题目 最大子数组 II 给定一个整数数组,找出两个不重叠子数组使得它们的和最大. 每个子数组的数字在数组中的位置应该是连续的. 返回最大的和. 样例 给出数组[1, 3, -1, 2, -1, 2], ...
- 最大子数组(LintCode)
最大子数组 给定一个整数数组,找到一个具有最大和的子数组,返回其最大和. 样例 给出数组[−2,2,−3,4,−1,2,1,−5,3],符合要求的子数组为[4,−1,2,1],其最大和为6 注意 子数 ...
- lincode.41 最大子数组
最大子数组 描述 笔记 数据 评测 给定一个整数数组,找到一个具有最大和的子数组,返回其最大和. 注意事项 子数组最少包含一个数 您在真实的面试中是否遇到过这个题? Yes 哪家公司问你的这个题? ...
- lintcode-42-最大子数组 II
42-最大子数组 II 给定一个整数数组,找出两个 不重叠 子数组使得它们的和最大. 每个子数组的数字在数组中的位置应该是连续的. 返回最大的和. 注意事项 子数组最少包含一个数 样例 给出数组 [1 ...
- LintCode-41.最大子数组
最大子数组 给定一个整数数组,找到一个具有最大和的子数组,返回其最大和. 注意事项 子数组最少包含一个数 样例 给出数组[−2,2,−3,4,−1,2,1,−5,3],符合要求的子数组为[4,−1,2 ...
- lintcode-45-最大子数组差
45-最大子数组差 给定一个整数数组,找出两个不重叠的子数组A和B,使两个子数组和的差的绝对值|SUM(A) - SUM(B)|最大. 返回这个最大的差值. 注意事项 子数组最少包含一个数 样例 给出 ...
- C++:最大子数组差
最大子数组差 内存限制:128 MiB 时间限制:1000 ms 题目描述: 给定一个整数数组,找出两个不重叠的子数组A和B,使两个子数组和的差的绝对值|SUM(A) - SUM(B) ...
随机推荐
- 使用ANT 生成Xfire 客户端端文件
这里需要用到的JAR包 : XmlSchema-1.1.jar activation-1.1.jar commons-codec-1.3.jar commons-httpclient-3.0.jar ...
- js设计模式(3)---桥接模式
0.前言 看设计模式比较痛苦,一则是自己经验尚浅,不能体会到使用这些设计模式的益处:二则是不能很好把握使用这些设计模式的时机.所以这一部分看得断断续续,拖拖拉拉,为了了却这快心病,决定最近一口气看完几 ...
- Unity3d 动态批处理的问题
这段时间做unity3d的优化,主要的入手是减少draw call. 1.代码上主要是把一些零碎的同材质的合并成一个大的mesh. 2.减少不必要的全屏后期处理.把摄像机的renderin ...
- PHP类的自动载入机制
php的自动加载: 在php5以前,我们要用某个类或类的方法,那必须include或者require,之后才能使用,每次用一个类,都需要写一条include,麻烦 php作者想简单点,最好能引用一个类 ...
- php解析url的三种方法举例
使用php解析url的三个示例. 方法一: $url="http://www.jbxue.com"; file_get_contents($url); 方法二: // CURL 方 ...
- WordPress 主题开发 - (三) 开发工具 待翻译
Before we get started building any WordPress Theme, we’re going to need to get our development tools ...
- php实现input输入框失去焦点自动保存输入框的数据
最近做一个输入框失去焦点时自动保存数据的功能,当然就是jQuery选择器选择input,blur时,ajax提交数据给php文件,php文件保存一下数据咯.主要是要注意一下中文的问题,所以中间需要转一 ...
- Python计算斗牛游戏的概率
Python计算斗牛游戏的概率 过年回家,都会约上亲朋好友聚聚会,会上经常会打麻将,斗地主,斗牛.在这些游戏中,斗牛是最受欢迎的,因为可以很多人一起玩,而且没有技术含量,都是看运气(专业术语是概率). ...
- 第一次比赛的 C题 (好后面才补的....) CodeForces 546B
Description Colonel has n badges. He wants to give one badge to every of his n soldiers. Each badge ...
- python命令行参数处理模块 optparse 使用参考
from optparse import OptionParser parser = OptionParser() parser.add_option( '-f', '--file', dest='f ...