From 4f4ad891e8058f48b79ed03d0a9872415b2a8adb Mon Sep 17 00:00:00 2001 From: Jinbeom Date: Mon, 23 Sep 2024 14:41:50 +0900 Subject: [PATCH 1/7] Reverse Linked List Solution --- reverse-linked-list/kayden.java | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 reverse-linked-list/kayden.java diff --git a/reverse-linked-list/kayden.java b/reverse-linked-list/kayden.java new file mode 100644 index 000000000..49a1e050a --- /dev/null +++ b/reverse-linked-list/kayden.java @@ -0,0 +1,26 @@ +/** +* Definition for singly-linked list. +* public class ListNode { +* int val; +* ListNode next; +* ListNode() {} +* ListNode(int val) { this.val = val; } +* ListNode(int val, ListNode next) { this.val = val; this.next = next; } +* } +*/ +// 시간복잡도: O(N) +// 공간복잡도: O(1) +class Solution { + public ListNode reverseList(ListNode head) { + + ListNode prev = null; + + while (head != null){ + ListNode curr = new ListNode(head.val, prev); + prev = curr; + head = head.next; + } + + return prev; + } +} From da4df4ffe7ba62650972f45ef876a1256f57662b Mon Sep 17 00:00:00 2001 From: Jinbeom Date: Mon, 23 Sep 2024 15:26:59 +0900 Subject: [PATCH 2/7] Longest Substring Without Repeating Characters Solution --- .../kayden.py | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 longest-substring-without-repeating-characters/kayden.py diff --git a/longest-substring-without-repeating-characters/kayden.py b/longest-substring-without-repeating-characters/kayden.py new file mode 100644 index 000000000..683a3b5fa --- /dev/null +++ b/longest-substring-without-repeating-characters/kayden.py @@ -0,0 +1,20 @@ +class Solution: + # 시간복잡도: O(N) + # 공간복잡도: O(N) + # set에서 제외하는 로직을 없애기 위해, map을 사용해서 idx값을 저장후, start와 비교해서 start보다 작은 idx를 가진 경우에는 중복이 아니라고 판단했습니다. + def lengthOfLongestSubstring(self, s: str) -> int: + + last_idx = {} + answer = 0 + start = 0 + + for idx, ch in enumerate(s): + # 중복 조회시 idx값과 start 값 비교를 통해, 삭제하는 로직을 없이 중복을 확인했습니다. + if ch in last_idx and last_idx[ch] >= start: + start = last_idx[ch] + 1 + last_idx[ch] = idx + else: + answer = max(answer, idx - start + 1) + last_idx[ch] = idx + + return answer From 9a4fe5e84142b23565e4e56e630deda519309a91 Mon Sep 17 00:00:00 2001 From: Jinbeom Date: Mon, 23 Sep 2024 15:38:07 +0900 Subject: [PATCH 3/7] Refactor Reverse Linked List --- reverse-linked-list/kayden.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/reverse-linked-list/kayden.java b/reverse-linked-list/kayden.java index 49a1e050a..5c7c652d6 100644 --- a/reverse-linked-list/kayden.java +++ b/reverse-linked-list/kayden.java @@ -12,13 +12,13 @@ // 공간복잡도: O(1) class Solution { public ListNode reverseList(ListNode head) { - ListNode prev = null; while (head != null){ - ListNode curr = new ListNode(head.val, prev); - prev = curr; - head = head.next; + ListNode next = head.next; + head.next = prev; + prev = head; + head = next; } return prev; From 579e4937ae9c78af92a7663f06677a3ff6aab7e8 Mon Sep 17 00:00:00 2001 From: Jinbeom Date: Thu, 26 Sep 2024 03:51:46 +0900 Subject: [PATCH 4/7] Number of Islands Solution --- number-of-islands/kayden.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 number-of-islands/kayden.py diff --git a/number-of-islands/kayden.py b/number-of-islands/kayden.py new file mode 100644 index 000000000..c18705a32 --- /dev/null +++ b/number-of-islands/kayden.py @@ -0,0 +1,36 @@ +# 시간복잡도: O(M*N) +# 공간복잡도: O(M*N) + +from collections import deque + + +class Solution: + def numIslands(self, grid: List[List[str]]) -> int: + dx = [0, 0, -1, 1] + dy = [-1, 1, 0, 0] + m = len(grid) + n = len(grid[0]) + q = deque() + + def bfs(a, b): + q.append((a, b)) + while q: + x, y = q.popleft() + + for i in range(4): + nx = x + dx[i] + ny = y + dy[i] + if not (0 <= nx < m and 0 <= ny < n): continue + + if grid[nx][ny] == '1': + grid[nx][ny] = '0' + q.append((nx, ny)) + + count = 0 + for i in range(m): + for j in range(n): + if grid[i][j] == '1': + count += 1 + bfs(i, j) + + return count From a647a2bbd98efe1b154e48c4699af120073c2ebf Mon Sep 17 00:00:00 2001 From: Jinbeom Date: Thu, 26 Sep 2024 04:12:39 +0900 Subject: [PATCH 5/7] Unique Paths Solution --- unique-paths/kayden.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 unique-paths/kayden.py diff --git a/unique-paths/kayden.py b/unique-paths/kayden.py new file mode 100644 index 000000000..92eb64d70 --- /dev/null +++ b/unique-paths/kayden.py @@ -0,0 +1,17 @@ +from math import comb +class Solution: + # 시간복잡도: O(m+n) + # 공간복잡도: O(1) + def uniquePaths(self, m: int, n: int) -> int: + return comb(m+n-2, n-1) # m+n-2Cn-1 + + # 시간복잡도: O(m*n) + # 공간복잡도: O(n) + def uniquePaths2(self, m: int, n: int) -> int: + dp = [1] * n + + for _ in range(1, m): + for j in range(1, n): + dp[j] = dp[j-1] + dp[j] + + return dp[n-1] From 74bfb9f727fe203aa87f0481d46397e49c07dbdb Mon Sep 17 00:00:00 2001 From: Jinbeom Date: Thu, 26 Sep 2024 04:15:07 +0900 Subject: [PATCH 6/7] Set Matrix Zeroes Solution --- set-matrix-zeroes/kayden.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 set-matrix-zeroes/kayden.py diff --git a/set-matrix-zeroes/kayden.py b/set-matrix-zeroes/kayden.py new file mode 100644 index 000000000..2f5425880 --- /dev/null +++ b/set-matrix-zeroes/kayden.py @@ -0,0 +1,23 @@ +# 시간복잡도: O(m*n) +# 공간복잡도: O(m+n) +class Solution: + def setZeroes(self, matrix: List[List[int]]) -> None: + m = len(matrix) + n = len(matrix[0]) + + rows = set() + cols = set() + + for i in range(m): + for j in range(n): + if matrix[i][j] == 0: + rows.add(i) + cols.add(j) + + for row in rows: + for j in range(n): + matrix[row][j] = 0 + + for col in cols: + for i in range(m): + matrix[i][col] = 0 \ No newline at end of file From 00458c7f5abd85b52ec870bf1d200bba782c2288 Mon Sep 17 00:00:00 2001 From: Jinbeom Date: Thu, 26 Sep 2024 04:16:06 +0900 Subject: [PATCH 7/7] Fix Set Matrix Zeroes --- set-matrix-zeroes/kayden.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/set-matrix-zeroes/kayden.py b/set-matrix-zeroes/kayden.py index 2f5425880..72aa68460 100644 --- a/set-matrix-zeroes/kayden.py +++ b/set-matrix-zeroes/kayden.py @@ -20,4 +20,4 @@ def setZeroes(self, matrix: List[List[int]]) -> None: for col in cols: for i in range(m): - matrix[i][col] = 0 \ No newline at end of file + matrix[i][col] = 0