Problems
September 2, 2022
Sudoku solver
We will use 3 hashset to store the value of rows, columns and boxes. For rows and columns hashset we will use the row and column number as key. For square hashset, we can use the integer division of...
ReadSeptember 2, 2022
Sum root to leaf numbers
We will run DFS from root to leaf, along in the way, we calculate the number for root to leaf value. Once we reach the leaf, we add it to our result. Finally we return the result after traversal is...
ReadSeptember 1, 2022
4sum
We will sort the array and start from first element i, the take the next element j which is not equal to i, then take the next element as l, and the last element of the list as r, added them, compare...
ReadSeptember 1, 2022
Bitwise and of numbers range
If the right crosses the next 2^n of left where n is the number of bits for left, that means 2^n >= left, then the result is always going to be 0. For example, AND product of 1010
and...
September 1, 2022
Clumsy factorial
If we calculate some values, then we will notice some pattern. Here are some calculations- From this we can create the following method to calculate the clumsy factorial. Time Complexity:...
ReadSeptember 1, 2022
Insertion sort list
We will create a dummy node and attach it at the beginning to make our life easier. Then we take 2 pointer, previous and current, previous will be our head and current will be the next node of...
ReadSeptember 1, 2022
Integer to roman
We will create 2 loopup table, one for ONES and another for FIVES. Then we start from least significant bit, check the value is 4 if we mod it by five, then take the value from FIVES lookup table,...
ReadSeptember 1, 2022
Minimum height trees
We will first create an adjacency list from the edge list. Then we start BFS from the leaf nodes, and whenever we visit a node, we add this to a list for current leaves, and decreasing the node count...
ReadSeptember 1, 2022
Minimum moves to equal array elements
Elevating n-1 elements is essensially same as decreasing 1 element. Thus we must decrease everything to the minimum value to get the result.
Time Complexity: O(n)
Space Complexity:...
September 1, 2022
N-ary tree level order traversal
We will run BFS in each level and append the values in a result list, and return the list when traversal is done.
Time Complexity: O(n)
Space Complexity: O(n)