Minimum Number of Pins Required to Hang All Banners in C++

Arnab Chakraborty
Updated on 12-Dec-2020 09:40:11

322 Views

Suppose we have a list of intervals of the form [start, end] this is representing the starts and end points of banners we want to hang. At least one pin is required to hang a banner, and one pin can hang more than once banners. We have to find the smallest number of pins required to hang all the banners.So, if the input is like intervals = [[2, 5], [5, 6], [8, 10], [10, 13]], then the output will be 2, as we can put two pins at position 5 and 10 to hang all of the banners.To solve this, ... Read More

Count Swaps to Group All 1s Together in Python

Arnab Chakraborty
Updated on 12-Dec-2020 09:37:51

173 Views

Suppose we have a binary string, and we can swap any two bits. We have to find the minimum number of swaps required to group all 1s together.So, if the input is like s = "0111001", then the output will be 1, as We can perform these swaps: 0111001 -> 1111000.To solve this, we will follow these steps −data := a list of 0s and 1s from the given binary stringset one := 0, n:= length of data arraymake an array summ of size n, and fill this with 0, set summ[0] := data[0]one := one + data[0]for i in ... Read More

Count True Queries in a Graph with Weighted Path in C++

Arnab Chakraborty
Updated on 12-Dec-2020 09:35:09

126 Views

Suppose we have an edge list for an undirected graph where each edge has [u, v, w] fields, u and v are source and destination vertices and w is the weight. And also have a list of queries of the same form [u, v, w]. That represents the question of does there exist a path between u and v such that each edge in the path have weight of at most w. So find the number of queries that are true.So, if the input is like edges = [[0, 1, 6], [1, 2, 7], [2, 3, 8], [0, 3, 5]] ... Read More

Check First Player Win in String Game Using C++

Arnab Chakraborty
Updated on 12-Dec-2020 09:32:10

196 Views

Suppose we have a list of words. Now consider a ghost game where two players can participate into it. Here players alternate appending letters to a string. And the string that is being made must be a valid prefix of a word in the list, and the player who spells out any word in the list loses. We have to check whether the first player can win or not if both players are playing optimally.So, if the input is like words = ["manage", "manager", "min"], then the output will be True, as they can play like −m [Player 1]ma [Player ... Read More

Check If We Can Cross River by Stones in Python

Arnab Chakraborty
Updated on 12-Dec-2020 09:29:42

374 Views

Suppose we have a list of sorted numbers called stones and this is representing the positions of stones on a river that we are trying to cross. To cross the river, we must finish at the last stone. Now in each step, we can jump (k - 1, k, or k + 1) steps ahead where k is the distance of the last jump. We have to check whether we can cross the river or not.So, if the input is like stones = [0, 1, 3, 4, 5, 6, 8, 9, 13], then the output will be True, as we ... Read More

Find Minimum Number of Colors After Merging in Python

Arnab Chakraborty
Updated on 12-Dec-2020 09:27:21

208 Views

Suppose we have a list of colors (R, G, B). Now if two different colors are there next to each other then they can transform into a single color item of the third color. We have to find the smallest number of them remaining after any possible sequence of such transformations.So, if the input is like colors = ["G", "R", "G", "B", "R"], then the output will be 1 as it can transform like below −To solve this, we will follow these steps −n := size of colorsif colors has only one distinct color, thenreturn nif n

Find Land with Longest Distance from Water in Python

Arnab Chakraborty
Updated on 12-Dec-2020 09:25:00

203 Views

Suppose we have a binary matrix where, 0 represents water and 1 represents land. Now we have to find the land which has the longest Manhattan distance from water and finally return the distance.So, if the input is like1111110111110011then the output will be 3, as [0, 0] cell has Manhattan distance of 3 from water.To solve this, we will follow these steps −if A is empty, thenreturn 0R := row count of matrix, C := column count of matrixdistance := a matrix of order R x C and fill with 0q := a double ended queue with some pairs (r, ... Read More

Convert One String to Another in C++

Arnab Chakraborty
Updated on 12-Dec-2020 09:22:27

225 Views

Suppose we have two strings S and T. We have to find the shortest sequence of operations that changes S to T. Here the operations are basically either deleting or inserting a character.So, if the input is like S = "xxxy" T = "xxyy", then the output will be ["x", "x", "-x", "y", "+y"], this means place first two x's, then remove 3rd x, then place y then add a new y.To solve this, we will follow these steps −make a table dp of size 505 x 505Define a function help(), this will take i, j, S, T, if i ... Read More

Count Ways to Fill 3 x n Box with 2 x 1 Dominos in Python

Arnab Chakraborty
Updated on 12-Dec-2020 09:18:55

260 Views

Suppose we have a number n, we have to find the number of ways we can fill a (3 x n) block with 1 x 2 dominos. We can rotate the dominos when required. If the answer is very large then return this mod 10^9 + 7.So, if the input is like n = 4, then the output will be 11.To solve this, we will follow these steps −m = 10^9 + 7if n is odd, thenreturn 0cs := 1, os := 0for i in range 2 to n, increase by 2, docs := 3 * cs + osos := ... Read More

Find Nth Term of a Sequence Divisible by A, B, C in Python

Arnab Chakraborty
Updated on 12-Dec-2020 09:17:05

392 Views

Suppose we have four numbers n, a, b, and c. We have to find the nth (0 indexed) term of the sorted sequence of numbers divisible by a, b or c.So, if the input is like n = 8 a = 3 b = 7 c = 9, then the output will be 18, as The first 9 terms of the sequence are [1, 3, 6, 7, 9, 12, 14, 15, 18].To solve this, we will follow these steps −if minimum of a, b, c is same as 1, thenreturn nab := lcm(a, b), bc := lcm(b, c), ca := ... Read More

Advertisements