Two questions a day T28

Two questions a day T28

algorithm

LeetCode T11. The container with the most water[1]

description

Give you n non-negative integers a1, a2,..., an, each of which represents a point (i, ai) in the coordinates. Draw n vertical lines in the coordinates. The two end points of the vertical line i are (i, ai) and (i, 0). Find two of the lines so that the container that they form with the x-axis can hold the most water.

Explanation: You cannot tilt the container, and the value of n is at least 2.

The vertical line in the figure represents the input array [1,8,6,2,5,4,8,3,7]. In this case, the maximum value that the container can hold water (shown as the blue part) is 49.

Example:

Input: [1,8,6,2,5,4,8,3,7]
Output: 49

analysis

The solution to this problem is similar 盛雨水, using double pointers.

Code

/**
 * @param {number[]} height
 * @return {number}
 */
var maxArea = function (height) {
  let i = 0, j = height.length-1;
  let square, max = 0;
  while (j-i >= 1) {
    if (height[i]> height[j]) {
      square = height[j] * (j-i);
      j--;
    } else {
      square = height[i] * (j-i);
      i++;
    }
    max = Math.max(square, max);
  }
  return max;
};

front end

Talk about the Promises/A+ specification

Term introduction

promise: Have is a thenmethod of an object or function, its actions are consistent with this specification thenable: a definition of the thenobject or function methods. This is mainly used to be compatible with some old Promise implementations. As long as a Promise implementation is thenable, that is, it has thenmethods, it can be compatible with Promises/A+. value: Refers resloveout value can be any valid value JS (including undefined, thenable promise and the like) exception: abnormal, in which a Promise throwthrown value reason: rejection reason is rejectinside the transmission parameter indicating rejectthe reason of

status

pending: A promise is in this state before resolve or reject. fulfilled: A promise is in a fulfilledstate after being resolved , this state cannot be changed, and must have an immutable value ( value). rejected: A promise is in a rejectedstate after being rejected , and this state cannot be changed, and it must have an immutable rejection reason ( reason).

then method

promise.then(fulfilledFn, rejectedFn)

catch method

promise.catch(rejectedFn)

Of course, the new ES specification async, awaithandles asynchronous is Promise+ generatorimplemented, the specific implementation set forth in the specification content of tomorrow.

References

[1]11. The container with the most water: https://leetcode-cn.com/problems/container-with-most-water/

Reference: https://cloud.tencent.com/developer/article/1664383 Two questions a day T28-Cloud + Community-Tencent Cloud