Jan 18 - Maximum Subarray; DAC; DP; Array;

Iteration:

public class Solution {
    public int maxSubArray(int[] nums) {
        int start = 0, end = nums.length-1;
        //return sumSubArray(nums, 0, Integer.MIN_VALUE, start, end);
        int max = Integer.MIN_VALUE;
        int sum = 0;
        while(start <= end){
            sum += nums[start];
            if(sum <= 0){
                max = sum > max? sum:max;
                start++;
                sum = 0;
            }
            else{
                max = sum > max? sum:max;
                start++;
            }
        }
        return max;
    }
}

  

时间: 2024-10-24 07:35:32

Jan 18 - Maximum Subarray; DAC; DP; Array;的相关文章

LEETCODE —— Maximum Subarray [一维DP]

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:

LeetCode Array Easy 53. Maximum Subarray 个人解法 和分治思想的学习

Description 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 the largest sum = 6. F

[LeetCode][JavaScript]Maximum Subarray

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. https://leetcod

LeetCode: Maximum Subarray 解题报告

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. SOLUTION 1: 采用滑

LeetCode: Maximum Subarray [052]

[题目] 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. [题意] 给定一个数组,找出和最大的子数组,返回这个

LeetCode 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. click to show more practice. Mor

Maximum Subarray II

Given an array of integers, find two non-overlapping subarrays which have the largest sum. The number in each subarray should be contiguous. Return the largest sum. Notice The subarray should contain at least one number For given [1, 3, -1, 2, -1, 2]

leetcode_53题——Maximum Subarray(动态规划)

Maximum Subarray Total Accepted: 62857 Total Submissions: 182415My Submissions Question Solution 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,−

[LeetCode] Maximum Subarray Sum

Dynamic Programming There is a nice introduction to the DP algorithm in this Wikipedia article. The idea is to maintain a running maximum smax and a current summation sum. When we visit each num in nums, addnum to sum, then update smax if necessary o