[30 day leetcoding challenge] day 3

Time:2020-7-13

Hi, everyone. I’m a pig. Welcome to “baby can understand” series special – 30 day leetcoding challenge.

This is a small official event for leetcode. As you can see on the official website, from April 1, every day, the official will select a question and complete it within 24 hours to get a small reward. Although the reward doesn’t seem to work, as an official punch in activity, piggy still comes to punch the card, which is just for entertainment after work and home every day.

This is the question on April 3, and the 53rd question in the list of questions – “the largest sum of subsequences”

Title Description

Given an array of integersnums, find a contiguous subarray with the largest sum (the subarray contains at least one element) and returns its maximum sum.

Example 1:

Input: [- 2,1, - 3,4, - 1,2,1, - 5,4],
Output: 6
Explanation: the maximum sum of continuous subarray [4, - 1,2,1] is 6.

Advanced:

If you’ve implemented an O (n) complexity solution, try a more subtle divide and conquer approach.

Official difficulty

EASY

Solutions

The key point in the title is that the target array is a continuous subarray. This means that we can’t change the order or just pick some children from the middle of the interval. If we use an image of chestnuts, we can imagine that there is a window with variable length. Its left border can be moved left and right, and the right border can also be used. In this way, it selects a continuous subarray in the array.

Based on this chestnut, let’s see what happens when we traverse array items. First, for the right boundary, there may be two situations:

  • A positive number: if you add this positive number to the current window, it will only make the sum of the window larger, so in this case, we only need to move the right boundary to the right without brain.
  • A negative number: adding a negative number to the current window will make the sum of the window smaller, but we may encounter a larger positive number later, so it seems that we can’t determine which is the best.

Then we try to look at the above two situations from the perspective of the left boundary

  • A positive number: removing the positive number will make the window and smaller, but we may be able to remove a larger negative number.
  • A negative number: for the left boundary, if you can remove more negative numbers from the window by moving to the right, the sum in the window will naturally be larger.

Here we get a few traversal process of the situation, it seems that at present it is still relatively vague. But don’t worry, let’s continue to analyze.

Direct scheme

First of all, the most direct way is to try all possible windows, and then you can find the largest window situation. However, the time complexity of this method is too high, I believe that there will not be a small partner to write like this. This is just a basic implementation. The specific code is as follows:

const maxSubArray = nums => {
  let max = nums[0];
  for (let i = 0; i < nums.length; ++i) {
    let cur = 0;
    for (let j = i; i < nums.length; ++j) {
      cur += nums[j];
      cur > max && (max = cur);
    }
  }
  return max;
};

optimization

The above direct scheme makes no use of our purpose and previous analysis in the traversal process. If we add these, we can imagine that in the process of traversing the array, in fact, there are only two situations we are facing: continue to expand the current window or create a new window based on this position.

Why do you say that? Suppose that the sum of the current window is x, and we encounter a new value y, then from the perspective of the right boundary, because we do not know the future situation, we will continue to expand the current window no matter whether it is positive or negative. Unless at this point x + y < y, that is, from the perspective of the left boundary, you can get rid of a load by moving to the right.

That’s why in the beginning, we will analyze the situation of the left and right boundaries. Finally, in order to get the global maximum value, we will compare the local maximum value continuously in the process. The specific codes are as follows:

const maxSubArray = nums => {
  let max = cur = nums[0];
  for (let i = 1; i < nums.length; i++) {
    cur += nums[i];
    nums[i] > cur && (cur = nums[i]);
    cur > max && (max = cur);
  }
  return max;
};

Change your position

This is actually based on our initial analysis, but let’s change the posture to see. Assuming that the current position is always the right boundary of the window, whether the left boundary of the window needs to be moved to the left depends on the accumulated results on the left. And this cumulative result can be maintained through calculation. The specific codes are as follows:

const maxSubArray = nums => {
  let max = nums[0];
  for (let i = 1; i < nums.length; i++) {
    nums[i - 1] > 0 && (nums[i] += nums[i - 1]);
    nums[i] > max && (max = nums[i]);
  }
  return max;
};

summary

The third problem is how to simplify the problem of “challenge 30”. Hope to be able to help small partners in need.

If you think it’s good, remember “Sanlian.”. Piggy loves you~

Related links

  • Activity topic list
  • Repo for leetcode
  • My segment fault column
  • My Zhihu column

[30 day leetcoding challenge] day 3

Recommended Today

Cartoon, programmer and product manager

Internet crime series Internet crimes series – Preface On the first day of work, the front-end sued the county government and listed five crimes This cartoon isInternet crime series#2」 This is about the last time Wang Da Na sued the back end, the company’s product managers made various kinds of demons, abuse and exploitation, which […]