Problems


September 10, 2022

Best time to buy and sell stock IV

We will create a helper function to calculate the max profit, in each step, we can either buy or if we already bought, then we can sell. And each of these steps, we can either take the item or skip...

Read

September 10, 2022

Maximum binary tree

We will recursively create our tree, we will take the largest number of the array and put it as root. Then we will create the left subtree with the left elements from the input of the root, and right...

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

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

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

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 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 8, 2022

Robot bounded in circle

The idea is, after the first iteration, if the direction is not north facing or the current position is the start position, then it is a cycle, otherwise not. First if condition basically encounters...

Read

September 8, 2022

Minimum consecutive cards to pick up

We will use a set to keep track of cards in the hand. Initially we will assume the current number of cards in the hand in infinity. We will have 2 pointers, we will move our right pointer, check if...

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