[LintCode] Maximum Subarray 最大子数组

Given an array of integers, find a contiguous subarray which has the largest sum.

Notice

The subarray should contain at least one number.

Have you met this question in a real interview?

Yes

Example

Given the array [−2,2,−3,4,−1,2,1,−5,3], the contiguous subarray [4,−1,2,1] has the largest sum = 6.

Challenge

Can you do it in time complexity O(n)?

s

时间: 2024-08-07 21:18:45

[LintCode] Maximum Subarray 最大子数组的相关文章

lintcode 中等题:maximum subarray最大子数组II

题目 最大子数组 II 给定一个整数数组,找出两个不重叠子数组使得它们的和最大. 每个子数组的数字在数组中的位置应该是连续的. 返回最大的和. 您在真实的面试中是否遇到过这个题? Yes 样例 给出数组[1, 3, -1, 2, -1, 2],这两个子数组分别为[1, 3]和[2, -1, 2]或者[1, 3, -1, 2]和[2],它们的最大和都是7 注意 子数组最少包含一个数 挑战 要求时间复杂度为O(n) 解题 最大子数组I 这个题目是求一个数组中一个最大子数组的和,而本题目是求数组中的前

[LeetCode] Maximum Subarray 最大子数组

Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [−2,1,−3,4,−1,2,1,−5,4],the contiguous subarray [4,−1,2,1] has the largest sum = 6. click to show more practice. Mor

【LeetCode每天一题】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

Maximum Subarray (最大子数组)

题目描述: Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [−2,1,−3,4,−1,2,1,−5,4], the contiguous subarray [4,−1,2,1] has the largest sum = 6. 这道题很经典,<算法导论>上有相关讨论,但是没有

53. Maximum Subarray最大子数组

[抄题]: Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [-2,1,-3,4,-1,2,1,-5,4],the contiguous subarray [4,-1,2,1] has the largest sum = 6. 方法一:贪心算法greedy [一句话思路]: 每次

LeetCode 70 _ Maximum Subarray 最大子数组 (Easy)

Description: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? Note: Given n will be a positive integer. Example 1: Input: 2 Output: 2

LintCode刷题---最大子数组

描述: 给定一个整数数组, 找到一个具有最大和的子数组, 返回其最大和. 样例: 输入: [-2, 2, -3, 4, -1, 2, 1, -5, 3] 输出:   6 解释:   符合要求得子数组为[4, -1, 2, 1], 其最大和为6 解题: 难点分析: 注意数组中有三种情况, 全为负数, 全为正数, 有正有负.   还需要注意得是, 这里得最大子数组是最大连续得子数组, 不是随意组合得最大子数组 两种方法解题: 1. 暴力解题: 两层循环,遍历所有直接找到最大的子数组. public

Lintcode: Maximum Subarray Difference

Given an array with integers. Find two non-overlapping subarrays A and B, which |SUM(A) - SUM(B)| is the largest. Return the largest difference. Note The subarray should contain at least one number Example For [1, 2, -3, 1], return 6 Challenge O(n) t

Lintcode - Maximum Subarray III

Given an array of integers and a number k, find k non-overlappingsubarrays which have the largest sum. The number in each subarray should be contiguous. Return the largest sum. http://www.lintcode.com/en/problem/maximum-subarray-iii/ Thought: 一开始以为这和