Problems


July 29, 2022

Longest consecutive sequence

We will first create a set with the input numbers, so we can look up in the set with constant time. Then we iterate over the numbers, check if the current number has any predecisior, if not that...

Read

July 29, 2022

Search suggestions system

First we will sort the products list. We will go through each character, create a search substring and then check if the product from the products list starts with the search substring. If yes, we...

Read

July 29, 2022

Generate parentheses

We will solve it with backtracking. We will add one open parentheses, then call the recusive backtracking method. If the number of opening parentheses and closing parentheses are equal to input, that...

Read

July 28, 2022

Min cost to connect all points

We will first create an adjacency list from the given points, in the adjacency list we will also keep the manhattan distance that is mentioned in the problem. After creating the adjacency list, it's...

Read

July 28, 2022

Palindrome partitioning

We will solve it through backtracking. We will check every possible substring, if it is palindrome, then we add it to the partition, when we are at the end of the string, we add it to the result. We...

Read

July 28, 2022

Swim in rising water

We will apply Dijkstra's greedy algorithm to solve this problem. It's basically a BFS traversal, but rather than use a regular queue, we will use a minimum heap aka. priority queue to pop from the...

Read

July 28, 2022

Happy number

We will calculate the next number, and memoize it. If it's already in the memo, that means we have a cycle, then we return false. Otherwise the number will end up on to be 1. Then we break the loop...

Read

July 28, 2022

reconstruct-itinerary

Fisrt we will sort our input tickets, so that when we create the adjacency list from it, it will already be sorted. Then we run DFS with backtrack, as it's possile that we go though one edge, but...

Read

July 27, 2022

Flatten binary tree to linked list

We will recursive move all the preorder traversal nodes to inorder. We keep track of previous node in very recursive call, move the right right subtree to a temporary variable, flatten the left...

Read

July 26, 2022

Cheapest flights within k stops

We will solve the problem using Bellman-Ford algorithm. Initially we will assign infinity prices to all nodes except the source nodes price will be 0. We will basically run the BFS for k+1 steps as...

Read
... 83 84 85 86 87 ...