Problems
December 19, 2022
Binary tree longest consecutive sequence
We will use a recursive function to find the longest consecutive sequence in the left subtree and the right subtree. Then we will compare the longest consecutive sequence in the left subtree and the...
ReadDecember 18, 2022
Day of the year
We can use the datetime
module to convert the date to a datetime object and then use the timetuple
method to get the day of the year.
Time complexity: O(1)
...
December 18, 2022
Check if there is a valid parentheses string path
We will use top-down memoization to solve the problem. For every opening parenthesis we add 1 to our current and for every closing parenthesis we subtract 1 from our current. If we ever reach a...
ReadDecember 18, 2022
Pancake sorting
We will iterate through the array, and for each element, we will find the index of the element in the array. Then we will reverse the array from the index of the element to the end of the array. Then...
ReadDecember 18, 2022
Powerful integers
We can use brute force to calculate all the possible values of x^i + y^j
until the value is less than the bound and store them in a set. Then we can return the set as a list.
Time...
December 17, 2022
Root equals sum of children
We will check the value of root with the sum of the rest of the tree. If the value of root is equal to the sum of the rest of the tree, then we will return True
. Otherwise, we will...
December 17, 2022
Add two integers
This is a stupid problem. We can just add to numbers and return the result.
Time complexity: O(1)
Space complexity: O(1)
December 17, 2022
Apply discount to prices
We will split the sentence into words. Then we will iterate through the words, and for each word, if we found a word with the $
prefix, we will replace the amount with the discounted...
December 17, 2022
Maximum sum score of array
We will calculate the prefix sum of the array. Then we will iterate through the array, and for each element, we will find the maximum sum of the subarray that ends at the current element. Then we...
ReadDecember 17, 2022
Ternary expression parser
We will use a stack to evaluate the expression. We start from the end of the expression, then append the value to the stack. If the number of element in the stack is more than 2, and the second last...
Read