File tree Expand file tree Collapse file tree 4 files changed +110
-0
lines changed
container-with-most-water
longest-increasing-subsequence Expand file tree Collapse file tree 4 files changed +110
-0
lines changed Original file line number Diff line number Diff line change 1+ """
2+ # Approach
3+ ํฌํฌ์ธํฐ๋ก ์ ๋์์ ๋ฌผ์ ์์ ๊ณ์ฐํด๋๊ฐ๋๋ค.
4+ ๋ ๋ฎ์ height์ธ ์ชฝ์ ํฌ์ธํฐ๋ฅผ ์ค์ฌ๋๊ฐ๋๋ฐ, ๋์ด๊ฐ ๊ฐ์ ๊ฒฝ์ฐ์๋ ์ผ์ชฝ ํฌ์ธํฐ๋ง ์์ง์
๋๋ค.
5+
6+ # Complexity
7+ height์ ๊ธธ์ด๊ฐ N์ผ ๋
8+ - Time complexity: O(N)
9+ - Space complexity: O(1)
10+ """
11+
12+
13+ class Solution :
14+ def maxArea (self , height : list [int ]) -> int :
15+ n = len (height )
16+ left , right = 0 , n - 1
17+ best = 0
18+ while left < right :
19+ best = max (best , (right - left ) * min (height [left ], height [right ]))
20+ if height [left ] > height [right ]:
21+ right -= 1
22+ else :
23+ left += 1
24+ return best
Original file line number Diff line number Diff line change 1+ """
2+ # Approach
3+ dp[n]์ nums[n] ์์น๊น์ง lis ๊ธธ์ด์
๋๋ค.
4+
5+ # Complexity
6+ nums์ ๊ธธ์ด๋ฅผ n์ด๋ผ๊ณ ํ ๋,
7+ - Time complexity: ์ด์ค ๋ฐ๋ณต๋ฌธ O(N^2)
8+ - Space complexity: dp ๋ฐฐ์ด O(N)
9+ """
10+
11+
12+ class Solution :
13+ def lengthOfLIS (self , nums : list [int ]) -> int :
14+ n = len (nums )
15+ dp = [1 ] * n
16+ answer = 1
17+ for i in range (1 , n ):
18+ for j in range (i ):
19+ if nums [j ] < nums [i ]:
20+ dp [i ] = max (dp [j ] + 1 , dp [i ])
21+ answer = max (answer , dp [i ])
22+ return answer
Original file line number Diff line number Diff line change 1+ """
2+ # Approach
3+ ์ํ์ข์ฐ ๊ฒฝ๊ณ๊ฐ์ ์ค์ ํ๊ณ matrix์ ์ํํ๋ฉฐ ๊ฒฝ๊ณ๊ฐ์ ์ค์ฌ๋๊ฐ๋๋ค.
4+
5+ # Complexity
6+ matrix ํฌ๊ธฐ์ ๊ฐ๋ก๋ฅผ M, ์ธ๋ก๋ฅผ N์ด๋ผ๊ณ ํ ๋
7+ - Time complexity: O(M*N)
8+ - Space complexity: O(M*N)
9+ """
10+
11+
12+ class Solution :
13+ def spiralOrder (self , matrix : list [list [int ]]) -> list [int ]:
14+ start_row , start_col = 0 , 0
15+ end_row , end_col = len (matrix ) - 1 , len (matrix [0 ]) - 1
16+ output = []
17+
18+ while start_row <= end_row and start_col <= end_col :
19+ # ์ข->์ฐ
20+ for col in range (start_col , end_col + 1 ):
21+ output .append (matrix [start_row ][col ])
22+ start_row += 1
23+
24+ # ์->ํ
25+ for row in range (start_row , end_row + 1 ):
26+ output .append (matrix [row ][end_col ])
27+ end_col -= 1
28+
29+ # ์ฐ->์ข
30+ if start_row <= end_row :
31+ for col in range (end_col , start_col - 1 , - 1 ):
32+ output .append (matrix [end_row ][col ])
33+ end_row -= 1
34+
35+ # ํ->์
36+ if start_col <= end_col :
37+ for row in range (end_row , start_row - 1 , - 1 ):
38+ output .append (matrix [row ][start_col ])
39+ start_col += 1
40+
41+ return output
Original file line number Diff line number Diff line change 1+ """
2+ # Approach
3+ ์คํ์ ์ด์ฉํ์ฌ ๋ซ๋ ๊ดํธ๊ฐ ๋์ฌ ๋ ์คํ top๊ณผ ๋น๊ตํ์ฌ ์ง์ด ๋ง์ผ๋ฉด popํฉ๋๋ค.
4+ ์ง์ด ๋ง์ง ์์ผ๋ฉด invalid, ์ต์ข
์ ์ผ๋ก ์คํ์ด ๋น์ด์์ง ์์ผ๋ฉด invalid ํฉ๋๋ค.
5+
6+ # Complexity
7+ s์ ๊ธธ์ด๋ฅผ N์ด๋ผ๊ณ ํ ๋
8+ - Time complexity: O(N)
9+ - Space complexity: O(N)
10+ """
11+
12+
13+ class Solution :
14+ def isValid (self , s : str ) -> bool :
15+ stack = []
16+ brackets = {"(" : ")" , "{" : "}" , "[" : "]" }
17+ for b in s :
18+ if b in brackets :
19+ stack .append (b )
20+ continue
21+ if not stack or brackets [stack .pop ()] != b :
22+ return False
23+ return not stack
You canโt perform that action at this time.
0 commit comments