Vasily Guzov 1 year ago
parent a54b5b8996
commit 656b5c6eb5

@ -0,0 +1,3 @@
import { maxArea } from "./maxArea";
export default maxArea;

@ -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);
});
});

@ -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;
};

@ -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;
};
```
Loading…
Cancel
Save