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...
ReadJuly 29, 2022
Longest repeating character replacement
We will take a sliding window to solve this problem. We will take a left and right pointer, initially both will be at 0-th index. Then we move our right pointer and count the number of occurances,...
ReadJuly 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...
ReadJuly 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...
ReadJuly 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...
ReadJuly 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...
ReadJuly 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...
ReadJuly 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...
ReadJuly 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...
ReadJuly 26, 2022
Alien dictionary
We will first create an adjacency list from our input array. If 2 array has a different first charcter, then word1[0] should be arreared before word2[0]. When the grph is already created, we can then...
Read