Copy List with Random Pointer

Intuition Traverse the list, saving the visited one to avoid cyclic loop. Approach A recursive function that Check if that node was visited, if it was, return visited. Create new node Saved new node in the visited (visited[curr] = new_node) Recursevily call the function to compute next and random Return the new_node Complexity Time complexity: O(n) Space complexity: O(n) Code """ # Definition for a Node. class Node: def __init__(self, x, next=None, random=None): self.val = int(x) self.next = next self.random = random """ class Solution(object): visited = {} def copyRandomList(self, head): """ :type head: Node :rtype: Node """ if not head: return None if head in self.visited: return self.visited[head] new_node = Node(head.val, None, None) self.visited[head] = new_node new_node.next = self.copyRandomList(head.next) new_node.random = self.copyRandomList(head.random) return new_node

April 16, 2025 · 1 min

H-Index

Intuition My first thought was to sort the array, and return the first element where count < citations[i]. However this proved to be wrong, since this would return the first element, and after sorting the array we want to return the last element where count < citations[i] is True. Approach Sort the array, then iterate over it storing the count where count < citations[i]. Complexity Time complexity: O(n log n) since we have the sort function ...

April 16, 2025 · 1 min

Integer to Roman

Intuition Create a map with the integer and the roman value of that integer, loop through the reversed array, treating the special cases (4 and 9) Approach Conver the number to a string then reverse it. Once reversed, create a base variable to store whether the digit is 1s, 10s, 100s, 1000s. Loop through each digit (d) getting the equivalent in roman: if d > 9 or d>4, return map[1*base] + map[(d+1)*base] if d > 5, store map[5*base], decrease d while d > 0 concat map[1*base] return stored roman Complexity Time complexity: O(n) ...

April 16, 2025 · 1 min

Linked List Cycle

Intuition Do a hash on the value itself, but this has a flaw that different nodes can have the same value, therefore not forming a cycle. A optiomal approach is to use the slow-fast approach. One pointer jumping one node at a time, the other jumping two. If the faster points reaches the end (node.next == null), then we don’t have a cycle. When both slow and fast meets each other, we found our cycle. ...

April 16, 2025 · 2 min

Reverse Words in a String

Intuition Split words into a array, build new string using .pop() Approach First split the text by (spaces), then pop the last element, strip whitespaces and concatenete at the end of the new string. Complexity Time complexity: O(n) Space complexity: O(n) Code class Solution(object): def reverseWords(self, s): """ :type s: str :rtype: str """ words = s.strip().split(' ') reversed = '' while len(words) > 0: w = words.pop().strip() if len(w) == 0: continue reversed += w + (' ' if len(words) > 0 else '') return reversed

April 16, 2025 · 1 min