From 2a444c1e209dc5dd48a01caca5b2a830b07d3aa3 Mon Sep 17 00:00:00 2001 From: shintaroyoshida20 Date: Mon, 9 Jun 2025 21:20:57 +0900 Subject: [PATCH 1/2] feat : #34 add the STEP1 --- .../array/container-with-most-water/answer.md | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 blind75/array/container-with-most-water/answer.md diff --git a/blind75/array/container-with-most-water/answer.md b/blind75/array/container-with-most-water/answer.md new file mode 100644 index 0000000..2c753c2 --- /dev/null +++ b/blind75/array/container-with-most-water/answer.md @@ -0,0 +1,78 @@ +# 11. Container With Most Water + +## STEP1 + +### 発想 + +### 想定されるユースケース + +### 何が分からなかったか? + +### 手作業でやってみる + +- 2ポインターを用意する。1つは、左端、もう1つは右端に置く。 +- ポインターが一致するまで以下の作業を繰り返す。 + - 1. 2つのポインターで小さい方を動かす。 + - 2. 小さ方のポインターの高さをマークしておく。 + - 3. その高さより大きい高さのポインターが見つかった際に1に戻る。 + +```javascript +const maxArea = function(height) { + let left = 0 + let right = height.length - 1 + let maxArea = Math.min(height[left], height[right]) * (right - left) + while (left < right) { + const leftHeight = height[left] + const rightHeight = height[right] + if (leftHeight <= rightHeight) { + while ( height[left] <= leftHeight && left < right) { + left++ + } + const currentArea = Math.min(height[left], height[right]) * (right - left) + maxArea = Math.max(maxArea, currentArea) + continue + } + while (height[right] <= rightHeight && left < right) { + right-- + } + const currentArea = Math.min(height[left], height[right]) * (right - left) + maxArea = Math.max(maxArea, currentArea) + } + return maxArea +}; +``` + +## STEP2 + +```javascript +``` + +## STEP3 + +```javascript +``` + +## 感想 + +### コメント集を読んで + +## 他の人のPRを読んで + +## その他の方法 + +### コードの良し悪し + +* `*0` + * 時間計算量: + * 空間計算量: + +* `*1` + * 時間計算量: + * 空間計算量: + +* `*2` + * 時間計算量: + * 空間計算量: + +## 調べたこと + From 5749004de4f2af343e268ba61d49501ad9bd509e Mon Sep 17 00:00:00 2001 From: shintaroyoshida20 Date: Tue, 10 Jun 2025 22:18:04 +0900 Subject: [PATCH 2/2] feat : #34 two more solution --- .../array/container-with-most-water/answer.md | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/blind75/array/container-with-most-water/answer.md b/blind75/array/container-with-most-water/answer.md index 2c753c2..3f76b3f 100644 --- a/blind75/array/container-with-most-water/answer.md +++ b/blind75/array/container-with-most-water/answer.md @@ -13,6 +13,8 @@ - 2ポインターを用意する。1つは、左端、もう1つは右端に置く。 - ポインターが一致するまで以下の作業を繰り返す。 - 1. 2つのポインターで小さい方を動かす。 + - 大きい方のポインターは、最大の面積となる時に使用する可能性があるため、 + 動かさない。 - 2. 小さ方のポインターの高さをマークしておく。 - 3. その高さより大きい高さのポインターが見つかった際に1に戻る。 @@ -45,6 +47,28 @@ const maxArea = function(height) { ## STEP2 ```javascript +const calculateArea = function(height, left, right) { + const width = right - left + const containerHeight = Math.min(height[left], height[right]) + return containerHeight * width +} +const maxArea = function(height) { + let left = 0 + let right = height.length - 1 + let maxArea = calculateArea(height, left, right) + + while (left < right) { + const area = calculateArea(height, left, right) + maxArea = Math.max(maxArea, area) + + if (height[left] < height[right]) { + left++ + continue + } + right-- + } + return maxArea +}; ``` ## STEP3 @@ -60,6 +84,77 @@ const maxArea = function(height) { ## その他の方法 +* `*1` インデックスを1つずつ移動させ、候補を絞っていく方法. + +```javascript +const calculateArea = function(height, left, right) { + const width = right - left + const containerHeight = Math.min(height[left], height[right]) + return containerHeight * width +} +var maxArea = function(height) { + let left = 0 + let right = height.length - 1 + let maxArea = calculateArea(height, left, right) + + while (left < right) { + const area = calculateArea(height, left, right) + maxArea = Math.max(maxArea, area) + + if (height[left] < height[right]) { + left++ + continue + } + right-- + } + return maxArea +}; +``` + +- `*2` 候補になり得ないインデックスはSkipして、2ポインターで候補を絞っていく方法 + +```javascript +const maxArea = function(height) { + let left = 0 + let right = height.length - 1 + let maxArea = Math.min(height[left], height[right]) * (right - left) + while (left < right) { + const leftHeight = height[left] + const rightHeight = height[right] + if (leftHeight <= rightHeight) { + while ( height[left] <= leftHeight && left < right) { + left++ + } + const currentArea = Math.min(height[left], height[right]) * (right - left) + maxArea = Math.max(maxArea, currentArea) + continue + } + while (height[right] <= rightHeight && left < right) { + right-- + } + const currentArea = Math.min(height[left], height[right]) * (right - left) + maxArea = Math.max(maxArea, currentArea) + } + return maxArea +}; +``` + +* `*3` 組み合わせを総当たりで試す方法 + +```javascript +const maxArea = function(height) { + let maxArea = -Infinity + for (let i = 0; i < height.length; i++) { + for (let j = i + 1; j < height.length; j++) { + const width = j - i + const containerHeight = Math.min(height[i], height[j]) + maxArea = Math.max(maxArea, containerHeight * width) + } + } + return maxArea +}; +``` + ### コードの良し悪し * `*0`