Skip to content

[printjin-gmailcom] Week 8 solutions #1487

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions clone-graph/printjin-gmailcom.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

자바로 주로 풀이를 하다 보니 파이썬에서 제공하는 자료형 모듈이 익숙한 편이 아니기는 한데 deque(데크)로 푸신 점이 인상적이었습니다.

Original file line number Diff line number Diff line change
@@ -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]
11 changes: 11 additions & 0 deletions longest-common-subsequence/printjin-gmailcom.py
Original file line number Diff line number Diff line change
@@ -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]
14 changes: 14 additions & 0 deletions longest-repeating-character-replacement/printjin-gmailcom.py
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions palindromic-substrings/printjin-gmailcom.py
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions reverse-bits/printjin-gmailcom.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

와...혹시 저 넘버를 다 외우고 계신건가요? 전 단순하게 bit operation으로 자리 옮겨가면서 풀이했는데 대단하십니다;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

우와... 저도 bit 연산을 사용해서 풀이했는데 신기하네요...

Original file line number Diff line number Diff line change
@@ -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