[leetcode] 57 – II. Sequence of continuous positive numbers with sum s

Time:2021-6-21
1. Input a positive integer target and output all the continuous positive integer sequences (at least two numbers) whose sum is target.

The numbers in the sequence are arranged from small to large, and different sequences are arranged from small to large according to the first number.

Example 1:
Input: target = 9
Output: [[2,3,4], [4,5]]
Example 2:
Input: target = 15
Output: [[1,2,3,4,5], [4,5,6], [7,8]]

Restrictions:

1 <= target <= 10^5

2

Using the sliding window:
Sliding windows can be seen asA framed part of an array. In some array topics, we can use the sliding window to observe the possible candidate results. When the sliding window slides from the left to the right of the array, we can find the best result from all the candidate results.

For this problem, an array is a sequence of positive integers 1,2,3,…, n [1,2,3,…, n]. Let the left boundary of the sliding window be III and the right boundary be JJJ, then the sliding window is framed with a left closed and right open interval [I, J] [I, J] [I, J]. Note that for the convenience of programming, the sliding window is generally represented as a left closed right open interval. At the beginning, I = 1, j = 1I = 1, j = 1I = 1, j = 1, the sliding window is located on the leftmost side of the sequence, and the window size is zero.

[leetcode] 57 - II. Sequence of continuous positive numbers with sum s

The important property of sliding windows is that the left and right boundaries of windows can only move to the right, but not to the left. This is to ensure that the time complexity of the sliding window is O (n) O (n) O (n). If the left and right boundaries move to the left, this is called “backtracking”, and the time complexity of the algorithm may be more than o (n) O (n) O (n).

In this problem, we focus on the sum of all numbers in the sliding window. When the right boundary of the sliding window moves to the right, that is, j = j + 1, the number J is added to the sum of the windows. When the left boundary of the sliding window moves to the right, that is, I = I + 1, a number I is missing in the window, and I will be subtracted from the sum of the windows. Sliding window has only two operations: right boundary moving to the right (enlarging window) and left boundary moving to the right (reducing window), so it is very simple in practice.
How to use sliding window to solve this problem

To solve this problem with a sliding window, we have to answer two questions

1. The first question is, when will the window expand and shrink?
2. Second question, can sliding window find all the solutions?

For the first question, the answer is very simple:

1. When the sum of the window is less than the target, the sum of the window needs to be increased, so to expand the window, the right boundary of the window moves to the right
2. When the sum of the window is larger than the target, the sum of the window needs to be reduced, so to narrow the window, move the left boundary of the window to the right

When the sum of windows is exactly equal to target, we need to record the result at this time. Let the window be [I, J] [I, J] [I, J], then we have found a sequence beginning with III, which is also the only sequence beginning with III. next, we need to find the sequence beginning with I + 1I + 1I + 1, so the left boundary of the window should move to the right

For the second question, we can prove it a little bit simply:

[leetcode] 57 - II. Sequence of continuous positive numbers with sum s

What we are looking for at the beginning is a sequence starting with 1. As long as the sum of windows is less than target, the right boundary of windows will always move to the right. Suppose that 1 + 2 +… + 81 + 2 + dots + 81 + 2 +… + 8 is less than target, and after adding a 9, we find that 1 + 2 +… + 8 + 91 + 2 + dots + 8 + 91 + 2 +… + 8 + 9 is greater than target again. This means that the sequence at the beginning of 1 cannot find a solution. The rightmost element of the sliding window is 9.

Next, we need to find the sequence starting with 2. We find that 2 +… + 8 < 1 + 2 +… + 8 < target2 + dots + 8 < 1 + 2 + dots + 8 < mathrm {target} 2 +… + 8 < 1 + 2 +… + 8 < target. This means that the sequence starting with 2 should be added to at least 9. Then, we just need to move the left boundary of the original 1 ~ 9 sliding window to the right and turn it into a 2 ~ 9 sliding window, and then continue to search. The right boundary does not need to move to the left at all.

By analogy, the left and right boundaries of the sliding window do not need to move to the left, so all the solutions of this problem can be obtained by using the sliding window. The time complexity is O (n) O (n) O (n).

Note: in this problem, we can use the summation formula of arithmetic sequence to calculate the sum of sliding window. However, I do not use the summation formula here to show a more general solution. In fact, the problem of positive integer sequence into any incremental integer sequence, this method can be solved.

Leetcode sliding window

Solution code:

/**
 * @param {number} target
 * @return {number[][]}
 */
var findContinuousSequence = function(target) {
    let i = 1; // Left border of sliding window
    let j = 1; // Right border of sliding window
    let sum = 0; // Sum of numbers in sliding window
    let res = []; // Storage results
    while (i <= target / 2) {
        if (sum < target) {
            //The right boundary moves to the right
            sum += j;
            j++;
        } else if (sum > target) {
            //The left border moves to the right
            sum -= i;
            i++;
        } else {
            let temp = []; // Storage results
            for (var k = i; k < j; k++) {
                temp.push(k);
            }
            res.push(temp);
            //The left border moves to the right
            sum -= i;
            i++;
        }
    }
    return res
};

Solution code:
[leetcode] 57 - II. Sequence of continuous positive numbers with sum s

Execution time:

[leetcode] 57 - II. Sequence of continuous positive numbers with sum s