diff --git a/clone-graph/printjin-gmailcom.py b/clone-graph/printjin-gmailcom.py new file mode 100644 index 000000000..18daa2822 --- /dev/null +++ b/clone-graph/printjin-gmailcom.py @@ -0,0 +1,17 @@ +from collections import deque +from typing import Optional + +class Solution: + def cloneGraph(self, node: Optional['Node']) -> Optional['Node']: + if not node: + return None + old_to_new = {node: Node(node.val)} + queue = deque([node]) + while queue: + current = queue.popleft() + for neighbor in current.neighbors: + if neighbor not in old_to_new: + old_to_new[neighbor] = Node(neighbor.val) + queue.append(neighbor) + old_to_new[current].neighbors.append(old_to_new[neighbor]) + return old_to_new[node] diff --git a/longest-common-subsequence/printjin-gmailcom.py b/longest-common-subsequence/printjin-gmailcom.py new file mode 100644 index 000000000..bb245f177 --- /dev/null +++ b/longest-common-subsequence/printjin-gmailcom.py @@ -0,0 +1,11 @@ +class Solution: + def longestCommonSubsequence(self, text1, text2): + m, n = len(text1), len(text2) + dp = [[0] * (n + 1) for _ in range(m + 1)] + for i in range(1, m + 1): + for j in range(1, n + 1): + if text1[i - 1] == text2[j - 1]: + dp[i][j] = dp[i - 1][j - 1] + 1 + else: + dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) + return dp[m][n] diff --git a/longest-repeating-character-replacement/printjin-gmailcom.py b/longest-repeating-character-replacement/printjin-gmailcom.py new file mode 100644 index 000000000..f7a7207b0 --- /dev/null +++ b/longest-repeating-character-replacement/printjin-gmailcom.py @@ -0,0 +1,14 @@ +class Solution: + def characterReplacement(self, s, k): + count = [0] * 26 + left = 0 + max_count = 0 + res = 0 + for right in range(len(s)): + count[ord(s[right]) - ord('A')] += 1 + max_count = max(max_count, count[ord(s[right]) - ord('A')]) + while (right - left + 1) - max_count > k: + count[ord(s[left]) - ord('A')] -= 1 + left += 1 + res = max(res, right - left + 1) + return res diff --git a/palindromic-substrings/printjin-gmailcom.py b/palindromic-substrings/printjin-gmailcom.py new file mode 100644 index 000000000..93a09da18 --- /dev/null +++ b/palindromic-substrings/printjin-gmailcom.py @@ -0,0 +1,15 @@ +class Solution: + def countSubstrings(self, s): + count = 0 + for center in range(len(s)): + left, right = center, center + while left >= 0 and right < len(s) and s[left] == s[right]: + count += 1 + left -= 1 + right += 1 + left, right = center, center + 1 + while left >= 0 and right < len(s) and s[left] == s[right]: + count += 1 + left -= 1 + right += 1 + return count diff --git a/reverse-bits/printjin-gmailcom.py b/reverse-bits/printjin-gmailcom.py new file mode 100644 index 000000000..97888f1d4 --- /dev/null +++ b/reverse-bits/printjin-gmailcom.py @@ -0,0 +1,8 @@ +class Solution: + def reverseBits(self, n): + n = (n >> 16) | (n << 16) + n = ((n & 0xff00ff00) >> 8) | ((n & 0x00ff00ff) << 8) + n = ((n & 0xf0f0f0f0) >> 4) | ((n & 0x0f0f0f0f) << 4) + n = ((n & 0xcccccccc) >> 2) | ((n & 0x33333333) << 2) + n = ((n & 0xaaaaaaaa) >> 1) | ((n & 0x55555555) << 1) + return n