-
-
Notifications
You must be signed in to change notification settings - Fork 194
[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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ce6f875
Solve : Reverse Bits
printjin-gmailcom cdf1427
Solve : Reverse Bits
printjin-gmailcom 5346623
Solve : Reverse Bits
printjin-gmailcom cb66bbf
Solve : Longest Repeating Character Replacement
printjin-gmailcom 2201d91
Solve : Clone Graph
printjin-gmailcom cee05de
Solve ; Clone Graph
printjin-gmailcom e7e5e2b
Solve : Longest Common Subsequence
printjin-gmailcom 15a60ed
Solve : Palindromic Substrings
printjin-gmailcom File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
14
longest-repeating-character-replacement/printjin-gmailcom.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 와...혹시 저 넘버를 다 외우고 계신건가요? 전 단순하게 bit operation으로 자리 옮겨가면서 풀이했는데 대단하십니다; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 우와... 저도 bit 연산을 사용해서 풀이했는데 신기하네요... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
자바로 주로 풀이를 하다 보니 파이썬에서 제공하는 자료형 모듈이 익숙한 편이 아니기는 한데 deque(데크)로 푸신 점이 인상적이었습니다.