-
-
Notifications
You must be signed in to change notification settings - Fork 195
[shinsj4653] Week 08 Solutions #1506
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
3 commits
Select commit
Hold shift + click to select a range
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,50 @@ | ||
""" | ||
[문제풀이] | ||
# Inputs | ||
|
||
# Outputs | ||
|
||
# Constraints | ||
|
||
# Ideas | ||
|
||
[회고] | ||
|
||
""" | ||
|
||
|
||
# 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 | ||
from collections import deque | ||
|
||
|
||
class Solution: | ||
def cloneGraph(self, node: Optional['Node']) -> Optional['Node']: | ||
|
||
if not node: | ||
return | ||
|
||
clone = Node(node.val) | ||
clones = {node: clone} | ||
|
||
q = deque([node]) # 해당 라인 답지 참고 | ||
|
||
while q: | ||
node = q.popleft() | ||
|
||
for nei in node.neighbors: | ||
if nei not in clones: | ||
clones[nei] = Node(nei.val) # 답지 참고 | ||
q.append(nei) | ||
|
||
clones[node].neighbors.append(clones[nei]) # 답지 참고 | ||
|
||
return clone | ||
|
||
|
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,30 @@ | ||
""" | ||
[문제풀이] | ||
# Inputs | ||
|
||
# Outputs | ||
|
||
# Constraints | ||
|
||
# Ideas | ||
|
||
[회고] | ||
|
||
""" | ||
|
||
|
||
class Solution: | ||
def longestCommonSubsequence(self, text1: str, text2: str) -> int: | ||
n, m = len(text1), len(text2) | ||
dp = [[0 for _ in range(m + 1)] for _ in range(n + 1)] | ||
|
||
for i in range(1, n + 1): | ||
for j in range(1, m + 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[n][m] | ||
|
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,27 @@ | ||
""" | ||
[문제풀이] | ||
# Inputs | ||
|
||
# Outputs | ||
|
||
# Constraints | ||
|
||
# Ideas | ||
|
||
[회고] | ||
|
||
""" | ||
class Solution: | ||
def characterReplacement(self, s: str, k: int) -> int: | ||
max_len = 0 | ||
counter = {} | ||
start, end = 0, 0 | ||
while end < len(s): | ||
counter[s[end]] = counter.get(s[end], 0) + 1 | ||
while end - start + 1 - max(counter.values()) > k: | ||
counter[s[start]] -= 1 | ||
start += 1 | ||
max_len = max(end - start + 1, max_len) | ||
end += 1 | ||
return max_len | ||
|
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,74 @@ | ||
""" | ||
[문제풀이] | ||
# Inputs | ||
string s | ||
|
||
# Outputs | ||
the number of palindromic substrings | ||
|
||
# Constraints | ||
1 <= s.length <= 1000 | ||
s consists of lowercase English letters. | ||
|
||
# Ideas | ||
부분 문자열 중 팰린드롬 인거 | ||
순열? | ||
10^3 => 시초 예상 | ||
|
||
코드를 짜보니 O(n^3) 나오긴하는데 우선 정답 | ||
|
||
[회고] | ||
|
||
""" | ||
|
||
|
||
class Solution: | ||
def countSubstrings(self, s: str) -> int: | ||
ret = 0 | ||
|
||
for num in range(1, len(s) + 1): | ||
for i in range(len(s) - num + 1): | ||
ss = s[i:i + num] | ||
if ss == ss[::-1]: | ||
ret += 1 | ||
|
||
return ret | ||
|
||
# 해설보고 스스로 풀이 | ||
|
||
class Solution: | ||
def countSubstrings(self, s: str) -> int: | ||
dp = {} | ||
|
||
for start in range(len(s)): | ||
for end in range(start, -1, -1): | ||
if start == end: | ||
dp[(start, end)] = True | ||
|
||
elif start + 1 == end: | ||
dp[(start, end)] = s[start] == s[end] | ||
|
||
else: | ||
dp[(start, end)] = dp[(start + 1, end - 1)] and s[start] == s[end] | ||
|
||
return dp.values().count(True) | ||
|
||
# 기존 값 재활용하려면 end 부터 세야하는게 이해가 안감 | ||
# -> 다시 풀이 | ||
class Solution: | ||
def countSubstrings(self, s: str) -> int: | ||
dp = {} | ||
|
||
for end in range(len(s)): | ||
for start in range(end, -1, -1): | ||
if start == end: | ||
dp[(start, end)] = True | ||
|
||
elif start + 1 == end: | ||
dp[(start, end)] = s[start] == s[end] | ||
|
||
else: | ||
dp[(start, end)] = dp[(start + 1, end - 1)] and s[start] == s[end] | ||
|
||
return list(dp.values()).count(True) | ||
|
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,42 @@ | ||
""" | ||
[문제풀이] | ||
# Inputs | ||
|
||
# Outputs | ||
|
||
# Constraints | ||
|
||
# Ideas | ||
|
||
[회고] | ||
|
||
""" | ||
|
||
|
||
class Solution: | ||
def reverseBits(self, n: int) -> int: | ||
st = [] | ||
|
||
# while n > 0: | ||
# st.append(n % 2) | ||
# n //= 2 => 32 bit 길이 맞춰야함! | ||
|
||
while len(st) < 32: | ||
print('st.append: ', n % 2) | ||
st.append(n % 2) | ||
n //= 2 | ||
|
||
ret, num = 0, 0 | ||
print("st: ", st) | ||
|
||
# 6 : 110 | ||
# [0 1 1] | ||
|
||
while st: | ||
print('st.pop(): ', st[-1]) | ||
ret += st.pop() * (2 ** num) | ||
num += 1 | ||
|
||
return ret | ||
|
||
|
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.
dp의 값을 가져와 리스트로 변환하는 방법외에 단순하게
count
변수 하나를 for 루프 위에 두고dp[(start, end)] = True
가 될 때마다 1씩 증가시키면 메모리나 시간을 절약해 볼 수 있을 것 같기도 합니다!