File tree 4 files changed +110
-0
lines changed
longest-repeating-character-replacement
4 files changed +110
-0
lines changed Original file line number Diff line number Diff line change
1
+ """
2
+ ํ์ด :
3
+ ์ฌ๊ท๋ฅผ ์ด์ฉํด์ dfsํ์ด
4
+ node๋ฅผ ๋ณต์ ํ๊ณ ๋
ธ๋์ ์ด์๋ ๋
ธ๋์ ๋ํด์ ์ฌ๊ทํจ์ ํธ์ถ์ ํตํด ์์ฑํ๋ค
5
+
6
+ clones ๋์
๋๋ฆฌ์ ์ด๋ฏธ ๋ณต์ฌ๋ node๋ค์ ์ ์ฅํด์ ์ด๋ฏธ ๋ณต์ ๋ node์ ๋ํด
7
+ ํจ์๋ฅผ ํธ์ถํ๋ฉด ๋ฐ๋ก return
8
+
9
+ ๋
ธ๋์ ์ : V(์ ์ : Vertex) ์ด์์ ์ : E(๊ฐ์ : Edge)๋ผ๊ณ ํ ๋
10
+
11
+ TC : O(V + E)
12
+ ๋
ธ๋์ ์ด์์ ๋ํด์ ์ํํ๋ฏ๋ก
13
+
14
+ SC : O(V + E)
15
+ ํด์ํ
์ด๋ธ์ ํฌ๊ธฐ๊ฐ ๋
ธ๋์ ์์ ๋น๋กํด์ ์ปค์ง๊ณ
16
+ dfs์ ํธ์ถ์คํ์ ์ด์์ ์๋งํผ ์์ด๋ฏ๋ก
17
+ """
18
+
19
+ """
20
+ # Definition for a Node.
21
+ class Node:
22
+ def __init__(self, val = 0, neighbors = None):
23
+ self.val = val
24
+ self.neighbors = neighbors if neighbors is not None else []
25
+ """
26
+ from typing import Optional
27
+
28
+ class Solution :
29
+ def cloneGraph (self , node : Optional ['Node' ]) -> Optional ['Node' ]:
30
+ if not node :
31
+ return None
32
+
33
+ clones = {}
34
+
35
+ def dfs (node : Optional ['Node' ]) -> Optional ['Node' ]:
36
+ if node in clones :
37
+ return clones [node ]
38
+ clone = Node (node .val )
39
+ clones [node ] = clone
40
+ for nei in node .neighbors :
41
+ clone .neighbors .append (dfs (nei ))
42
+ return clone
43
+
44
+ return dfs (node )
Original file line number Diff line number Diff line change
1
+ """
2
+ ํ์ด :
3
+ ๋ถ๋ถ๋ฌธ์์ด ์ค์ ๊ฐ์ฅ ๋ง์ ๋น๋์ ๋ฌธ์๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ค๋ฅธ ๋ฌธ์๊ฐ k๊ฐ ์ดํ๋ก ์์ผ๋ฉด
4
+ ๊ฐ์ ๋ฐ๋ณต๋ฌธ์์ด๋ก ๋ง๋ค ์ ์๋ค
5
+ sliding window ๊ธฐ๋ฒ์ ํตํด ์ต๋ค๋น๋ ๋ฌธ์์ ๋ค๋ฅธ ๋ฌธ์๊ฐ k๊ฐ ์ด๊ณผ์ด๋ฉด start๋ฅผ ์ด๋์ํจ๋ค
6
+
7
+ SC : O(N)
8
+ start, endํฌ์ธํฐ๋ ๋ฌธ์์ด ๊ธธ์ด์ ๋น๋กํด ๋ฐ๋ณตํด์ ์ด๋ํ๋ฏ๋ก (max ์ฐ์ฐ์ O(26))
9
+
10
+ TC : O(1)
11
+ counter ๋์
๋๋ฆฌ๋ ์ต๋ ์ํ๋ฒณ ๊ฐ์๋งํผ์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ฐจ์งํ๋ฏ๋ก O(26)์ผ๋ก ์์์ด๋ค๋ค
12
+ """
13
+
14
+ class Solution :
15
+ def characterReplacement (self , s : str , k : int ) -> int :
16
+ start , end = 0 , 0
17
+ counter = {}
18
+ max_len = 0
19
+ while end < len (s ) :
20
+ counter [s [end ]] = counter .get (s [end ], 0 ) + 1
21
+ while end - start + 1 - max (counter .values ()) > k :
22
+ counter [s [start ]] -= 1
23
+ start += 1
24
+ max_len = max (max_len , end - start + 1 )
25
+ end += 1
26
+ return max_len
Original file line number Diff line number Diff line change
1
+ """
2
+ ํ์ด : mask๋ฅผ int ๋นํธ ์ ๋งํผ ์ด๋์ํค๋ฉด์ 1์ ๊ฐ์ ์ผ๋ค
3
+
4
+ TC : O(1)
5
+
6
+ SC : O(1)
7
+ """
8
+
9
+ class Solution :
10
+ def hammingWeight (self , n : int ) -> int :
11
+ mask = 1 << 31
12
+ cnt = 0
13
+ while mask :
14
+ if mask & n :
15
+ cnt += 1
16
+ mask >>= 1
17
+ return cnt
Original file line number Diff line number Diff line change
1
+ """
2
+ ํ์ด :
3
+ ๋นํธ ์ฐ์ฐ์ xor, and, shift๋ฅผ ์ด์ฉํด ์ํํ๋ค
4
+ a๋ a์ b์ xor์ฐ์ฐ ๊ฒฐ๊ณผ
5
+ -> a์ b์ set-bit๊ฐ ๊ฒน์น์ง ์๋ ์์น์์์ ํฉ์ฐ์ฐ์ ๊ฐ๋ฅํ๊ฒ ํจ
6
+ b๋ a์ b์ and์ฐ์ฐ ๊ฒฐ๊ณผ << 1
7
+ -> a์ b๊ฐ ๋ ๋ค 1์ธ ์์น์์ ํฉ์ ํตํด ์ฌ๋ฆผ์๋ก ์ฌ๋ ค์ฃผ๋ ์ญํ ์ํ
8
+
9
+ ํ์ด์ฌ์์๋ int๊ฐ 32๋นํธ๊ฐ ์๋๋ฏ๋ก 1 32๊ฐ๋ก ์ด๋ฃจ์ด์ง mask๋ฅผ ์ค์ ํด์ฃผ๊ณ
10
+ ์ฌ๋ฆผ์ b๊ฐ 32๋นํธ ๋ฒ์๋ฅผ ๋ฒ์ด๋์ง ์๊ณ ์กด์ฌํ ๋์ while๋ฌธ ์ฐ์ฐ์ ์งํํ๋ค
11
+ ๋ฐ๋ณต๋ฌธ ์งํ ํ 32๋นํธ์ ๋ํด์๋ง return
12
+
13
+ TC : O(1)
14
+
15
+ SC : O(1)
16
+ """
17
+
18
+ class Solution :
19
+ def getSum (self , a : int , b : int ) -> int :
20
+ mask = 0xFFFFFFFF
21
+ while mask & b :
22
+ a , b = a ^ b , (a & b ) << 1
23
+ return a & mask if b > 0 else a
You canโt perform that action at this time.
0 commit comments