Skip to content

Commit 9b8729f

Browse files
authored
Merge pull request #4 from o-p/0011
* Add #11 Container With Most Water * Add basic approach of #11 * Add approach 2 * Add shorten approach JavaScript version
2 parents 8ff3d5b + 6d06de8 commit 9b8729f

File tree

6 files changed

+95
-0
lines changed

6 files changed

+95
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../problems/0011-container-with-most-water
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../problems/0011-container-with-most-water
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* 解題詳情在 ts,這邊是簡化後結果
3+
*
4+
* @param {number[]} height
5+
* @return {number}
6+
*/
7+
const maxArea = (
8+
height,
9+
max = 0,
10+
left = 0,
11+
right = height.length - 1,
12+
leftH = height[left],
13+
rightH = height[right],
14+
length = right - left,
15+
minH = Math.min(leftH, rightH)
16+
) => length > 0
17+
? maxArea(
18+
height,
19+
Math.max(max, minH * length),
20+
left + (leftH === minH),
21+
right - (rightH === minH)
22+
)
23+
: max
24+
25+
export default maxArea
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import maxArea from './container-with-most-water.short';
2+
3+
test.each`
4+
height | expected
5+
${[1,8,6,2,5,4,8,3,7]} | ${49} }
6+
${[1,1]} | ${1}
7+
${[4,3,2,1,4]} | ${16}
8+
${[1,2,1]} | ${2}
9+
`('Container With Most Water: ($height)', ({ height, expected }) =>
10+
expect(maxArea(height)).toBe(expected)
11+
);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import maxArea from './container-with-most-water';
2+
3+
test.each`
4+
height | expected
5+
${[1,8,6,2,5,4,8,3,7]} | ${49} }
6+
${[1,1]} | ${1}
7+
${[4,3,2,1,4]} | ${16}
8+
${[1,2,1]} | ${2}
9+
`('Container With Most Water: ($height)', ({ height, expected }) =>
10+
expect(maxArea(height)).toBe(expected)
11+
);
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const maxArea = 最基本作法
2+
3+
export default 從兩側向內找高點;
4+
5+
/**
6+
* 想不到有效加速法,先用最基本的做法
7+
*
8+
* Result:
9+
* Runtime: 7044 ms, faster than 5.00% of TypeScript online submissions
10+
* Memory Usage: 52.4 MB, less than 5.00% of TypeScript online submissions
11+
*/
12+
function 最基本作法(height: number[]): number {
13+
14+
const 算容積 = (左側高度: number, maps: number[]) => maps.map(
15+
(右側高度, distance) => Math.min(左側高度, 右側高度) * distance
16+
)
17+
18+
const 各座標作為左邊界的最大容積 = height.map(
19+
(height, index, arr) => Math.max(...算容積(height, arr.slice(index)))
20+
)
21+
22+
return Math.max(...各座標作為左邊界的最大容積)
23+
}
24+
25+
/**
26+
* 依照 Approach 2: Two Pointer Approach 實作
27+
*
28+
* Result:
29+
* Runtime: 80 ms, faster than 95.78% of TypeScript online submissions
30+
* Memory Usage: 43.9 MB, less than 5.42% of TypeScript online submissions
31+
*/
32+
function 從兩側向內找高點(height: number[]): number {
33+
const 尋找最大容積 = (左側座標: number, 右側座標: number, max: number = 0): number => {
34+
if (左側座標 === 右側座標) return max
35+
36+
const 左高 = height[左側座標]
37+
const 右高 = height[右側座標]
38+
const 容積 = Math.min(左高, 右高) * (右側座標 - 左側座標)
39+
40+
return 左高 > 右高
41+
? 尋找最大容積(左側座標, 右側座標 - 1, Math.max(max, 容積))
42+
: 尋找最大容積(左側座標 + 1, 右側座標, Math.max(max, 容積))
43+
}
44+
45+
return 尋找最大容積(0, height.length - 1);
46+
}

0 commit comments

Comments
 (0)