Problems


September 10, 2022

Maximum binary tree II

We will run a simple DFS to find the place and insert the new node there. Time Complexity: O(n) Space Complexity: O(n)

Read

September 10, 2022

Partition array according to given pivot

We will take 3 list, one for less, one for equal and one for more than the pivot value. Then we iterate over the input list, append the value to the respective list. Finally we combine 3 list, and...

Read

September 9, 2022

Peeking iterator

We will use a current value property in the class to keep track of current value of the iterator. For rest of the functionality we will just relay the behaviour of the original iterator. Time...

Read

September 9, 2022

Remove linked list elements

We will create a dummy node and attach it before our head. Then we will iterate over each node, if the value of the node is equal to our node, we will remove them. Then we will return the next node...

Read

September 9, 2022

Design a food rating system

We will have a hashmap for mapping the food to the cuisines and another hashmap to for the ratings. Whenever we receive a new rating or change rating, we add it to our max heap where we store the...

Read

September 9, 2022

The number of weak characters in the game

We will use a max heap with our properties and use the attack as sorting mechanism. Then we loop over our properties, pop the max item from the heap, and then we need to check the current defense,...

Read

September 9, 2022

Tuple with same product

We will first count the product of 2 elements to a hashmap, where the key will be product. Then we iterate over each product, if it is more than 1, then we calculate the number of tuple by...

Read

September 9, 2022

Minimum number of swaps to make the string balanced

We will iterate over the string, for each opening bracket, we increase the left count by 1. For each closing bracket, if the count of left braket is greater than right bracket, then we increse the...

Read

September 8, 2022

Maximum erasure value

We will use a set to keep track of repeating numbers. We will have 2 pointers, we will move our right pointer, check if the number as right pointer is already exist in the set, if exists, then we...

Read

September 8, 2022

Find k closest elements

We will iterate over our input array, find the absolute diffrece of each elements form target x, and put them into a heap alogn with the actual number. Then we pop k numbers from the heap, sort the...

Read
... 59 60 61 62 63 ...