Problems


December 1, 2022

Evaluate the bracket pairs of a string

We will use a hashmap to store the key-value pairs. Then we will iterate over the string and if we encounter a (, we will start a new string. If we encounter a ), we will...

Read

December 1, 2022

Determine if string halves are alike

We will itrerate over the string, for first half we increase the count and for second half we decrease the count. At the end if the count is 0, then the string halves are alike. Time complexity:...

Read

December 1, 2022

Majority element II

We will count the frequency of each element in the array and return the elements whose frequency is greater than n//3. We will use a hashmap to store the frequency of each element, and a...

Read

November 30, 2022

Maximum average subtree

We will traverse the tree in post-order fashion. For each node, we will calculate the sum of all nodes in its subtree and the number of nodes in its subtree. Then we will check if the average of the...

Read

November 30, 2022

Find leaves of binary tree

We will traverse the tree in post-order fashion. For each node, we will check if it is a leaf node. If it is, we will append the value of the node in a list. Then we will check if the left and right...

Read

November 30, 2022

Recover a tree from preorder traversal

We save the construction path in a stack. In each loop, we get the number level of '-'. We get the value val of node to add. If the size of stack is bigger than the level of node, we pop the stack...

Read

November 30, 2022

Find all lonely numbers in the array

We will count the occurrences of each number in the array and return the numbers that have only one occurrence and no adjacent number is present in the list. Time complexity: O(n), n...

Read

November 30, 2022

Fizz buzz

We will iterate over the numbers from 1 to n and append the corresponding string to the result list. If the number is divisible by 3, we will append "Fizz" to the result list. If the number is...

Read

November 30, 2022

Remove duplicates from an unsorted linked list

We will use a hashmap to count the frequency of each node in the linked list. Then we will traverse the linked list again and remove the nodes that have frequency greater than 1. Time complexity:...

Read

November 30, 2022

Partitioning into minimum number of deci binary numbers

Assume max digit in n is x, because deci-binary only contains 0 and 1, we need at least x numbers to sum up a digit x. So we can just find the max digit in n and return it. Time complexity:...

Read
... 25 26 27 28 29 ...