From 656b5c6eb5e63010498b7720bd7924667d6c9ca1 Mon Sep 17 00:00:00 2001 From: Vasily Guzov Date: Sun, 8 Dec 2024 19:37:39 +0300 Subject: [PATCH] [HAI] week3 max area https://leetcode.com/problems/container-with-most-water/description/?source=submission-ac --- lib/maxArea/index.ts | 3 ++ lib/maxArea/maxArea.test.ts | 13 +++++ lib/maxArea/maxArea.ts | 15 ++++++ lib/maxArea/readme.md | 94 +++++++++++++++++++++++++++++++++++++ 4 files changed, 125 insertions(+) create mode 100644 lib/maxArea/index.ts create mode 100644 lib/maxArea/maxArea.test.ts create mode 100644 lib/maxArea/maxArea.ts create mode 100644 lib/maxArea/readme.md diff --git a/lib/maxArea/index.ts b/lib/maxArea/index.ts new file mode 100644 index 0000000..de5b24c --- /dev/null +++ b/lib/maxArea/index.ts @@ -0,0 +1,3 @@ +import { maxArea } from "./maxArea"; + +export default maxArea; diff --git a/lib/maxArea/maxArea.test.ts b/lib/maxArea/maxArea.test.ts new file mode 100644 index 0000000..5fa69b9 --- /dev/null +++ b/lib/maxArea/maxArea.test.ts @@ -0,0 +1,13 @@ +import { describe, it, expect } from "vitest"; +import { maxArea } from "./maxArea.ts"; + +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); + }); +}); diff --git a/lib/maxArea/maxArea.ts b/lib/maxArea/maxArea.ts new file mode 100644 index 0000000..c43b23a --- /dev/null +++ b/lib/maxArea/maxArea.ts @@ -0,0 +1,15 @@ + 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; +}; diff --git a/lib/maxArea/readme.md b/lib/maxArea/readme.md new file mode 100644 index 0000000..47f645c --- /dev/null +++ b/lib/maxArea/readme.md @@ -0,0 +1,94 @@ +# 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 + +## тест кейсы + +```ts +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 + +```ts +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; +}; +```