File tree 5 files changed +105
-0
lines changed
longest-common-subsequence
longest-repeating-character-replacement 5 files changed +105
-0
lines changed Original file line number Diff line number Diff line change
1
+ """
2
+ # Definition for a Node.
3
+ class Node:
4
+ def __init__(self, val = 0, neighbors = None):
5
+ self.val = val
6
+ self.neighbors = neighbors if neighbors is not None else []
7
+ """
8
+
9
+ from typing import Optional
10
+ class Solution :
11
+ def cloneGraph (self , node : Optional ['Node' ]) -> Optional ['Node' ]:
12
+
13
+ # ๊น์ ๋ณต์ฌ
14
+ # DFS ์ด์ฉํ์ผ๋ ๋์ค์ BFS๋ก๋ ํด ๋ณผ ๊ฒ
15
+ # ์๊ฐ๋ณต์ก๋ O(n), ๊ณต๊ฐ๋ณต์ก๋ O(n)
16
+
17
+ if not node :
18
+ return None
19
+
20
+ # ์๋ณธ์์ ๋ณต์ฌ๋
ธ๋ ๋งคํ ์ ์ฅํ ๋์
๋๋ฆฌ
21
+ copied = {}
22
+
23
+ def dfs (curr ):
24
+ # ํ์ฌ ๋
ธ๋๊ฐ ๋ณต์ฌ๋ ๋
ธ๋๋ฉด ๊ทธ๋๋ก ๋ฆฌํด
25
+ if curr in copied :
26
+ return copied [curr ]
27
+
28
+ copy = Node (curr .val ) # ํ์ฌ ๋
ธ๋ ๋ณต์ฌ
29
+ copied [curr ] = copy # ๋ณต์ฌ๋ณธ ์ ์ฅ
30
+
31
+ # ์ด์๋
ธ๋๋ค ๋ณต์ฌํด์ ์ฐ๊ฒฐ
32
+ for i in curr .neighbors :
33
+ copy .neighbors .append (dfs (i ))
34
+
35
+ return copy
36
+
37
+ return dfs (node )
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def longestCommonSubsequence (self , text1 : str , text2 : str ) -> int :
3
+ # DP (์๊ฐ๋ณต์ก๋ : O(len(text1)*len(text2)), ๊ณต๊ฐ๋ณต์ก๋ : O(len(text1)*len(text2)))
4
+ # dp[i][j] : text1์ ์i๊ธ์์ text2์ ์j๊ธ์ ์ฌ์ด์ LCS(๊ฐ์ฅ๊ธด๊ณตํต๋ถ๋ถ์์ด)๊ธธ์ด
5
+ dp = [[0 ]* (len (text2 )+ 1 ) for _ in range (len (text1 )+ 1 )]
6
+
7
+ for i in range (1 , len (text1 )+ 1 ):
8
+ for j in range (1 , len (text2 )+ 1 ):
9
+
10
+ # ํ์ฌ ๋ฌธ์๊ฐ ๊ฐ๋ค๋ฉด, ์ด์ ๋๊ฐ์ ๊ฐ์ +1
11
+ if text1 [i - 1 ] == text2 [j - 1 ]:
12
+ dp [i ][j ] = dp [i - 1 ][j - 1 ]+ 1
13
+
14
+ # ํ์ฌ ๋ฌธ์๊ฐ ๋ค๋ฅด๋ค๋ฉด, ์ผ์ชฝ์ด๋ ์์ชฝ ์ค ๋ ํฐ ๊ฐ
15
+ else :
16
+ dp [i ][j ] = max (dp [i - 1 ][j ], dp [i ][j - 1 ])
17
+
18
+ return dp [len (text1 )][len (text2 )]
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def characterReplacement (self , s : str , k : int ) -> int :
3
+
4
+ # ์ฌ๋ผ์ด๋ฉ ์๋์ฐ(ํฌํฌ์ธํฐ)
5
+ count = {} # ๋ฌธ์ ๋น๋์
6
+ left = 0 # ์ผ์ชฝ ํฌ์ธํฐ
7
+ max_count = 0 # ํ์ฌ ์๋์ฐ์์ ์ต๋ ๋ฌธ์ ๋น๋์
8
+ answer = 0 # ๊ฐ์ฅ ๊ธด ๋์ผ ๋ฌธ์ ๋ถ๋ถ ๋ฌธ์์ด์ ์ต๋ ๊ธธ์ด
9
+
10
+ # ์ค๋ฅธ์ชฝ ํฌ์ธํฐ๋ฅผ ํ ์นธ์ฉ ๋๋ ค๊ฐ๋ฉฐ ์๋์ฐ ํ์ฅ
11
+ for right in range (len (s )):
12
+ count [s [right ]] = count .get (s [right ],0 ) + 1 # ํ์ฌ ๋ฌธ์ ์นด์ดํธ ์ฆ๊ฐ
13
+ max_count = max (max_count , count [s [right ]]) # ๊ฐ์ฅ ๋ง์ด ๋ฑ์ฅํ ๋ฌธ์ ์ ๊ฐฑ์
14
+
15
+ # ์๋์ฐ๊ธธ์ด(right-left+1) - ๊ฐ์ฅ์์ฃผ๋์จ๋ฌธ์ ๋น๋์ > k : ํ์ฌ ์๋์ฐ์์ ๋ฐ๊ฟ์ผํ๋ ๋ฌธ์๊ฐ ๋ ๋ง์ผ๋ฉด ์ผ์ชฝ ํฌ์ธํฐ ์ด๋
16
+ if (right - left + 1 ) - max_count > k :
17
+ count [s [left ]] -= 1 # ์ผ์ชฝ ๋ฌธ์ ์ ๊ฑฐ
18
+ left += 1 # ์ผ์ชฝ ํฌ์ธํฐ ์ค๋ฅธ์ชฝ์ผ๋ก ํ ์นธ ์ด๋
19
+
20
+ answer = max (answer , right - left + 1 )
21
+
22
+ return answer
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def countSubstrings (self , s : str ) -> int :
3
+
4
+ # DP
5
+ dp = [[False ]* (len (s )) for _ in range (len (s ))] # dp ํ
์ด๋ธ ์ด๊ธฐํ(dp[i][j]=s[i:j+1]์ ํฐ๋ฆฐ๋๋กฌ ์ฌ๋ถ)
6
+ answer = 0
7
+
8
+ # j = ๋๊ธ์์ธ๋ฑ์ค, i = ์ฒ์๊ธ์์ธ๋ฑ์ค
9
+ for j in range (len (s )):
10
+ for i in range (j + 1 ):
11
+ # 1. s[i](์ฒซ๊ธ์)์ s[j](๋๊ธ์)๊ฐ ๊ฐ์ ๊ธ์์ด๊ณ ,
12
+ # 2. j-i <= 2, ์ฆ ๊ธ์ ๊ธธ์ด๊ฐ 1~3์ผ๋๋ ๋ฌด์กฐ๊ฑด ํฐ๋ฆฌ๋๋กฌ /์๋๋ฉด s[i+1][j-1](๊ฐ์ด๋ฐ๊ธ์๋ค)์ด ํฐ๋ฆฐ๋๋ฃธ์ด๋ฉด s[i]๋ถํฐ s[j]๊น์ง ํฐ๋ฆฐ๋๋ฃธ.
13
+ if s [i ] == s [j ] and (j - i <= 2 or dp [i + 1 ][j - 1 ]):
14
+ dp [i ][j ] = True # s[i]์์ s[j]๋ ํฐ๋ฆฐ๋๋ฃธ(True)
15
+ answer += 1
16
+
17
+ return answer
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def reverseBits (self , n : int ) -> int :
3
+
4
+ answer = 0
5
+
6
+ for i in range (32 ):
7
+ answer <<= 1 # ์ผ์ชฝ์ผ๋ก ํ ์นธ ๋ฐ์ด์ ์๋ฆฌ ํ๋ณด
8
+ answer |= (n & 1 ) # n์ ๋ง์ง๋ง ๋นํธ ์ถ์ถํด์ answer ๋งจ ๋ค์ ์ถ๊ฐ
9
+ n >>= 1 # n ์ค๋ฅธ์ชฝ์ผ๋ก ํ ์นธ ๋ฐ๊ธฐ
10
+
11
+ return answer
You canโt perform that action at this time.
0 commit comments