diff --git a/longest-substring-without-repeating-characters/dolphinflow86.py b/longest-substring-without-repeating-characters/dolphinflow86.py new file mode 100644 index 0000000000..7d0f78f68f --- /dev/null +++ b/longest-substring-without-repeating-characters/dolphinflow86.py @@ -0,0 +1,20 @@ +# N is the length of s. +# TC: O(N) - each character is added and removed at most once +# SC: O(N) - stores the characters in the current window +class Solution: + + def lengthOfLongestSubstring(self, s: str) -> int: + chars = set() + left = 0 + longest = 0 + + for right, char in enumerate(s): + while char in chars: + chars.remove(s[left]) + left += 1 + + chars.add(char) + longest = max(longest, right - left + 1) + + return longest + diff --git a/number-of-islands/dolphinflow86.py b/number-of-islands/dolphinflow86.py new file mode 100644 index 0000000000..0a9760a273 --- /dev/null +++ b/number-of-islands/dolphinflow86.py @@ -0,0 +1,36 @@ +# R is the number of rows, and C is the number of columns. +# TC: O(R * C) - visits each cell at most once +# SC: O(R * C) - uses the recursion stack in the worst case +class Solution: + + def numIslands(self, grid: List[List[str]]) -> int: + rows = len(grid) + cols = len(grid[0]) + + def dfs(row, col): + if ( + row < 0 + or row >= rows + or col < 0 + or col >= cols + or grid[row][col] != "1" + ): + return + + grid[row][col] = "0" + + dfs(row - 1, col) + dfs(row + 1, col) + dfs(row, col - 1) + dfs(row, col + 1) + + islands = 0 + + for row in range(rows): + for col in range(cols): + if grid[row][col] == "1": + islands += 1 + dfs(row, col) + + return islands + diff --git a/reverse-linked-list/dolphinflow86.py b/reverse-linked-list/dolphinflow86.py new file mode 100644 index 0000000000..905079f98a --- /dev/null +++ b/reverse-linked-list/dolphinflow86.py @@ -0,0 +1,16 @@ +# TC: O(N) - visits each node exactly once +# SC: O(1) - reverses the links in place +class Solution: + + def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: + prev = None + current = head + + while current: + next_node = current.next + current.next = prev + prev = current + current = next_node + + return prev +