Found 33676 Articles for Programming

Program to get maximum profit by scheduling jobs in Python

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

829 Views

Suppose we have a list of intervals where each interval contains three values [start, end, profit]. We can perform only one task at a time, we have to find the most amount of profit we can get.So, if the input is like intervals = [[1, 2, 100], [3, 5, 40], [6, 19, 150], [2, 100, 250]], then the output will be 350, as we can take these two intervals [1, 2, 100] and [2, 100, 250]To solve this, we will follow these stepsd := an empty map that contains lists as valuesn := 0for each (start, end, profit) in intervals, ... Read More

Program to find all possible IP address after restoration in C++

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

346 Views

Suppose we have a string with only digits, we have to restore it by forming all possible valid IP address combinations. We know that a valid IP address consists of exactly four integers (each integer is in range 0 to 255) separated by single period symbol.So, if the input is like ip = "25525511136", then the output will be ["254.25.40.123", "254.254.0.123"]To solve this, we will follow these steps −Define a function convertToNum(), this will take s, start, end, num := 0for initialize i := start, when i 255, then −return 10000return numDefine a function addDots(), this will take positions, ... Read More

Program to find number of increasing subsequences of size k in Python

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

482 Views

Suppose we have a list of numbers called nums and also another value k, we have to find the number of subsequences of size k that are strictly increasing. If the answer is very large, mod it by 10^9 + 7.So, if the input is like nums = [2, 3, 4, 1] k = 2, then the output will be 3, as we have the subsequences of size 2: [2, 3], [3, 4], [2, 4].To solve this, we will follow these steps −m := 10^9 + 7dp := a list of size same as nums and fill with 1iterate the ... Read More

Program to find cost to reach final index of any of given two lists in Python

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

166 Views

Suppose we have two lists of numbers nums0 and nums1 of the same length and two other values d as distance and c as cost. If we start from index 0 at either nums0 or nums1 and want to end up at the final index of either list. Now, in each round, we can select to switch to the other list for cost of cost. Then we can jump forward at most d distance away where the c cost of landing at an index is the value at that point. So we have to find the minimum total cost possible ... Read More

Program to find minimum number of pins required to hang all banners in C++

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

313 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

Program to count number of swaps required to group all 1s together in Python

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

166 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

Program to count number of queries that are true in a graph with weighted path in C++

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

118 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

Program to check whether first player can win a game where players can form string char by char in C++

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

190 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

Program to check we can cross river by stones or not in Python

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

363 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

Program to find minimum number colors remain after merging in Python

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

200 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

Advertisements