You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Vasily Guzov 656b5c6eb5 [HAI] week3 max area
https://leetcode.com/problems/container-with-most-water/description/?source=submission-ac
1 year ago
..
index.ts [HAI] week3 max area 1 year ago
maxArea.test.ts [HAI] week3 max area 1 year ago
maxArea.ts [HAI] week3 max area 1 year ago
readme.md [HAI] week3 max area 1 year ago

readme.md

maxArea

описание задачи

You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]).

Find two lines that together with the x-axis form a container, such that the container contains the most water.

Return the maximum amount of water a container can store.

Notice that you may not slant the container.

Example 1:

Input: height = [1,8,6,2,5,4,8,3,7] Output: 49 Explanation: The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49. Example 2:

Input: height = [1,1] Output: 1

Constraints:

n == height.length 2 <= n <= 105 0 <= height[i] <= 104

тест кейсы

describe("maxArea", () => {

  it("[1,8,6,2,5,4,8,3,7] => 49", () => {
    expect(maxArea([1, 8, 6, 2, 5, 4, 8, 3, 7])).toBe(49);
  });

  it("[1,1] => 1", () => {
    expect(maxArea([1, 1])).toBe(1);
  });
});

текстовое описание решения

  • заводим два указателя в начале и в конце
  • в цикле я буду двигаться к центру и считать площадь воды которая будет состоять из высоты столбцов и расстояния между ними
  • сравнивая высоты крайних столбцов буду решать какой указатель двигать левый или правый
  • разность между левым и правим указателем ширина площади воды минимальная высота высота площади

ассимптотическая оценка

Description Estimation
time: O(n)
mem: O(1)

time

Description Time
анализ и сбор информации 05:00
обдумываение решения и формулировка решения 15:12
имплементация 05:34
исправление ошибок 00:00
полное время затраченое на решение 25:46

журнал ошибок

code

typescript

export function maxArea(height: number[]): number {
  let L = 0;
  let R = height.length - 1;
  let res = 0;

  while (L <= R) {
    let hL = height[L];
    let hR = height[R];
    let currArea = Math.min(hL, hR) * (R - L);
    if (currArea > res) res = currArea;
    hR > hL ? L++ : R--;
  }

  return res;
};