File tree Expand file tree Collapse file tree 5 files changed +80
-0
lines changed
longest-common-subsequence
longest-repeating-character-replacement Expand file tree Collapse file tree 5 files changed +80
-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
+ if not node :
13
+ return None
14
+
15
+ visited = [None ] * 101
16
+
17
+ def dfs (n ):
18
+ if visited [n .val ] is not None :
19
+ return visited [n .val ]
20
+
21
+ copy = Node (n .val , [])
22
+ visited [n .val ] = copy
23
+
24
+ for nei in n .neighbors :
25
+ copy .neighbors .append (dfs (nei ))
26
+ return copy
27
+
28
+ return dfs (node )
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ """
3
+ 문제의 힌트에서 DP를 활용하는 것을 확인
4
+ """
5
+ def longestCommonSubsequence (self , text1 : str , text2 : str ) -> int :
6
+ dp = [[0 ] * (len (text2 )+ 1 ) for _ in range (len (text1 )+ 1 )]
7
+
8
+ for i in range (1 , len (text1 )+ 1 ):
9
+ for j in range (1 , len (text2 )+ 1 ):
10
+ if text1 [i - 1 ] == text2 [j - 1 ]:
11
+ dp [i ][j ] = dp [i - 1 ][j - 1 ] + 1
12
+ else :
13
+ dp [i ][j ] = max (dp [i - 1 ][j ], dp [i ][j - 1 ])
14
+
15
+ return dp [- 1 ][- 1 ]
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def characterReplacement (self , s : str , k : int ) -> int :
3
+ counts = {}
4
+ left = 0
5
+ max_count = 0
6
+ res = 0
7
+
8
+ for right in range (len (s )):
9
+ counts [s [right ]] = counts .get (s [right ], 0 ) + 1
10
+ max_count = max (max_count , counts [s [right ]])
11
+
12
+ while (right - left + 1 ) - max_count > k :
13
+ counts [s [left ]] -= 1
14
+ left += 1
15
+
16
+ res = max (res , right - left + 1 )
17
+
18
+ return res
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ """
3
+ 브루트포스로 모두 탐색하면서 집어넣는 방법
4
+ O(n^2)
5
+ """
6
+ def countSubstrings (self , s : str ) -> int :
7
+ cnt = []
8
+ for i in range (len (s )):
9
+ for j in range (i , len (s )):
10
+ sub = s [i :j + 1 ]
11
+ if sub and sub == sub [::- 1 ]:
12
+ cnt .append (sub )
13
+ return len (cnt )
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ """
3
+ binary로 변환 후 zfill로 32자리를 맞춰주고 reverse시킨다.
4
+ """
5
+ def reverseBits (self , n : int ) -> int :
6
+ return int (bin (n )[2 :].zfill (32 )[::- 1 ], 2 )
You can’t perform that action at this time.
0 commit comments