Problems


September 13, 2022

Is graph bipartite

If we run BFS, then every level of traversal will have a different color. So we start from node 0, then start traversal using BFS and on each level, we alternate the color. In the process if we find...

Read

September 12, 2022

Bag of tokens

We will first sort the tokens and take 2 pointers. If the value of the left pointer token is less than our power, we face down the token, so our score increase, we will also keep a running max score....

Read

September 12, 2022

Elimination game

We will recursively eleminate people from beginning to end and then return backword with the same logic until only one person left, then return that. Time Complexity: O(n) Space...

Read

September 12, 2022

Sender with largest word count

We will create a count each words of the the messages for each sender. Then we take the sender with the highest number of word count, sort them and then return the largest sender name...

Read

September 12, 2022

Different ways to add parentheses

We will create a decision tree with all possible options. As it is given only 3 types of operation is possible, we will check whether the current character belongs to any of these, then we calculate...

Read

September 12, 2022

Longest word in dictionary

We will create a trie and insert all the words in our trie. Then we run BFS to get the largest word, as we are only append the word in our BFS queue, when we reach word's end. So, the longest word...

Read

September 11, 2022

Maximum difference between node and ancestor

We will keep track of both max value of the tree and min value of the tree from root to leaf. When we reach the leaf, we store the difference between these 2 in our result. We will run the same logic...

Read

September 11, 2022

Max sum of a pair with equal sum of digits

This problem is very similar to two sum problem. But instead of storing the complement of the number, we will store the sum of digits of the number. When we iterate over the numbers, we will also...

Read

September 11, 2022

Find the city with the smallest number of neighbors at a threshold distance

We will first create an adjacency list from the edge list. Then we run dijkstra's shortest path algorithm to get the number of neighbors of a city. We will run this dijkstra's algorithm for each node...

Read

September 11, 2022

Number of restricted paths from first to last node

We will first create an adjacency list from the edge list. Then we run dijkstra's shortest path algorithm to get the shortest distance from first node every other node. Then we run DFS from first...

Read
... 57 58 59 60 61 ...