【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.

思路:

很直接的最长上升子序列,线性 dp, dp 式子为:

dp[i] = dp[i-1] > 0?(A[i] + dp[i-1]):A[i]

然后迭代更新最大值就好。(会不会觉得 python 的实现写得很不一样?)

代码:

C++:

class Solution {
public:
    int maxSubArray(int A[], int n) {
        int ans = A[0];
        int *dp = new int[n];
        dp[0] = A[0];

        for(int i = 1;i < n;++i)
        {
            dp[i] = dp[i-1]>0?(A[i] + dp[i-1]):A[i];
            if(dp[i] > ans)
                ans = dp[i];
        }

        return ans;
    }
};

Python:

class Solution:
    # @param A, a list of integers
    # @return an integer
    def maxSubArray(self, A):
        ans = A[0]
        n = len(A)
        for i in range(1,n):
            if A[i-1] > 0:
                A[i] = A[i-1] + A[i]

            if A[i] > ans:
                ans = A[i]
        return ans
时间: 2024-08-05 07:09:15

【LeetCode】Maximum Subarray的相关文章

【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. More practice: If you have figu

【leetcode】Maximum Subarray (53)

1.   Maximum Subarray (#53) 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 s

【LeetCode】 Maximum Depth of Binary Tree

Maximum Depth of Binary Tree  Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 递归基础,不解释. class Solution { public: int getMax(TreeNode *root

【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大

Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4],the contiguous subarray [2,3] has the largest product = 

【Leetcode】Maximum Product Subarray

题目链接:https://leetcode.com/problems/maximum-product-subarray/ 题目: Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4], the contiguous subarray [2,3] has t

【数组】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.

【leetcode】Maximum Depth of Binary Tree

Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 利用递归. 也可以使用栈和队列,即使用DFS和BFS方法. C++: 1 /** 2 * Definition for binary tree 3 * struct TreeNod

【leetcode】Maximum Gap

Given an unsorted array, find the maximum difference between the successive elements in its sorted form. Try to solve it in linear time/space. Return 0 if the array contains less than 2 elements. You may assume all elements in the array are non-negat

【leetcode】Maximum Product of Word Lengths

Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. You may assume that each word will contain only lower case letters. If no such two words exist, return 0. Example