diff --git a/longest-substring-without-repeating-characters/naringst.py b/longest-substring-without-repeating-characters/naringst.py new file mode 100644 index 000000000..e8af59110 --- /dev/null +++ b/longest-substring-without-repeating-characters/naringst.py @@ -0,0 +1,22 @@ + +# Runtime: 51ms, Memory: 16.83MB +# Time complexity: O(len(s)^2) +# 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 diff --git a/number-of-islands/naringst.py b/number-of-islands/naringst.py new file mode 100644 index 000000000..2e64ee693 --- /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 diff --git a/reverse-linked-list/naringst.py b/reverse-linked-list/naringst.py new file mode 100644 index 000000000..4cde2ab75 --- /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