Problems


November 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...

Read

November 15, 2022

Make two arrays equal by reversing subarrays

We can sort both arrays and compare them. If they are equal, then we can make the two arrays equal by reversing subarrays. Time complexity: O(nlog(n)) Space complexity:...

Read

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)

Read

November 15, 2022

Maximum width of binary tree

We will use BFS to find the maximum width of the tree. Time complexity: O(n) Space complexity: O(n)

Read

November 15, 2022

Most frequent subtree sum

First we will use DFS to find all the subtree sum and then use a hash map to store the frequency of each subtree sum. Then we can find the most frequent subtree sum. Time complexity:...

Read

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...

Read

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)

Read

November 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...

Read

November 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...

Read

November 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:...

Read
... 37 38 39 40 41 ...