File tree 4 files changed +123
-0
lines changed
container-with-most-water
longest-increasing-subsequence
4 files changed +123
-0
lines changed Original file line number Diff line number Diff line change
1
+ """
2
+ 풀이 :
3
+ left, right 커서를 양 끝에 놓고 조건에 따라 반대편으로 이동시키며 물 양 비교
4
+ 둘 중 더 높은 높이를 이동시키면 무조건 물의 양이 줄어들기 때문에 더 낮거나 같은 커서를 이동시키며 업데이트
5
+ 둘이 만나면 비교 종료
6
+
7
+
8
+ len(height) : N
9
+ TC : O(N)
10
+ l, r의 이동이 전체 height 개수만큼 일어나므로
11
+ SC : O(1)
12
+ """
13
+
14
+ class Solution :
15
+ def maxArea (self , height : List [int ]) -> int :
16
+ l = 0
17
+ r = len (height ) - 1
18
+ max_area = 0
19
+ while (l != r ) :
20
+ cur_area = (r - l ) * min (height [l ], height [r ])
21
+ max_area = max (cur_area , max_area )
22
+ if (height [l ] >= height [r ]) :
23
+ r -= 1
24
+ else :
25
+ l += 1
26
+ return max_area
Original file line number Diff line number Diff line change
1
+ """
2
+ 풀이 :
3
+ 해당 인덱스의 숫자로 끝나는 LIS의 길이를 dp배열에 저장
4
+ nums[i] 와 그 이후에 오는 nums[j]를 비교해 nums[j]가 크고
5
+ i까지의 LIS 길이 + 1 > j까지의 LIS길이이면 j까지의 LIS길이 업데이트
6
+ 업데이트 할 때마다 max길이와 비교해서 최대길이 업데이트
7
+
8
+ nums의 길이 N
9
+ TC : O(N^2)
10
+ for문의 개수를 살펴보면 N-1 + N-2 + ... + 1 = (N-1)N/2 이므로 N^2/2 -> O(N^2)
11
+
12
+ SC : O(N)
13
+ dp의 길이는 nums의 길이에 비례하므로
14
+ """
15
+
16
+ class Solution :
17
+ def lengthOfLIS (self , nums : List [int ]) -> int :
18
+ dp = [1 ] * len (nums )
19
+ max_LIS = 1
20
+ for i in range (len (nums ) - 1 ) :
21
+ for j in range (i + 1 , len (nums )) :
22
+ if nums [i ] < nums [j ] and dp [i ] + 1 > dp [j ] :
23
+ dp [j ] = dp [i ] + 1
24
+ max_LIS = max (max_LIS , dp [j ])
25
+ return max_LIS
Original file line number Diff line number Diff line change
1
+ """
2
+ 풀이 :
3
+ 오른쪽, 아래쪽으로 이동할 땐 각각 column과 row가 증가하므로 direction 1
4
+ 왼쪽, 위쪽은 감소하므로 direction -1
5
+ 로 설정하고 n_row 또는 n_col만큼 direction을 이동하면서 ans에 담아준다
6
+ 각 for문이 끝날 때마다 n_row 또는 n_col을 감소시켜 주고
7
+ 둘 중 하나가 0이 될 때까지 계속 진행
8
+
9
+ - col이 -1부터 시작해서 matrix[0][0]부터 append할 수 있도록 유의
10
+ - n_cols, n_rows 둘 중 하나만 0이되도 끝남에 유의
11
+
12
+ TC : O(M * N)
13
+ matrix 전체를 한번씩 순회해야하므로
14
+
15
+ SC : O(1)
16
+ return할 리스트 외에는 추가적인 메모리 사용이 상수개(5개)이므로
17
+ """
18
+
19
+ class Solution :
20
+ def spiralOrder (self , matrix : List [List [int ]]) -> List [int ]:
21
+ ans = []
22
+ n_rows = len (matrix )
23
+ n_cols = len (matrix [0 ])
24
+
25
+ row = 0
26
+ col = - 1
27
+ direction = 1
28
+
29
+ while n_cols and n_rows :
30
+ for _ in range (n_cols ):
31
+ col += direction
32
+ ans .append (matrix [row ][col ])
33
+ n_rows -= 1
34
+
35
+ for _ in range (n_rows ):
36
+ row += direction
37
+ ans .append (matrix [row ][col ])
38
+ n_cols -= 1
39
+
40
+ direction *= - 1
41
+
42
+ return ans
Original file line number Diff line number Diff line change
1
+ """
2
+ 풀이 :
3
+ stack구조를 이용해서 구현
4
+ 괄호의 페어를 딕셔너리의 key, value를 이용해 매치시킨다
5
+ 여는 괄호를 만나면 stack에 push, 닫는 괄호를 만나면 stack의 마지막 괄호의 pair일 때 pop, 아니면 return False
6
+ 문자열 전체를 순회했을 때 stack에 남아있으면 안 닫힌 괄호가 있으므로 return False
7
+
8
+ s의 길이 N
9
+
10
+ TC : O(N)
11
+ s에 대해 한번 for문
12
+ SC : O(N)
13
+ stack의 최대 크기는 s의 길이와 비례
14
+ """
15
+
16
+ class Solution :
17
+ def isValid (self , s : str ) -> bool :
18
+ stack = []
19
+ pair = dict (zip ('({[' ,')}]' ))
20
+ for paren in s :
21
+ if paren in pair :
22
+ stack .append (paren )
23
+ elif not stack :
24
+ return False
25
+ elif pair [stack .pop ()] != paren :
26
+ return False
27
+ if not stack :
28
+ return True
29
+ else :
30
+ return False
You can’t perform that action at this time.
0 commit comments