Problems
July 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
Find and replace pattern
We will create a hashmap for each word counting the length of the hashmap at that position. Then we create the same hashmap for our pattern. Then we iterate over each word, match it with the pattern...
ReadJuly 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...
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
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
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
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 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 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
Network delay time
We will use Dijkstra's shortest path algorithm to solve this problem. First we will create an adjacency list from the edge list, where the key will be the source node and value will be a tuple...
Read