From 7d732f22ef777112add91ce16a533a0e108edbe3 Mon Sep 17 00:00:00 2001 From: SEONG JUN SHIN Date: Wed, 21 May 2025 20:40:57 +0900 Subject: [PATCH 1/3] feat: add week 08 problems --- clone-graph/shinsj4653.py | 14 ++++++++++++++ longest-common-subsequence/shinsj4653.py | 15 +++++++++++++++ .../shinsj4653.py | 14 ++++++++++++++ palindromic-substrings/shinsj4653.py | 15 +++++++++++++++ reverse-bits/shinsj4653.py | 15 +++++++++++++++ 5 files changed, 73 insertions(+) create mode 100644 clone-graph/shinsj4653.py create mode 100644 longest-common-subsequence/shinsj4653.py create mode 100644 longest-repeating-character-replacement/shinsj4653.py create mode 100644 palindromic-substrings/shinsj4653.py create mode 100644 reverse-bits/shinsj4653.py diff --git a/clone-graph/shinsj4653.py b/clone-graph/shinsj4653.py new file mode 100644 index 000000000..4f309ff5d --- /dev/null +++ b/clone-graph/shinsj4653.py @@ -0,0 +1,14 @@ +""" +[문제풀이] +# Inputs + +# Outputs + +# Constraints + +# Ideas + +[회고] + +""" + diff --git a/longest-common-subsequence/shinsj4653.py b/longest-common-subsequence/shinsj4653.py new file mode 100644 index 000000000..f2dd9f373 --- /dev/null +++ b/longest-common-subsequence/shinsj4653.py @@ -0,0 +1,15 @@ +""" +[문제풀이] +# Inputs + +# Outputs + +# Constraints + +# Ideas + +[회고] + +""" + + diff --git a/longest-repeating-character-replacement/shinsj4653.py b/longest-repeating-character-replacement/shinsj4653.py new file mode 100644 index 000000000..4f309ff5d --- /dev/null +++ b/longest-repeating-character-replacement/shinsj4653.py @@ -0,0 +1,14 @@ +""" +[문제풀이] +# Inputs + +# Outputs + +# Constraints + +# Ideas + +[회고] + +""" + diff --git a/palindromic-substrings/shinsj4653.py b/palindromic-substrings/shinsj4653.py new file mode 100644 index 000000000..f2dd9f373 --- /dev/null +++ b/palindromic-substrings/shinsj4653.py @@ -0,0 +1,15 @@ +""" +[문제풀이] +# Inputs + +# Outputs + +# Constraints + +# Ideas + +[회고] + +""" + + diff --git a/reverse-bits/shinsj4653.py b/reverse-bits/shinsj4653.py new file mode 100644 index 000000000..f2dd9f373 --- /dev/null +++ b/reverse-bits/shinsj4653.py @@ -0,0 +1,15 @@ +""" +[문제풀이] +# Inputs + +# Outputs + +# Constraints + +# Ideas + +[회고] + +""" + + From 17c7d2e1bbce7b2331c69c8dd754efa4a8700d7b Mon Sep 17 00:00:00 2001 From: SEONG JUN SHIN Date: Fri, 23 May 2025 19:11:08 +0900 Subject: [PATCH 2/3] feat: add solutions --- clone-graph/shinsj4653.py | 36 +++++++++++++++++++ .../shinsj4653.py | 13 +++++++ reverse-bits/shinsj4653.py | 27 ++++++++++++++ 3 files changed, 76 insertions(+) diff --git a/clone-graph/shinsj4653.py b/clone-graph/shinsj4653.py index 4f309ff5d..2d1db3534 100644 --- a/clone-graph/shinsj4653.py +++ b/clone-graph/shinsj4653.py @@ -12,3 +12,39 @@ """ + +# 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 + + diff --git a/longest-repeating-character-replacement/shinsj4653.py b/longest-repeating-character-replacement/shinsj4653.py index 4f309ff5d..36417733d 100644 --- a/longest-repeating-character-replacement/shinsj4653.py +++ b/longest-repeating-character-replacement/shinsj4653.py @@ -11,4 +11,17 @@ [회고] """ +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 diff --git a/reverse-bits/shinsj4653.py b/reverse-bits/shinsj4653.py index f2dd9f373..6557e998e 100644 --- a/reverse-bits/shinsj4653.py +++ b/reverse-bits/shinsj4653.py @@ -13,3 +13,30 @@ """ +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 + + From 25448182ab5954b38bcea7b3871a42872b252c06 Mon Sep 17 00:00:00 2001 From: SEONG JUN SHIN Date: Fri, 23 May 2025 21:47:44 +0900 Subject: [PATCH 3/3] feat: add palindromic-substrings, lcs solutions --- longest-common-subsequence/shinsj4653.py | 15 ++++++ palindromic-substrings/shinsj4653.py | 59 ++++++++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/longest-common-subsequence/shinsj4653.py b/longest-common-subsequence/shinsj4653.py index f2dd9f373..b338661f1 100644 --- a/longest-common-subsequence/shinsj4653.py +++ b/longest-common-subsequence/shinsj4653.py @@ -13,3 +13,18 @@ """ +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] + diff --git a/palindromic-substrings/shinsj4653.py b/palindromic-substrings/shinsj4653.py index f2dd9f373..a4df15ad2 100644 --- a/palindromic-substrings/shinsj4653.py +++ b/palindromic-substrings/shinsj4653.py @@ -1,15 +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) +