Dynamic Programming CurSum rightmost position of the maximum recording sequence, res record the actual maximum.
class Solution {
public int maxSubArray(int[] nums) {
int res = Integer.MIN_VALUE, curSum = 0;
for (int num : nums) {
curSum = Math.max(curSum + num, num);
res = Math.max(res, curSum);
}
return res;
}
}
After a partition to do, first of all about the array is divided into three parts, to find the largest sub-arrays of left and right and then have to start scanning from the middle to the left and right, respectively, to obtain the maximum value, respectively, and the left and right sides of the maximum value of the phase comparison results of taking the maximum of one. (This method is less sophisticated in sequence only once when the two elements when, Lmax and mmax equal, the first for loop will not run, give it exactly the same parts.
public class Solution {
public int maxSubArray(int[] nums) {
if (nums.length == 0) return 0;
return helper(nums, 0, nums.length - 1);
}
public int helper(int[] nums, int left, int right) {
if (left >= right) return nums[left];
int mid = left + (right - left) / 2;
int lmax = helper(nums, left, mid - 1);
int rmax = helper(nums, mid + 1, right);
int mmax = nums[mid], t = mmax;
for (int i = mid - 1; i >= left; --i) {
t += nums[i];
mmax = Math.max(mmax, t);
}
t = mmax;
for (int i = mid + 1; i <= right; ++i) {
t += nums[i];
mmax = Math.max(mmax, t);
}
return Math.max(mmax, Math.max(lmax, rmax));
}
}
Very classic greedy thought. Look at the code directly. Topic link:53. Maximum subsequence and...
Given an array of integers nums , find a contiguous subarray with the largest sum (the subarray contains at least one element) and return its maximum sum. example: Enter: [-2,1,-3,4,-1,2,1,-5,4], outp...
topic Algorithm thought: There are three solutions to this problem. (1) Traverse all consecutive subarrays and find the maximum value. This time complexity is too high. (2) Using the idea of dynamic...
Given an array of integersnumsFind a contiguous subarray with the largest sum (the subarray contains at least one element) and return its maximum sum. Example: Advanced: If you have achieved a complex...
A small program acquires the data on the previous page...
Dynamic Programming CurSum rightmost position of the maximum recording sequence, res record the actual maximum. After a partition to do, first of all about the array is divided into three parts, to fi...
1 problem A collection of a range, find the minimum number of intervals to remove, so that the remaining intervals do not overlap. note: It can be considered that the end point of the interval is alwa...
Create an Articleset class, open a commodity warehouse, used to store product elements Create an ArticleManage class, call the Article class and Articles, And achieve a rendezvous change...
1. Download the MAD package. libmad-0.15.1b Check all the information to compile OK on the PC. 2. Download MADLD-1.1P1. All information can be found on Github. MAD_STREAM_BUFFER combined data MAD_FRAM...
premise First of all, you have to have an ordinary SpringBoot capability, which is simplified on the original project. MyBatis-Plus Mybatis-Plus (MP) is a MyBatis's enhancement tool. Only in MyBatis, ...