File tree 2 files changed +55
-0
lines changed
container-with-most-water
2 files changed +55
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[] } height
3
+ * @return {number }
4
+ */
5
+ var maxArea = function ( height ) {
6
+ // Two Pointer를 이용해서 문제를 접근해야할 것 같은데 두 포인터 중에 어떤 거를 이동시켜야하는지 이 부분이 고민이 됨.
7
+ // 그래서 이 부분은 풀이 참고
8
+ let maxArea = 0 ;
9
+ let start = 0 ,
10
+ end = height . length - 1 ;
11
+
12
+ while ( start < end ) {
13
+ // 가로 * 세로
14
+ let area = ( end - start ) * Math . min ( height [ start ] , height [ end ] ) ;
15
+
16
+ maxArea = Math . max ( area , maxArea ) ;
17
+
18
+ // 고민했던 지점
19
+ if ( height [ start ] < height [ end ] ) {
20
+ start ++ ;
21
+ } else {
22
+ end -- ;
23
+ }
24
+ }
25
+ return maxArea ;
26
+ } ;
27
+
28
+ // 시간복잡도: O(n)
29
+ // 공간복잡도: O(1)
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {string } s
3
+ * @return {boolean }
4
+ */
5
+ var isValid = function ( s ) {
6
+ // 괄호 관리 스택
7
+ const stack = [ ] ;
8
+
9
+ // 여는 괄호, 닫는 괄호 매핑
10
+ const brackets = { "(" : ")" , "{" : "}" , "[" : "]" } ;
11
+
12
+ // for문 돌며 확인
13
+ for ( let i of s ) {
14
+ // 여는 괄호일 경우
15
+ if ( brackets [ i ] ) {
16
+ stack . push ( brackets [ i ] ) ;
17
+ // 닫는 괄호일 경우
18
+ } else if ( stack . length === 0 || i !== stack . pop ( ) ) {
19
+ return false ;
20
+ }
21
+ }
22
+ return stack . length === 0 ;
23
+ } ;
24
+
25
+ // 시간복잡도: O(n)
26
+ // 공간복잡도: O(n)
You can’t perform that action at this time.
0 commit comments