diff --git a/longest-repeating-character-replacement/jamiebase.py b/longest-repeating-character-replacement/jamiebase.py new file mode 100644 index 0000000000..631eb6069c --- /dev/null +++ b/longest-repeating-character-replacement/jamiebase.py @@ -0,0 +1,34 @@ +""" +# Approach +1. 구간에서 제일 많은 문자 찾는다 +2. 나머지를 바꾼다 +3. 바꾸는 개수가 k 이하인지 본다 +4. 가능하면 구간 길이를 키운다 + +# Complexity +문자열 s에서 서로 다른 글자의 종류 U, s의 길이 N +- Time complexity: O(N) +- Space complexity: O(U) => 영어 대문자만 나오므로 사실상 O(1) +""" + +from collections import defaultdict + + +class Solution: + def characterReplacement(self, s: str, k: int) -> int: + count = defaultdict(int) + left = 0 + max_freq = 0 + answer = 0 + + for right in range(len(s)): + count[s[right]] += 1 + max_freq = max(max_freq, count[s[right]]) + + while (right - left + 1) - max_freq > k: + count[s[left]] -= 1 + left += 1 + + answer = max(answer, right - left + 1) + + return answer diff --git a/reverse-bits/jamiebase.py b/reverse-bits/jamiebase.py new file mode 100644 index 0000000000..deb72199ea --- /dev/null +++ b/reverse-bits/jamiebase.py @@ -0,0 +1,29 @@ +""" +# Approach +1) 정수를 이진 문자열로 변환한 뒤, +32비트로 맞추고 뒤집어서 다시 정수로 변환한다. + +2) n에서 비트를 하나씩 꺼내서 res에 역순으로 붙인다 + +# Complexity +- Time complexity: O(1) +- Space complexity: O(1) +""" + + +# 1 +class Solution: + def reverseBits(self, n: int) -> int: + b_num = format(n, "b") + b_reversed = b_num.zfill(32)[::-1] + return int(b_reversed, 2) + + +# 2 +class Solution: + def reverseBits(self, n: int) -> int: + res = 0 + for _ in range(32): + res = (res << 1) | (n & 1) + n >>= 1 + return res