Maximum Sum 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.

代码:

 1 public class Solution {
 2     public int maxSubArray(int[] nums)
 3     {
 4         int len = nums.length;
 5         int SubSum = nums[0];
 6         int MaxSum = nums[0];
 7         for(int i=1;i<len;i++)
 8         {
 9             SubSum=Math.max(nums[i],SubSum+nums[i]);
10             MaxSum=Math.max(MaxSum,SubSum);
11
12         }
13
14         return MaxSum;
15
16     }
17 }
时间: 2024-12-18 02:33:35

Maximum Sum Subarray的相关文章

Maximum Product Subarray Leetcode

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 = 6. 这道题和Maximum Sum Subarray类似,也是考虑什么时候可以取得最大值

LeetCode – Refresh – Maximum Product Subarray

Similar to maximum sum subarray, but need a gmin to record the global minimum to handle negative number multiplication. 1 class Solution { 2 public: 3 int maxProduct(int A[], int n) { 4 int result = A[0], gMax = A[0], gMin = A[0]; 5 for (int i = 1; i

maximum sum of a subarray with at-least k elements.

// Returns maximum sum of a subarray with at-least // k elements. static int maxSumWithK(int a[], int n, int k) { // maxSum[i] is going to store maximum sum // till index i such that a[i] is part of the // sum. int maxSum[] = new int [n]; maxSum[0] =

Leetcode Week5 Maximum Sum Circular Subarray

Question Given a circular array C of integers represented by A, find the maximum possible sum of a non-empty subarray of C. Here, a circular array means the end of the array connects to the beginning of the array.  (Formally, C[i] = A[i] when 0 <= i

LC 918. Maximum Sum Circular Subarray

Given a circular array C of integers represented by A, find the maximum possible sum of a non-empty subarray of C. Here, a circular array means the end of the array connects to the beginning of the array.  (Formally, C[i] = A[i] when 0 <= i < A.leng

算法设计与分析[0009] Dynamic Programming(II)(Maximum Sum/Product Subarray)

原文引用https://www.dazhuanlan.com/2019/08/25/5d625b5c4d1ea/ 本文通过 53. Maximum Subarray & 152. Maximum Product Subarray 分析根据动态规划思路进行问题求解中的一个关键环节:子问题的拆分和求解. Problem Description 两道题解决的问题相似,都是求解给定序列中满足某种数学特征(和最大/乘积最大)的子序列,虽然不需要将该子序列输出. 留意的关键字眼是:containing at

UVa 108 - Maximum Sum(最大连续子序列)

题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=44  Maximum Sum  Background A problem that is simple to solve in one dimension is often much more difficult to solve in more th

URAL 1146. Maximum Sum(求最大子矩阵和)

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1146 1146. Maximum Sum Time limit: 0.5 second Memory limit: 64 MB Given a 2-dimensional array of positive and negative integers, find the sub-rectangle with the largest sum. The sum of a rectangle is

ural 1146. Maximum Sum

1146. Maximum Sum Time limit: 0.5 secondMemory limit: 64 MB Given a 2-dimensional array of positive and negative integers, find the sub-rectangle with the largest sum. The sum of a rectangle is the sum of all the elements in that rectangle. In this p