From f651ab14593ef21379b3d2315ba7d53bbe63169e Mon Sep 17 00:00:00 2001 From: Nari Jeong <92130993+naringst@users.noreply.github.com> Date: Mon, 23 Sep 2024 21:47:48 +0900 Subject: [PATCH 1/7] Reverse Linked List Solution --- reverse-linked-list/naringst.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 reverse-linked-list/naringst.py diff --git a/reverse-linked-list/naringst.py b/reverse-linked-list/naringst.py new file mode 100644 index 00000000..4cde2ab7 --- /dev/null +++ b/reverse-linked-list/naringst.py @@ -0,0 +1,27 @@ + +# Runtime: 39ms, Memory: 17.88MB +# Time complexity: O(len(head)) +# Space complexity: O(len(head)) + + +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next + +class Solution: + def __init__(self): + self.nodes = [] # ListNode 객체를 저장할 배열 + + def reverseList(self, head: Optional[ListNode]) -> List[Optional[ListNode]]: + prev = None + curr = head + + while curr is not None: + nextNode = curr.next + curr.next = prev + prev = curr + curr = nextNode + + return prev From 6c88f0e5ffc53963132502d39658c5ab374a81f5 Mon Sep 17 00:00:00 2001 From: Nari Jeong <92130993+naringst@users.noreply.github.com> Date: Wed, 25 Sep 2024 00:31:23 +0900 Subject: [PATCH 2/7] Longest Substring Without Repeating Characters --- .../naringst.py | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 longest-substring-without-repeating-characters/naringst.py diff --git a/longest-substring-without-repeating-characters/naringst.py b/longest-substring-without-repeating-characters/naringst.py new file mode 100644 index 00000000..53993396 --- /dev/null +++ b/longest-substring-without-repeating-characters/naringst.py @@ -0,0 +1,22 @@ + +# Runtime: 51ms, Memory: 16.83MB +# Time complexity: O(len(s)) +# Space complexity: O(len(s)) + +class Solution: + def lengthOfLongestSubstring(self, s: str) -> int: + stringArr = [] + maxLength = 0 + + for sub in s : + if sub in stringArr : + maxLength = max(maxLength, len(stringArr)) + repeatIdx = stringArr.index(sub) + stringArr = stringArr[repeatIdx+1 :] + + stringArr.append(sub) + + maxLength = max(maxLength, len(stringArr)) + + + return maxLength \ No newline at end of file From 4e22d718af926cc8db897608679bc314c000cb86 Mon Sep 17 00:00:00 2001 From: Nari Jeong <92130993+naringst@users.noreply.github.com> Date: Wed, 25 Sep 2024 00:33:07 +0900 Subject: [PATCH 3/7] fix: Add end line break --- longest-substring-without-repeating-characters/naringst.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/longest-substring-without-repeating-characters/naringst.py b/longest-substring-without-repeating-characters/naringst.py index 53993396..7993c5f8 100644 --- a/longest-substring-without-repeating-characters/naringst.py +++ b/longest-substring-without-repeating-characters/naringst.py @@ -19,4 +19,5 @@ def lengthOfLongestSubstring(self, s: str) -> int: maxLength = max(maxLength, len(stringArr)) - return maxLength \ No newline at end of file + return maxLength + \ No newline at end of file From 64467df629435069391ce8027c0eafb52c31f1d3 Mon Sep 17 00:00:00 2001 From: Nari Jeong <92130993+naringst@users.noreply.github.com> Date: Wed, 25 Sep 2024 00:34:44 +0900 Subject: [PATCH 4/7] fix: Add line break again --- longest-substring-without-repeating-characters/naringst.py | 1 - 1 file changed, 1 deletion(-) diff --git a/longest-substring-without-repeating-characters/naringst.py b/longest-substring-without-repeating-characters/naringst.py index 7993c5f8..cc8d0c58 100644 --- a/longest-substring-without-repeating-characters/naringst.py +++ b/longest-substring-without-repeating-characters/naringst.py @@ -20,4 +20,3 @@ def lengthOfLongestSubstring(self, s: str) -> int: return maxLength - \ No newline at end of file From 8818b5f729eb68d0162e9559a7c59a8cd9e637d7 Mon Sep 17 00:00:00 2001 From: Nari Jeong <92130993+naringst@users.noreply.github.com> Date: Wed, 25 Sep 2024 22:33:43 +0900 Subject: [PATCH 5/7] Number Of Islands Solution --- number-of-islands/naringst.py | 44 +++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 number-of-islands/naringst.py diff --git a/number-of-islands/naringst.py b/number-of-islands/naringst.py new file mode 100644 index 00000000..de744cc9 --- /dev/null +++ b/number-of-islands/naringst.py @@ -0,0 +1,44 @@ +from collections import deque + +# Runtime: 241ms, Memory: 18.94MB +# Time complexity: O(len(n*m)) +# Space complexity: O(len(n*m)) + + +class Solution: + def bfs(self, a,b, grid, visited) : + n = len(grid) + m = len(grid[0]) + + dx = [0, 0, 1, -1] + dy = [1, -1 ,0 ,0] + q = deque() + q.append([a,b]) + visited[a][b] = True + + while q : + x,y = q.popleft() + + for i in range(4) : + nx = x + dx[i] + ny = y + dy[i] + + if (0 <= nx < n and 0 <= ny < m and not visited[nx][ny] and grid[nx][ny] == '1'): + visited[nx][ny] = True + q.append([nx,ny]) + + + + def numIslands(self, grid: List[List[str]]) -> int: + n = len(grid) + m = len(grid[0]) + visited = [[False] * m for _ in range(n)] + answer = 0 + + for i in range(n) : + for j in range(m) : + if grid[i][j] == '1' and not visited[i][j] : + self.bfs(i,j,grid,visited) + answer += 1 + + return answer \ No newline at end of file From c47ebed0b062fefb9b6d9f5d5de859dadd069320 Mon Sep 17 00:00:00 2001 From: Nari Jeong <92130993+naringst@users.noreply.github.com> Date: Wed, 25 Sep 2024 22:34:51 +0900 Subject: [PATCH 6/7] fix: add line break --- number-of-islands/naringst.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/number-of-islands/naringst.py b/number-of-islands/naringst.py index de744cc9..2e64ee69 100644 --- a/number-of-islands/naringst.py +++ b/number-of-islands/naringst.py @@ -41,4 +41,4 @@ def numIslands(self, grid: List[List[str]]) -> int: self.bfs(i,j,grid,visited) answer += 1 - return answer \ No newline at end of file + return answer From cefbb6b0793b03332d7fd9c263f1265b08cae282 Mon Sep 17 00:00:00 2001 From: Nari Jeong <92130993+naringst@users.noreply.github.com> Date: Sun, 29 Sep 2024 17:13:14 +0900 Subject: [PATCH 7/7] Update Time Complexity --- longest-substring-without-repeating-characters/naringst.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/longest-substring-without-repeating-characters/naringst.py b/longest-substring-without-repeating-characters/naringst.py index cc8d0c58..e8af5911 100644 --- a/longest-substring-without-repeating-characters/naringst.py +++ b/longest-substring-without-repeating-characters/naringst.py @@ -1,6 +1,6 @@ # Runtime: 51ms, Memory: 16.83MB -# Time complexity: O(len(s)) +# Time complexity: O(len(s)^2) # Space complexity: O(len(s)) class Solution: