File tree Expand file tree Collapse file tree 5 files changed +129
-0
lines changed
longest-common-subsequence
longest-repeating-character-replacement Expand file tree Collapse file tree 5 files changed +129
-0
lines changed Original file line number Diff line number Diff line change 1+ '''
2+ ์๊ฐ ๋ณต์ก๋: O(V + E)
3+ - ๊ทธ๋ํ์ ๋ชจ๋ ๋
ธ๋๋ฅผ ํ ๋ฒ์ฉ ๋ฐฉ๋ฌธํด์ผ ํ๋ฏ๋ก O(V)
4+ - ๊ฐ ๋
ธ๋์ ๋ชจ๋ ๊ฐ์ ์ ํ ๋ฒ์ฉ ํ์ํด์ผ ํ๋ฏ๋ก O(E)
5+ - ๋ฐ๋ผ์ ์ ์ฒด ์๊ฐ ๋ณต์ก๋๋ O(V + E)
6+
7+ ๊ณต๊ฐ ๋ณต์ก๋: O(V + E)
8+ - ํด๋ก ๋
ธ๋๋ฅผ ์ ์ฅํ๋ ๋์
๋๋ฆฌ(clones): O(V)
9+ - BFS ํ์์ ์ํ ํ(queue): O(V)
10+ - ๋ณต์ ๋ ๊ทธ๋ํ์ ๋
ธ๋์ ๊ฐ์ ์ ์ฅ ๊ณต๊ฐ: O(V + E)
11+ '''
12+
13+ from typing import Optional
14+ from collections import deque
15+ # Definition for a Node.
16+ class Node :
17+ def __init__ (self , val = 0 , neighbors = None ):
18+ self .val = val
19+ self .neighbors = neighbors if neighbors is not None else []
20+
21+ class Solution :
22+ def cloneGraph (self , node : Optional ['Node' ]) -> Optional ['Node' ]:
23+ if not node :
24+ return None
25+
26+ clones = { node .val : Node (node .val ) }
27+ queue = deque ([node ])
28+
29+ while queue :
30+ current_node = queue .popleft ()
31+
32+ for neighbor in current_node .neighbors :
33+ # add neighbors
34+ if neighbor .val not in clones .keys ():
35+ queue .append (neighbor )
36+ clones [neighbor .val ] = Node (neighbor .val )
37+
38+ clones [current_node .val ].neighbors .append (clones [neighbor .val ])
39+
40+ return clones [node .val ]
Original file line number Diff line number Diff line change 1+ '''
2+ ์๊ฐ ๋ณต์ก๋: O(m * n)
3+ ๊ณต๊ฐ ๋ณต์ก๋: O(n)
4+ '''
5+
6+ class Solution :
7+ def longestCommonSubsequence (self , text1 : str , text2 : str ) -> int :
8+ m , n = len (text1 ), len (text2 )
9+ prev = [0 ] * (n + 1 )
10+
11+ for i in range (1 , m + 1 ):
12+ curr = [0 ] * (n + 1 )
13+ for j in range (1 , n + 1 ):
14+ if text1 [i - 1 ] == text2 [j - 1 ]:
15+ curr [j ] = prev [j - 1 ] + 1
16+ else :
17+ curr [j ] = max (prev [j ], curr [j - 1 ])
18+ prev = curr # ํ์ฌ ํ์ ์ด์ ํ์ผ๋ก ์
๋ฐ์ดํธ
19+
20+ return prev [n ]
Original file line number Diff line number Diff line change 1+ '''
2+ ์๊ฐ๋ณต์ก๋: O(n)
3+ - ๋ฌธ์์ด์ ๊ธธ์ด๋งํผ ํ ๋ฒ๋ง ์ํํฉ๋๋ค.
4+ ๊ณต๊ฐ๋ณต์ก๋: O(n)
5+ - char_count ๋์
๋๋ฆฌ๋ ์ต๋ ์ํ๋ฒณ 26๊ฐ๋ง ์ ์ฅํฉ๋๋ค.
6+ '''
7+
8+ class Solution :
9+ def characterReplacement (self , s : str , k : int ) -> int :
10+ left = 0
11+ max_count = 0
12+ max_length = 0
13+ char_count = {}
14+
15+ for right in range (len (s )):
16+ char_count [s [right ]] = char_count .get (s [right ], 0 ) + 1
17+ max_count = max (max_count , char_count [s [right ]])
18+
19+ # If the remaining characters exceed the allowed k changes
20+ while (right - left + 1 ) - max_count > k :
21+ char_count [s [left ]] -= 1
22+ left += 1
23+
24+ max_length = max (max_length , right - left + 1 )
25+
26+ return max_length
Original file line number Diff line number Diff line change 1+ '''
2+ ์๊ฐ ๋ณต์ก๋
3+ - format(n, 'b'): ์ ์๋ฅผ ์ด์ง ๋ฌธ์์ด๋ก ๋ณํํ๋ ์์
์ O(k)์
๋๋ค.
4+ - Counter(bits): ๋ฌธ์์ด์ ์ํํ๋ฉด์ ๊ฐ ๋ฌธ์์ ๋น๋๋ฅผ ๊ณ์ฐํ๋ฉฐ, ์ด ์์
๋ ๋ฌธ์์ด ๊ธธ์ด k์ ๋น๋กํฉ๋๋ค.
5+ - count['1']: ๋์
๋๋ฆฌ ์กฐํ๋ ์์ ์๊ฐ์ด๋ฏ๋ก O(1)์
๋๋ค.
6+
7+ ์ด ์๊ฐ ๋ณต์ก๋: O(k) + O(k) + O(1) = O(k)
8+
9+ ๊ณต๊ฐ ๋ณต์ก๋
10+ - format(n, 'b'): ์์ฑ๋ ์ด์ง ๋ฌธ์์ด์ ๊ธธ์ด k๋ฅผ ์ฐจ์งํฉ๋๋ค.
11+ - Counter(bits): ๋์
๋๋ฆฌ ํํ๋ก ๊ฐ ๋ฌธ์์ ๋น๋๋ฅผ ์ ์ฅํฉ๋๋ค. ์ต์
์ ๊ฒฝ์ฐ, ๋ ๊ฐ์ง ๋ฌธ์(โ0โ๊ณผ โ1โ)๋ง ์์ผ๋ฏ๋ก ๊ณต๊ฐ ๋ณต์ก๋๋ O(2) = O(1)๋ก ๊ฐ์ฃผํ ์ ์์ต๋๋ค.
12+
13+ ์ด ๊ณต๊ฐ ๋ณต์ก๋: O(k)
14+ '''
15+
16+ from collections import Counter
17+
18+ class Solution :
19+ def hammingWeight (self , n : int ) -> int :
20+ bits = format (n , 'b' )
21+ count = Counter (bits )
22+
23+ return count ['1' ]
Original file line number Diff line number Diff line change 1+ '''
2+ ์๊ฐ ๋ณต์ก๋: O(1)
3+ - ๋ง์
์ ๋นํธ ์ฐ์ฐ์ ์ด์ฉํ์ฌ ์ํ๋๋ฉฐ, ์ ์ ํฌ๊ธฐ๊ฐ ๊ณ ์ ๋์ด ์๊ธฐ ๋๋ฌธ์ ์ฐ์ฐ ํ์๊ฐ ์ ํ์ ์
๋๋ค.
4+
5+ ๊ณต๊ฐ ๋ณต์ก๋: O(1)
6+ - ์ถ๊ฐ์ ์ธ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๊ฑฐ์ ์ฌ์ฉํ์ง ์์ต๋๋ค.
7+ '''
8+
9+ class Solution :
10+ def getSum (self , a : int , b : int ) -> int :
11+ MASK = 0xFFFFFFFF # 32๋นํธ ์ ์ ๋ง์คํฌ
12+ MAX_INT = 0x7FFFFFFF # 32๋นํธ ์ ์์ ์ต๋๊ฐ
13+
14+ while b :
15+ carry = (a & b ) << 1 # ์๋ฆฌ ์ฌ๋ฆผ ๊ณ์ฐ
16+ a = (a ^ b ) & MASK # ๋ง์
์ํ
17+ b = carry & MASK # ์๋ฆฌ ์ฌ๋ฆผ๊ฐ์ ๋ฐ์ํ์ฌ ๋ค์ ์ฐ์ฐ ์งํ
18+
19+ # ์์ ์ฒ๋ฆฌ๋ฅผ ์ํด 32๋นํธ ์ด๊ณผ ์ ๋ณด์
20+ return a if a <= MAX_INT else ~ (a ^ MASK )
You canโt perform that action at this time.
0 commit comments