Find Lexicographically Smallest Subsequence of Size K in Python

Arnab Chakraborty
Updated on 12-Dec-2020 10:04:26

334 Views

Suppose we have a list of numbers called nums and another value k, we have to find the lexicographically smallest subsequence of size k.So, if the input is like nums = [2, 3, 1, 10, 3, 4] k = 3, then the output will be [1, 3, 4]To solve this, we will follow these steps −l := size of nums, r := k - 1out := a new listfor j in range 0 to k, domn := nums[complement of r]for i in range r to l, doif mn >= nums[complement of i], thenmn := nums[complement of i]l := ir := ... Read More

Justify Words into Same Length Lines in C++

Arnab Chakraborty
Updated on 12-Dec-2020 10:00:33

102 Views

Suppose we have an list of words and a width k, we have to arrange the text such that each line has exactly k number of characters and the text is fully justified. Here we shall pack our words as many words as we can insert in each line. And we shall pad extra spaces ' ' when necessary so that each line has exactly k characters.Here extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line do not divide evenly between words, empty slots on the left will be assigned ... Read More

Find Minimum Sum of Difficulties to Complete Jobs Over K Days in C++

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

189 Views

Suppose we have a list of numbers called jobs and another value k. Now we want to finish all jobs in k different days. The jobs must be performed in the given order and in each day we have to complete one task. The difficulty of job i is stored at jobs[i] and the difficulty of completing a list of jobs on a day will be the maximum difficulty job performed on that day. So we have to find the minimum sum of the difficulties to perform the jobs over k different days.So, if the input is like jobs = ... Read More

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

Find All Possible IP Addresses 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

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

Find Cost to Reach Final Index of Two Lists in Python

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

165 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

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

Count Swaps 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

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

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

117 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

Advertisements