File tree 6 files changed +131
-12
lines changed
longest-common-subsequence
longest-consecutive-sequence
longest-repeating-character-replacement 6 files changed +131
-12
lines changed Original file line number Diff line number Diff line change
1
+ from typing import Optional
2
+ from collections import deque
3
+
4
+
5
+ class Solution :
6
+ # 시간복잡도: O(N) node 개수: N
7
+ # 공간복잡도: O(N)
8
+ def cloneGraph (self , node : Optional ['Node' ]) -> Optional ['Node' ]:
9
+ if node :
10
+ visited = {}
11
+ copy = Node (val = node .val )
12
+ visited [copy .val ] = copy
13
+ q = deque ()
14
+ q .append ((copy , node ))
15
+
16
+ while q :
17
+ cur , node = q .popleft ()
18
+
19
+ for idx , next_node in enumerate (node .neighbors ):
20
+ if next_node .val not in visited :
21
+ new = Node (val = next_node .val )
22
+ visited [new .val ] = new
23
+ q .append ((new , next_node ))
24
+ cur .neighbors .append (visited [next_node .val ])
25
+
26
+ return copy
27
+
28
+ return node
Original file line number Diff line number Diff line change 1
- # 시간복잡도: O(N)
2
- # 공간복잡도: O(N)
3
1
class Solution :
4
- def longestConsecutive (self , nums : List [int ]) -> int :
5
- nums = set (nums )
6
- answer = 0
2
+ # 시간복잡도: O(A*B)
3
+ # 공간복잡도: O(A*B)
4
+ def longestCommonSubsequence (self , text1 : str , text2 : str ) -> int :
5
+ a = len (text1 )
6
+ b = len (text2 )
7
7
8
- for num in nums :
9
- if num - 1 not in nums :
10
- length = 1
8
+ lcs = [[0 ]* (b + 1 ) for _ in range (a + 1 )]
11
9
12
- while num + length in nums :
13
- length += 1
10
+ for i in range (1 , a + 1 ):
11
+ for j in range (1 , b + 1 ):
12
+ if text1 [i - 1 ] == text2 [j - 1 ]:
13
+ lcs [i ][j ] = lcs [i - 1 ][j - 1 ] + 1
14
+ lcs [i ][j ] = max (lcs [i ][j ], lcs [i - 1 ][j ], lcs [i ][j - 1 ])
14
15
15
- answer = max ( answer , length )
16
+ return lcs [ a ][ b ]
16
17
17
- return answer
18
+ # 시간복잡도: O(A*B)
19
+ # 공간복잡도: O(A)
20
+ def longestCommonSubsequence2 (self , text1 : str , text2 : str ) -> int :
21
+ n = len (text1 )
22
+ lcs = [0 ]* n
23
+ longest = 0
24
+ for ch in text2 :
25
+ cur = 0
26
+ for i in range (n ):
27
+ if cur < lcs [i ]:
28
+ cur = lcs [i ]
29
+ elif ch == text1 [i ]:
30
+ lcs [i ] = cur + 1
31
+ longest = max (longest , cur + 1 )
32
+
33
+ return longest
Original file line number Diff line number Diff line change
1
+ # 시간복잡도: O(N)
2
+ # 공간복잡도: O(N)
3
+ class Solution :
4
+ def longestConsecutive (self , nums : List [int ]) -> int :
5
+ nums = set (nums )
6
+ answer = 0
7
+
8
+ for num in nums :
9
+ if num - 1 not in nums :
10
+ length = 1
11
+
12
+ while num + length in nums :
13
+ length += 1
14
+
15
+ answer = max (answer , length )
16
+
17
+ return answer
Original file line number Diff line number Diff line change
1
+ from collections import defaultdict
2
+
3
+ class Solution :
4
+ # 시간복잡도: O(N)
5
+ # 공간복잡도: O(N)
6
+ def characterReplacement (self , s : str , k : int ) -> int :
7
+
8
+ n = len (s )
9
+ l , r = 0 , 1
10
+
11
+ d = defaultdict (int )
12
+ d [s [l ]] += 1
13
+
14
+ longest = 1
15
+ while l < r and r < n :
16
+ d [s [r ]] += 1
17
+ if r - l + 1 - max (d .values ()) > k :
18
+ d [s [l ]] -= 1
19
+ l += 1
20
+ longest = max (longest , r - l + 1 )
21
+ r += 1
22
+
23
+ return longest
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ # 시간복잡도: O(N+M) list1:N, list2: M
3
+ # 공간복잡도: O(N+M)
4
+ def mergeTwoLists (self , list1 : Optional [ListNode ], list2 : Optional [ListNode ]) -> Optional [ListNode ]:
5
+ merged = ListNode ()
6
+ cur = merged
7
+
8
+ while list1 and list2 :
9
+ if list1 .val < list2 .val :
10
+ cur .next = list1
11
+ list1 = list1 .next
12
+ else :
13
+ cur .next = list2
14
+ list2 = list2 .next
15
+ temp = temp .next
16
+
17
+ if list1 :
18
+ cur .next = list1
19
+
20
+ if list2 :
21
+ cur .next = list2
22
+
23
+ return merged .next
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ // time: O(1)
3
+ // space: O(1)
4
+ public int getSum (int a , int b ) {
5
+ while (b != 0 ) {
6
+ int tmp = (a & b ) << 1 ;
7
+ a = a ^ b ;
8
+ b = tmp ;
9
+ }
10
+ return a ;
11
+ }
12
+ }
You can’t perform that action at this time.
0 commit comments