Minimum Cost to Paint Fences with K Different Colors in Python

Arnab Chakraborty
Updated on 22-Dec-2020 08:20:48

307 Views

Suppose we want to paint a row of N fences with K different colors. We want to minimize the cost while ensuring that no two neighboring fences are of the same color. So if we have an N x K matrix where the nth row and kth column represents the cost to paint the nth fence with kth color, we have to find the minimum cost which achieves this goal.So, if the input is like645327345544then the output will be 14, as we can select the following color indices (starting from the first fence) − 5 → 2 → 3 → ... Read More

Find Maximum Number of Packages Buyers Can Purchase in C++

Arnab Chakraborty
Updated on 22-Dec-2020 08:18:29

284 Views

Suppose we have two lists sales and buyers. Each element in sales contains two values in the form [day, price] this indicates the package is available for sale only on that day for that given price. And each element in buyers in the form [payday, amount] indicates that the buyer has that amount of money to spend on payday and afterward. If each buyer can buy at most one package, and each package can be sold to only one person, find the maximum number of packages that can be bought.So, if the input is like sales = [[0, 5], [0, ... Read More

Get Maximum Value of Power of a List by Rearranging Elements in Python

Arnab Chakraborty
Updated on 22-Dec-2020 06:44:36

658 Views

Suppose we have a list nums of N positive numbers. Now we can select any single value from the list, and move (not swap) it to any position. We can also not move any to position at all. So we have to find what is the maximum possible final power of the list? As we know the power of a list is the sum of (index + 1) * value_at_index over all indices i.$$\displaystyle\sum\limits_{i=0}^{n-1} (i+1)\times list[i]$$So, if the input is like nums = [6, 2, 3], then the output will be 26, as we can move the 6 to the ... Read More

Find Length of Longest Increasing Subsequence with At Least K Odd Values in Python

Arnab Chakraborty
Updated on 22-Dec-2020 06:40:36

246 Views

Suppose we have a list of numbers called nums and another value k, we have to find the size of the longest increasing subsequence with at least k odd elements.So, if the input is like nums = [12, 14, 16, 5, 7, 8] k = 2, then the output will be 3, as the longest increasing subsequence with at least 2 odd values is [5, 7, 8].To solve this, we will follow these steps −best := 0Define a function dp() . This will take i, j, odd, takenif odd >= k, thenbest := maximum of best and takenif j is ... Read More

Find Number of Operations to Decrease N to 0 in C++

Arnab Chakraborty
Updated on 22-Dec-2020 06:38:57

205 Views

Suppose we have a number n. Now consider an operation where we can either 1. Decrement n by one 2. If n is even number, then decrement it by n / 2 3. If n is divisible by 3, then decrement by 2 * (n / 3) Finally find the minimum number of operations required to decrement n to zero.So, if the input is like n = 16, then the output will be 5, as n = 16 even then decreases it by n/2 4 times, it will be 1. Then reduce it by 1 to get 0. So total ... Read More

Get Next Integer Permutation of a Number in C++

Arnab Chakraborty
Updated on 22-Dec-2020 06:36:12

445 Views

Suppose we have a number n, we have to find the next bigger permutation of its digits. When n is already in its largest permutation, then rotate it down to the smallest permutation.So, if the input is like n = 319, then the output will be 391.To solve this, we will follow these steps −Define a function makeArray(), this will take x, Define an array retwhile x is non-zero, do −insert x mod 10 at the end of retx := x / 10reverse the array retreturn retDefine a function combine(), this will take an array v, ret := 0for each ... Read More

Check N-Queens Solution in Python

Arnab Chakraborty
Updated on 22-Dec-2020 06:32:49

256 Views

Suppose we have a binary matrix where 0 is representing empty cell and 1 is representing a chess queen at that cell. We have to check whether we can fill this board and get a valid nqueen solution or not. As we know the n queens puzzle asks to place n queens on an n × n chessboard so that no two chess queens can attack each other.So, if the input is like1000000000000010000000010then the output will be True, as one solution is like −1000000100000010100000010To solve this, we will follow these steps −Define a function isSafe() . This will take board, ... Read More

Find Length of Longest Valid Parenthesis in Python

Arnab Chakraborty
Updated on 22-Dec-2020 06:27:58

130 Views

Suppose we have a string s. This s consists of opening and closing parenthesis only. We have to find the length of the longest valid (well-formed) parentheses substring. So if the input is like “))(())())”, then the result will be 6, as the valid string is “(())()”.To solve this, we will follow these steps −Make a stack, and insert -1., set ans := 0for i in range 0 to length of stack – 1if s[i] is opening parentheses, then insert i into stackotherwiseif stack is not empty and top of stack is not -1 and s[stack top] is opening parentheses, ... Read More

Find Maximum Price by Holding Items in a Bag in Python

Arnab Chakraborty
Updated on 22-Dec-2020 06:26:20

358 Views

Suppose we have two lists of numbers. One is called weights and another is called values. These are of same length, We also have two values called capacity and count. Here weights[i] and values[i] represent the weight and value of the ith item. We can hold at most capacity weight and at most count items in total, and we can take only one copy of each item, so we have to find the maximum amount of value we can get.So, if the input is like weights = [2, 2, 4, 6] values = [15, 15, 20, 35] capacity = 8 ... Read More

Get Final Position of Moving Animals When They Stop in Python

Arnab Chakraborty
Updated on 22-Dec-2020 06:24:50

210 Views

Suppose we have a string s that is representing the initial conditions of some animals. Each animal can take one of three values: L, indicates the animal moved to left. R, indicates the animal moved to right. @, indicates the animal is standing still. Animals moving on a direction will pick up other animals unless the animal receives a force from the opposite direction. Then, it will stand still. We have to find the orientation of each animal when the animal stop moving.So, if the input is like s = "@@L@R@@@@L", then the output will be "LLL@RRRLLL"To solve this, we ... Read More

Advertisements