diff --git a/clone-graph/kayden.py b/clone-graph/kayden.py new file mode 100644 index 000000000..500897ca4 --- /dev/null +++ b/clone-graph/kayden.py @@ -0,0 +1,28 @@ +from typing import Optional +from collections import deque + + +class Solution: + # 시간복잡도: O(N) node 개수: N + # 공간복잡도: O(N) + def cloneGraph(self, node: Optional['Node']) -> Optional['Node']: + if node: + visited = {} + copy = Node(val=node.val) + visited[copy.val] = copy + q = deque() + q.append((copy, node)) + + while q: + cur, node = q.popleft() + + for idx, next_node in enumerate(node.neighbors): + if next_node.val not in visited: + new = Node(val=next_node.val) + visited[new.val] = new + q.append((new, next_node)) + cur.neighbors.append(visited[next_node.val]) + + return copy + + return node diff --git a/longest-common-subsequence/kayden.py b/longest-common-subsequence/kayden.py index d43456d11..c35ac3633 100644 --- a/longest-common-subsequence/kayden.py +++ b/longest-common-subsequence/kayden.py @@ -1,17 +1,33 @@ -# 시간복잡도: O(N) -# 공간복잡도: O(N) class Solution: - def longestConsecutive(self, nums: List[int]) -> int: - nums = set(nums) - answer = 0 + # 시간복잡도: O(A*B) + # 공간복잡도: O(A*B) + def longestCommonSubsequence(self, text1: str, text2: str) -> int: + a = len(text1) + b = len(text2) - for num in nums: - if num - 1 not in nums: - length = 1 + lcs = [[0]*(b+1) for _ in range(a+1)] - while num + length in nums: - length += 1 + for i in range(1, a+1): + for j in range(1, b+1): + if text1[i-1] == text2[j-1]: + lcs[i][j] = lcs[i-1][j-1] + 1 + lcs[i][j] = max(lcs[i][j], lcs[i-1][j], lcs[i][j-1]) - answer = max(answer, length) + return lcs[a][b] - return answer + # 시간복잡도: O(A*B) + # 공간복잡도: O(A) + def longestCommonSubsequence2(self, text1: str, text2: str) -> int: + n = len(text1) + lcs = [0]*n + longest = 0 + for ch in text2: + cur = 0 + for i in range(n): + if cur < lcs[i]: + cur = lcs[i] + elif ch == text1[i]: + lcs[i] = cur + 1 + longest = max(longest, cur+1) + + return longest diff --git a/longest-consecutive-sequence/kayden.py b/longest-consecutive-sequence/kayden.py new file mode 100644 index 000000000..d43456d11 --- /dev/null +++ b/longest-consecutive-sequence/kayden.py @@ -0,0 +1,17 @@ +# 시간복잡도: O(N) +# 공간복잡도: O(N) +class Solution: + def longestConsecutive(self, nums: List[int]) -> int: + nums = set(nums) + answer = 0 + + for num in nums: + if num - 1 not in nums: + length = 1 + + while num + length in nums: + length += 1 + + answer = max(answer, length) + + return answer diff --git a/longest-repeating-character-replacement/kayden.py b/longest-repeating-character-replacement/kayden.py new file mode 100644 index 000000000..e7cb2397d --- /dev/null +++ b/longest-repeating-character-replacement/kayden.py @@ -0,0 +1,23 @@ +from collections import defaultdict + +class Solution: + # 시간복잡도: O(N) + # 공간복잡도: O(N) + def characterReplacement(self, s: str, k: int) -> int: + + n = len(s) + l, r = 0, 1 + + d = defaultdict(int) + d[s[l]] += 1 + + longest = 1 + while l < r and r < n: + d[s[r]] += 1 + if r-l+1 - max(d.values()) > k: + d[s[l]] -= 1 + l += 1 + longest = max(longest, r-l+1) + r += 1 + + return longest diff --git a/merge-two-sorted-lists/kayden.py b/merge-two-sorted-lists/kayden.py new file mode 100644 index 000000000..ea5ce4b1c --- /dev/null +++ b/merge-two-sorted-lists/kayden.py @@ -0,0 +1,23 @@ +class Solution: + # 시간복잡도: O(N+M) list1:N, list2: M + # 공간복잡도: O(N+M) + def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]: + merged = ListNode() + cur = merged + + while list1 and list2: + if list1.val < list2.val: + cur.next = list1 + list1 = list1.next + else: + cur.next = list2 + list2 = list2.next + temp = temp.next + + if list1: + cur.next = list1 + + if list2: + cur.next = list2 + + return merged.next diff --git a/sum-of-two-integers/kayden.java b/sum-of-two-integers/kayden.java new file mode 100644 index 000000000..adc1af9a2 --- /dev/null +++ b/sum-of-two-integers/kayden.java @@ -0,0 +1,12 @@ +class Solution { + // time: O(1) + // space: O(1) + public int getSum(int a, int b) { + while (b != 0) { + int tmp = (a & b) << 1; + a = a ^ b; + b = tmp; + } + return a; + } +}