Problems
December 7, 2022
Range sum of bst
We will traverse the tree with DFS and add the value to the sum if it is in the range.
Time complexity: O(n)
Space complexity: O(n)
December 7, 2022
Design tic tac toe
We will use a hash map to store the number of times a row, column, or diagonal has been filled by a player. We will also use a variable to store the number of moves made. If the number of moves is...
ReadDecember 7, 2022
Number of subarrays having even product
For a subarray to have an even product, some elements must be even. However, we don't know which one (or which ones). It is simpler to work with conditions of a form "all elements must satisfy some...
ReadDecember 7, 2022
Minimum cost to buy apples
We will use Dijkstra's algorithm to find the shortest path from the source to the target. The cost of each edge is the number of apples in that node.
Time complexity: O(nlog(n))
...
December 7, 2022
Maximum size subarray sum equals k
We will use prefix sum to solve this problem. We will store the prefix sum in a hash map. If the prefix sum is equal to k
, then we will update the maximum length. If the prefix sum minus...
December 7, 2022
Words within two edits of dictionary
We will take the brute force approach to solve this problem. For every query, we will go through the dictionary and check if the word is within two edits of the query. Time complexity:...
ReadDecember 7, 2022
Can you eat your favorite candy on your favorite day
Define a prefix sum of candiesCount. If you label all candies starting from 1 and order them by type: [prefix[candyType]+1, prefix[candyType+1]] are all the candies of type candyType. At minimum,...
ReadDecember 6, 2022
largest-bst-subtree
We will use a recursive function to check if the tree is a BST or not. If it is, we will return the size of the tree. If it's not, we will return the maximum size of the left and right subtrees....
ReadDecember 6, 2022
Encode and decode strings
We will use a delimiter to separate the strings. Then we will loop over the strings and add the length of the string and the delimiter to the result. Finally, we will add the string to the result....
ReadDecember 6, 2022
Alert using same key card three or more times in a one hour period
We will loop over the keyName
and time
and add the time to a list. Then we will sort the list and loop over it. We will keep track of the current key name and the current...