-
-
Notifications
You must be signed in to change notification settings - Fork 361
[parkhojeong] WEEK 08 Solutions #2819
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| """ | ||
| # 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 deep_copy(node): | ||
| if node is None: | ||
| return None | ||
|
|
||
| cur = Node(node.val) | ||
| visited[node.val] = cur | ||
|
|
||
| cur.val = node.val | ||
| for neighbor in node.neighbors: | ||
| if neighbor.val in visited: | ||
| cur.neighbors.append(visited[neighbor.val]) | ||
| else: | ||
| cur.neighbors.append(deep_copy(neighbor)) | ||
|
|
||
| return cur | ||
|
|
||
| return deep_copy(node) |
|
Contributor
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. 🏷️ 알고리즘 패턴 분석longest-repeating-character-replacement/parkhojeong.pyclass Solution:
def characterReplacement(self, s: str, k: int) -> int:
freq = [0] * 26
max_len = 0
def idx(ch):
return ord(ch) - ord('A')
left = 0
for i in range(len(s)):
freq[idx(s[i])] += 1
window_size = i - left + 1
size = window_size - max(freq)
while size > k:
freq[idx(s[left])] -= 1
left += 1
window_size -= 1
size = window_size - max(freq)
max_len = max(max_len, window_size)
return max_len
📊 시간/공간 복잡도 분석
피드백: 윈도우의 크기를 늘리며 현재 윈도우의 빈도 중 최다빈도(freq 최대)값을 이용해 필요한 변경 수를 계산한다. 필요 시 왼쪽 경계를 이동한다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| class Solution: | ||
| def characterReplacement(self, s: str, k: int) -> int: | ||
| freq = [0] * 26 | ||
| max_len = 0 | ||
|
|
||
| def idx(ch): | ||
| return ord(ch) - ord('A') | ||
|
|
||
| left = 0 | ||
| for i in range(len(s)): | ||
| freq[idx(s[i])] += 1 | ||
| window_size = i - left + 1 | ||
| size = window_size - max(freq) | ||
|
|
||
| while size > k: | ||
| freq[idx(s[left])] -= 1 | ||
|
|
||
| left += 1 | ||
| window_size -= 1 | ||
| size = window_size - max(freq) | ||
|
|
||
| max_len = max(max_len, window_size) | ||
|
|
||
| return max_len | ||
|
|
|
Contributor
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. 🏷️ 알고리즘 패턴 분석palindromic-substrings/parkhojeong.pyclass Solution:
def countSubstrings(self, s: str) -> int:
s_len = len(s)
success = set([(i, i + 1) for i in range(s_len)])
for r in range(s_len):
for l in range(0, r):
if r - l == 1 and s[l] == s[r]:
success.add((l, r + 1))
continue
if (l + 1, r) in success and s[l] == s[r]:
success.add((l, r + 1))
return len(success)
📊 시간/공간 복잡도 분석
피드백: 각 부분 문자열을 검사하는 대신 현재 로직은 비효율적으로 보이며, 중심 확장 또는 DP를 이용하면 시간복잡도를 개선할 수 있다. 개선 제안: 고려해볼 만한 대안: 중심 확장법으로 각 인덱스를 중심으로 좌우 확장하며 모든 팰린드롬의 개수를 세는 것으로 O(n^2) 시간, O(1) 공간으로 구현 가능.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| class Solution: | ||
| def countSubstrings(self, s: str) -> int: | ||
| s_len = len(s) | ||
| success = set([(i, i + 1) for i in range(s_len)]) | ||
|
|
||
| for r in range(s_len): | ||
| for l in range(0, r): | ||
| if r - l == 1 and s[l] == s[r]: | ||
| success.add((l, r + 1)) | ||
| continue | ||
|
|
||
| if (l + 1, r) in success and s[l] == s[r]: | ||
| success.add((l, r + 1)) | ||
|
|
||
| return len(success) |
|
Contributor
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. 🏷️ 알고리즘 패턴 분석reverse-bits/parkhojeong.pyclass Solution:
def reverseBits(self, n: int) -> int:
m = 0
for i in range(32):
remainder = n % 2
n = n // 2
m += pow(2, 31 - i) * remainder
return m
📊 시간/공간 복잡도 분석
피드백: 비트를 하나씩 뒤집으며 결과에 누적하는 직접 구현이다. 비트 연산으로도 최적화 가능하지만 현재 방식은 직관적이다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| class Solution: | ||
| def reverseBits(self, n: int) -> int: | ||
| m = 0 | ||
| for i in range(32): | ||
| remainder = n % 2 | ||
| n = n // 2 | ||
| m += pow(2, 31 - i) * remainder | ||
|
|
||
| return m |
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.
🏷️ 알고리즘 패턴 분석
clone-graph/parkhojeong.py
📊 시간/공간 복잡도 분석
피드백: 깊은 복사 방식으로 그래프를 순회하며 각 노드를 새 노드로 생성하고 이웃 관계를 재구성한다. 방문 맵을 사용해 중복 방문을 피한다.
개선 제안: 현재 구현이 적절해 보입니다.