Problems
November 15, 2022
Find duplicate subtrees
We will use DFS and do preorder traversal to find all the subtrees. Then we can use a hash map to store the frequency of each subtree. If the frequency of a subtree is greater than 1, then we can add...
ReadNovember 15, 2022
Path sum III
We will use dfs to traverse the tree and for each node, we will find the number of paths that start from the current node and have the sum equal to the target sum. We will use a hashmap to store the...
ReadNovember 15, 2022
Count of matches in tournament
We can simply count the number of matches played by counting the number of times we divide the number of teams by 2. We can also count the number of teams by counting the number of times we multiply...
ReadNovember 15, 2022
Find mode in binary search tree
We will use a hashmap to store the frequency of each node's value. We will then traverse the hashmap and return the keys with the maximum frequency.
Time complexity: O(n)
Space...
November 15, 2022
Longest substring of all vowels in order
We can use a sliding window and a hash set to find the longest substring of all vowels in order.
Time complexity: O(n)
Space complexity: O(1)
November 15, 2022
Binary tree tilt
We will use DFS to find the tilt of each subtree and then add them together.
Time complexity: O(n)
Space complexity: O(n)
November 15, 2022
Maximum area of a piece of cake after horizontal and vertical cuts
We will add border cuts to our horizontal and vertical cuts. Then all we need to do is consider all horizontal gaps between adjacent cuts, all vertical gaps between adjacent cuts, choose the biggest...
ReadNovember 15, 2022
Maximum xor after operations
The problem calls for choosing an integer x, selecting an element n of the list, applying the compound operator op(n,x) = (x&n)^n, and taking the bit-intersection of the modified set. Because of...
ReadNovember 14, 2022
Most stones removed with same row or column
We will create 2 adjacency list from the stones and then we will use BFS to find the connected components. The answer is the number of stones - the number of connected components. Time complexity:...
ReadNovember 14, 2022
Merge strings alternately
We will use 2 pointers to iterate through the 2 strings and append the characters to the result.
Time complexity: O(n+m)
Space complexity: O(n+m)
We can use python's...