Problems
January 13, 2023
Number of good binary strings
We will solve it using top-down dynamic programming. We will create a helper function dp that will return the number of good binary strings of length n
and ending with end
....
January 13, 2023
Longest path with different adjacent characters
We will create a tree from the edges list. Then we will start DFS from starting node 0
. Then we will calculate the longest path in the subtree of each node. Finally we will return the...
January 12, 2023
Number of nodes in the sub tree with the same label
We will create a tree from the edges list. Then we will start DFS from starting node 0
. Then we will calculate all the nodes in the subtree of each node. Finally we will return the...
January 11, 2023
Maximum number of points with cost
We will use top-down approach to solve the problem. We will calculate the maximum points for each cell. Then we will return the maximum points in the last row.
Time complexity: O(mn)
...
January 11, 2023
Minimum time to collect all apples in a tree
We will create a graph form the edges. Then we will start from node 0, and run DFS to visit all the apples and count the number of edges on the way. Finally we will return the maximum number of...
ReadJanuary 11, 2023
Maximum binary string after change
We will use greedy approach to solve the problem. We will find the first 0
and change it to 1
. Then we will find the next 0
and change it to 1
. We...
January 10, 2023
Shuffle the array
We will iterate over the half of of the array and add the elements to the result array form the front and middle.
Time complexity: O(n)
Space complexity: O(1)
January 10, 2023
Find good days to rob the bank
We will calculate the prefix sum and postfix sum of the securities. Then we iterate form time till securities-time, check the prefix and postfix sum for the given condition, if the satify, we add...
ReadJanuary 9, 2023
Find and replace in string
We will initialize the string as a character array. Then create a replacement map using the provided source, indices and target. Then we iterate over this map, and replace the characters. Finally...
ReadJanuary 9, 2023
Confusing number
We will use a dictionary to map the digits to their confusing digits. We will iterate through the digits of the number and check if the confusing digit is the same as the original digit. If it is, we...
Read