> For the complete documentation index, see [llms.txt](https://ucan.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ucan.gitbook.io/notes/53.-maximum-subarray.md).

# 53. Maximum Subarray

```java
class Solution {
    public int maxSubArray(int[] nums) {
        // dp[i] represent the larget sum of subarray end at index i
        // dp[i] = Math.max(dp[i-1]+nums[i], nums[i])
        int res = nums[0];
        if (nums.length>1){
            for(int i=1; i<nums.length; i++){
                nums[i] = Math.max(nums[i], nums[i-1]+nums[i]);
                res = res<nums[i]? nums[i]: res;            
            }
        }
        return res;      
    }
}
```
