Skip to content

Commit 88fe786

Browse files
authored
feat: container-with-most-water
1 parent 718d0c3 commit 88fe786

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @param {number[]} height
3+
* @return {number}
4+
*/
5+
var maxArea = function(height) {
6+
// x축이 가장 길면서 y축 역시 가장 높은 즉 높이가 가장 높은 값을 찾으라는 의미.
7+
// startIndex값과 그 값에 해당하는 height를 받고
8+
// endIndex의 값과 그 값에 해당하는 height를 받는다.
9+
// 여기서 가장 큰 점은 startIndex와 endIndex가 멀면 멀수록 좋다
10+
// 그리고 그 값을 하나씩 줄여가면서 더 큰 값을 찾아 적용한다
11+
12+
let max = 0;
13+
14+
let startIndex = 0;
15+
let endIndex = height.length -1;
16+
17+
while(startIndex < endIndex){
18+
const startHeight = height[startIndex];
19+
const endHeight = height[endIndex];
20+
21+
const xLength = endIndex - startIndex;
22+
const minHeight = Math.min(startHeight, endHeight);
23+
24+
const size = xLength * minHeight;
25+
26+
max = Math.max(size,max);
27+
28+
if(startHeight >= endHeight){
29+
endIndex -=1;
30+
}else{
31+
startIndex +=1;
32+
}
33+
};
34+
35+
return max
36+
};

0 commit comments

Comments
 (0)