LintCode: 最大子数组
2015-10-09 13:51
337 查看
容易 最大子数组 查看运行结果
37% 通过
给定一个整数数组,找到一个具有最大和的子数组,返回其最大和。
您在真实的面试中是否遇到过这个题? Yes
样例
给出数组[−2,2,−3,4,−1,2,1,−5,3],符合要求的子数组为[4,−1,2,1],其最大和为6
注意
子数组最少包含一个数
挑战
要求时间复杂度为O(n)
37% 通过
给定一个整数数组,找到一个具有最大和的子数组,返回其最大和。
您在真实的面试中是否遇到过这个题? Yes
样例
给出数组[−2,2,−3,4,−1,2,1,−5,3],符合要求的子数组为[4,−1,2,1],其最大和为6
注意
子数组最少包含一个数
挑战
要求时间复杂度为O(n)
/* 动态规划: 状态方程: ans[0] = nums[0] ans[i] = max{ans[i-1] + nums[i], nums[i]} (i>=1) */ 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 ans = [0 for i in range(len(nums))] ans[0] = nums[0] for i in range(1, len(nums)): ans[i] = max(ans[i-1] + nums[i], nums[i]) return max(ans)
相关文章推荐
- 小算法--最大子数组
- Subtree
- LintCode --number-of-airplanes-in-the-sky(数飞机)
- LintCode --invert-binary-tree(翻转二叉树)
- LintCode --find-the-missing-number(寻找缺失的数)
- LintCode--best-time-to-buy-and-sell-stock(买卖股票的最佳时机)
- LintCode--best-time-to-buy-and-sell-stock-ii(买卖股票的最佳时机 II)
- 算法导论4.1-3~4.1-5最大子数组暴力算法和递归算法的实现和线性时间算法实现
- 最大子数组 java语言
- 最大子数组问题分治法(递归)Java实现
- 算法之求最大子数组
- LintCode 翻转二叉树
- LintCode 寻找缺失的数
- Lintcode 二叉树的后序遍历
- Lintcode 二叉树中序遍历
- Lintcode 二叉树前序遍历
- Lintcode 尾部零的个数
- Lintcode 比较字符串
- Lintcode 主元素
- Lintcode解题笔记 - 岛屿的个数