diff --git a/clone-graph/yuseok89.py b/clone-graph/yuseok89.py new file mode 100644 index 0000000000..c29a9049fc --- /dev/null +++ b/clone-graph/yuseok89.py @@ -0,0 +1,37 @@ +# TC: O(N) +# SC: O(N) +""" +# Definition for a Node. +class Node: + def __init__(self, val = 0, neighbors = None): + self.val = val + self.neighbors = neighbors if neighbors is not None else [] +""" + +from typing import Optional +class Solution: + def cloneGraph(self, node: Optional['Node']) -> Optional['Node']: + + visited = {} + + def rec(node: Optional['Node']) -> Optional['Node']: + + if not node: + return None + + if node.val in visited: + return visited[node.val] + + return_val = Node(node.val) + visited[node.val] = return_val + + for neighbor in node.neighbors: + cloned = rec(neighbor) + + if cloned: + return_val.neighbors.append(cloned) + + return return_val + + return rec(node) + diff --git a/longest-common-subsequence/yuseok89.py b/longest-common-subsequence/yuseok89.py new file mode 100644 index 0000000000..dbe78d900a --- /dev/null +++ b/longest-common-subsequence/yuseok89.py @@ -0,0 +1,22 @@ +# TC: O(N*M) +# SC: O(M) +class Solution: + def longestCommonSubsequence(self, text1: str, text2: str) -> int: + + n = len(text1) + m = len(text2) + dp = [[0 for _ in range(m + 1)] for _ in range(2)] + + cur, prev = 1, 0 + + for i in range(n): + for j in range(m): + if text1[i] == text2[j]: + dp[cur][j + 1] = dp[prev][j] + 1 + else: + dp[cur][j + 1] = max(dp[prev][j + 1], dp[cur][j]) + + cur, prev = prev, cur + + return dp[prev][m] + diff --git a/longest-repeating-character-replacement/yuseok89.py b/longest-repeating-character-replacement/yuseok89.py new file mode 100644 index 0000000000..77b82b3cad --- /dev/null +++ b/longest-repeating-character-replacement/yuseok89.py @@ -0,0 +1,27 @@ +# TC: O(N) +# SC: O(K) +class Solution: + def characterReplacement(self, s: str, k: int) -> int: + + l = 0 + cnt = defaultdict(int) + m, ans = 0, 0 + + for r in range(len(s)): + c = s[r] + cnt[c] += 1 + + m = max(m, cnt[c]) + + while m + k < r - l + 1: + c = s[l] + l += 1 + cnt[c] -= 1 + + if cnt[c] == m - 1: + m = max(cnt.values()) + + ans = max(ans, r - l + 1); + + return ans + diff --git a/palindromic-substrings/yuseok89.py b/palindromic-substrings/yuseok89.py new file mode 100644 index 0000000000..a5d8ad73d5 --- /dev/null +++ b/palindromic-substrings/yuseok89.py @@ -0,0 +1,31 @@ +# TC: O(N^2) +# SC: O(1) +class Solution: + def countSubstrings(self, s: str) -> int: + + ans = 0 + n = len(s) + + for center_idx in range(n): + + idx = 0 + + while 0 <= center_idx - idx and center_idx + idx < n: + if s[center_idx - idx] == s[center_idx + idx]: + ans = ans + 1 + else: + break + + idx += 1 + + idx = 0 + while 0 <= center_idx - idx and center_idx + idx + 1< n: + if s[center_idx - idx] == s[center_idx + idx + 1]: + ans = ans + 1 + else: + break + + idx += 1 + + return ans + diff --git a/reverse-bits/yuseok89.py b/reverse-bits/yuseok89.py new file mode 100644 index 0000000000..c717168ef0 --- /dev/null +++ b/reverse-bits/yuseok89.py @@ -0,0 +1,13 @@ +# TC: O(K) +# SC: O(1) +class Solution: + def reverseBits(self, n: int) -> int: + ans = 0 + + for _ in range(32): + ans *= 2 + ans += n % 2 + n //= 2 + + return ans +