From 0d47f05c6d6cfc124f844f6d0a40a6fd5a7731bc Mon Sep 17 00:00:00 2001 From: sun912 Date: Tue, 24 Sep 2024 21:24:00 +0900 Subject: [PATCH 1/3] [WEEK7] reversed linked list solved --- reverse-linked-list/sun912.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 reverse-linked-list/sun912.py diff --git a/reverse-linked-list/sun912.py b/reverse-linked-list/sun912.py new file mode 100644 index 000000000..e28cd18fc --- /dev/null +++ b/reverse-linked-list/sun912.py @@ -0,0 +1,20 @@ +""" + TC: O(n) + SC: O(1) + +""" + +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +class Solution: + def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: + node, prev = head, None + + while node: + next, node.next = node.next, prev + prev, node = node, next + + return prev From 8bac4aadbb050bef3ff858882dc99de87d160884 Mon Sep 17 00:00:00 2001 From: sun912 Date: Tue, 24 Sep 2024 21:38:16 +0900 Subject: [PATCH 2/3] [WEEK7] Longest Substring Without Repeating Characters solution --- .../sun912.py | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 longest-substring-without-repeating-characters/sun912.py diff --git a/longest-substring-without-repeating-characters/sun912.py b/longest-substring-without-repeating-characters/sun912.py new file mode 100644 index 000000000..f89727880 --- /dev/null +++ b/longest-substring-without-repeating-characters/sun912.py @@ -0,0 +1,22 @@ +""" + TC: O(n) + SC: O(n) +""" +class Solution: + def lengthOfLongestSubstring(self, s: str) -> int: + str_list = [] + max_length = 0 + for i in range(len(s)): + if s[i] not in str_list: + str_list.append(s[i]) + else: + if max_length < len(str_list): + max_length = len(str_list) + str_list = str_list[str_list.index(s[i])+1:] + str_list.append(s[i]) + + if max_length < len(str_list): + max_length = len(str_list) + + return max_length + From 075404c140eaf731ffc5d3f1cc241bcfa3f3e4b5 Mon Sep 17 00:00:00 2001 From: sun912 Date: Wed, 25 Sep 2024 22:06:17 +0900 Subject: [PATCH 3/3] [WEEK7] number-of-islands solution --- number-of-islands/sun912.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 number-of-islands/sun912.py diff --git a/number-of-islands/sun912.py b/number-of-islands/sun912.py new file mode 100644 index 000000000..b36fbe34b --- /dev/null +++ b/number-of-islands/sun912.py @@ -0,0 +1,26 @@ +""" + TC: O(m*n) + SC: O(m*n) +""" +class Solution: + def numIslands(self, grid: List[List[str]]) -> int: + def dfs(i, j): + if i < 0 or j < 0 or i >= len(grid) or j >= len(grid[0]) or grid[i][j] == "-1" or grid[i][j] == "0": + return + + grid[i][j] = "-1" + dfs(i+1, j) + dfs(i, j+1) + dfs(i-1, j) + dfs(i, j-1) + + if not grid: + return 0 + + count = 0 + for i in range(len(grid)): + for j in range(len(grid[0])): + if grid[i][j] == "1": + count += 1 + dfs(i, j) + return count