Problems
December 6, 2022
Number of ways to buy pens and pencils
We will loop over the pens
and pencils
and add the start and end points to a list. Then we will sort the list and loop over it. We will keep track of the current color and...
December 6, 2022
Describe the painting
We will loop over the segments and add the start and end points to a list. Then we will sort the list and loop over it. We will keep track of the current color and the current start point. When we...
ReadDecember 5, 2022
One edit distance
We will check if the length of the strings are equal or not. If they are equal, we will check if there is only one character that is different. If they are not equal, we will check if the length...
ReadDecember 5, 2022
Remove interval
We will iterate over each interval, if the interval doesn't overlap with the to be removed interval, we just add it to the output. If start is less than remove start, we take the remove start as end....
ReadDecember 5, 2022
Wiggle sort
We will loop over the array, that 2 elements per iteration, then sort 2 elements once ascending and then descending order and swap them. We will do this until we reach the end of the array. Time...
ReadDecember 5, 2022
Find center of star graph
As it's a start graph, each edge will be connected to the center. So we can just check the first 2 edges and return the node that is connected to both of them.
Time complexity: O(1)
...
December 5, 2022
Find k pairs with smallest sums
We will use a min heap to store the smallest sum. We will add the first element of each array to the heap. Then we will pop the smallest element from the heap and add the next element of the array...
ReadDecember 5, 2022
Minimum score of a path between two cities
We will create an adjacency list for the graph. Then we traverse the graph with BFS and keep track of the minimum score for each node. We will return the minimum score for the destination node....
ReadDecember 5, 2022
Fraction to recurring decimal
We will use a dictionary to store the remainder and the index of the remainder. If the remainder is already in the dictionary, we will add the parenthesis and return the result. Time complexity:...
ReadDecember 5, 2022
Middle of the linked list
We will take two pointers, one will move one step at a time and the other will move two steps at a time. When the second pointer reaches the end of the list, the first pointer will be at the middle...
Read