File tree Expand file tree Collapse file tree
longest-repeating-character-replacement Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ """
2+ # Approach
3+ 1. ๊ตฌ๊ฐ์์ ์ ์ผ ๋ง์ ๋ฌธ์ ์ฐพ๋๋ค
4+ 2. ๋๋จธ์ง๋ฅผ ๋ฐ๊พผ๋ค
5+ 3. ๋ฐ๊พธ๋ ๊ฐ์๊ฐ k ์ดํ์ธ์ง ๋ณธ๋ค
6+ 4. ๊ฐ๋ฅํ๋ฉด ๊ตฌ๊ฐ ๊ธธ์ด๋ฅผ ํค์ด๋ค
7+
8+ # Complexity
9+ ๋ฌธ์์ด s์์ ์๋ก ๋ค๋ฅธ ๊ธ์์ ์ข
๋ฅ U, s์ ๊ธธ์ด N
10+ - Time complexity: O(N)
11+ - Space complexity: O(U) => ์์ด ๋๋ฌธ์๋ง ๋์ค๋ฏ๋ก ์ฌ์ค์ O(1)
12+ """
13+
14+ from collections import defaultdict
15+
16+
17+ class Solution :
18+ def characterReplacement (self , s : str , k : int ) -> int :
19+ count = defaultdict (int )
20+ left = 0
21+ max_freq = 0
22+ answer = 0
23+
24+ for right in range (len (s )):
25+ count [s [right ]] += 1
26+ max_freq = max (max_freq , count [s [right ]])
27+
28+ while (right - left + 1 ) - max_freq > k :
29+ count [s [left ]] -= 1
30+ left += 1
31+
32+ answer = max (answer , right - left + 1 )
33+
34+ return answer
Original file line number Diff line number Diff line change 1+ """
2+ # Approach
3+ 1) ์ ์๋ฅผ ์ด์ง ๋ฌธ์์ด๋ก ๋ณํํ ๋ค,
4+ 32๋นํธ๋ก ๋ง์ถ๊ณ ๋ค์ง์ด์ ๋ค์ ์ ์๋ก ๋ณํํ๋ค.
5+
6+ 2) n์์ ๋นํธ๋ฅผ ํ๋์ฉ ๊บผ๋ด์ res์ ์ญ์์ผ๋ก ๋ถ์ธ๋ค
7+
8+ # Complexity
9+ - Time complexity: O(1)
10+ - Space complexity: O(1)
11+ """
12+
13+
14+ # 1
15+ class Solution :
16+ def reverseBits (self , n : int ) -> int :
17+ b_num = format (n , "b" )
18+ b_reversed = b_num .zfill (32 )[::- 1 ]
19+ return int (b_reversed , 2 )
20+
21+
22+ # 2
23+ class Solution :
24+ def reverseBits (self , n : int ) -> int :
25+ res = 0
26+ for _ in range (32 ):
27+ res = (res << 1 ) | (n & 1 )
28+ n >>= 1
29+ return res
You canโt perform that action at this time.
0 commit comments