Find Cost to Remove Consecutive Duplicate Characters in C++

Arnab Chakraborty
Updated on 10-Nov-2020 09:07:01

482 Views

Suppose we have a string with lowercase letters and we also have a list of non-negative values called costs, the string and the list have the same length. We can delete character s[i] for cost costs[i], and then both s[i] and costs[i] is removed. We have to find the minimum cost to delete all consecutively repeating characters.So, if the input is like s = "xxyyx" nums = [2, 3, 10, 4, 6], then the output will be 6, as we can delete s[0] and s[3] for a total cost of 2 + 4 = 6.To solve this, we will follow ... Read More

Print Maximum Number of Characters by Copy-Pasting in N Steps in Python

Arnab Chakraborty
Updated on 10-Nov-2020 09:00:23

122 Views

Suppose we have a number n; we have to find the maximum number of characters we can enter using n operations where each operation is likeInserting the character "x".Copy all characters.PasteSo, if the input is like n = 12, then the output will be 81.To solve this, we will follow these stepsif n

Count Minimum Operations to Make Numbers Non-Coprime in Python

Arnab Chakraborty
Updated on 10-Nov-2020 08:57:53

331 Views

Suppose we have two numbers A and B. Now in each operation, we can select any one of the number and increment it by 1 or decrement it by 1. We have to find the minimum number of operations we need such that the greatest common divisor between A and B is not 1.So, if the input is like A = 8, B = 9, then the output will be 1, as we can select 9 then increase it to 10, so 8 and 10 are not coprime.To solve this, we will follow these steps:if gcd of a and b ... Read More

Find Next State of Cell Matrix in Python

Arnab Chakraborty
Updated on 10-Nov-2020 08:54:17

191 Views

Suppose we have a 2D binary matrix where a 1 means a live cell and a 0 means a dead cell. A cell's neighbors are its immediate horizontal, vertical and diagonal cells. We have to find the next state of the matrix using these rulesAny living cell with two or three living neighbors lives.Any dead cell with three living neighbors becomes a live cell.All other cells die.So, if the input is like1100010001011101then the output will be1100010001001100To solve this, we will follow these steps:n := row size of matrix, m := column size of matrixres := a matrix of size n ... Read More

Remove Nodes with Only One Child from a Binary Tree in Python

Arnab Chakraborty
Updated on 10-Nov-2020 08:51:15

713 Views

Suppose we have a binary tree root; we have to remove all nodes with only one child.So, if the input is likethen the output will beTo solve this, we will follow these steps:Define a method called solve(), this will take tree rootif root is null, thenreturn rootif left of root is null and right of root is null, thenreturn rootif left of root is null, thenreturn solve(right of root)if right of root is null, thenreturn solve(left of root)left of root := solve(left of root)right of root := solve(right of root)return rootExampleclass TreeNode:    def __init__(self, data, left = None, right ... Read More

Find Length of Longest Possible Stick in Python

Arnab Chakraborty
Updated on 10-Nov-2020 08:47:08

289 Views

Suppose we have a list of integers sticks. Here each element in the list represents a stick with two ends, these values are between 1 and 6. These are representing each end. We can connect two sticks together if any of their ends are same. The resulting stick's ends will be the leftover ends and its length is increased. We have to find the length of the longest stick possible.So, if the input is like sticks = [ [2, 3], [2, 4], [3, 5], [6, 6] ], then the output will be 3, as we can connect [2, 3] and ... Read More

Find Matrix for Maximum Height Increase of Condominium in Python

Arnab Chakraborty
Updated on 10-Nov-2020 08:44:10

282 Views

Suppose we have a 2D matrix, where matrix [r, c] represents the height of a condominium in a city. The west-east skyline is visible by taking the maximum of each row in the matrix. And the north-south skyline can be visible by taking the maximum of each column. We have to find a new matrix where each condominium's height is increased to the maximum possible height while keeping the same west-east and north-south skyline.So, if the input is like23456789104447778910as the west-east skyline is [4, 7, 10] and north-south skyline is [8, 9, 10]. We can increase everything in the first ... Read More

Sum of Concatenated Pairs of Elements in a List in Python

Arnab Chakraborty
Updated on 10-Nov-2020 08:41:57

589 Views

Suppose we have a list of numbers called nums. We have to find the sum of every concatenation of every pair of numbers in nums. Here the pair (i, j) and pair (j, i) are considered different.So, if the input is like nums = [5, 3], then the output will be 176, as We have the following concatenations: (nums[0] + nums[0]) = (5 concat 5) = 55, (nums[0] + nums[1]) = (5 concat 3) = 53, (nums[1] + nums[0]) = (3 concat 5) = 35, (nums[0] + nums[0]) = (3 concat 3) = 33, then the sum is 55 + ... Read More

Length of Concatenated String of Unique Characters in Python

Arnab Chakraborty
Updated on 10-Nov-2020 08:36:22

271 Views

Suppose we have a list of strings words. We have to make a string that is constructed by concatenating a subsequence of words such that each letter is unique. We have to finally find the length of the longest such concatenation.So, if the input is like words = ["xyz", "xyw", "wab", "cde"], then the output will be 9, as we cannot pick any word since they contain duplicate characters.To solve this, we will follow these stepsans := 0Define a function recur() . This will take i:= 0, cur:= blank stringif i is same as size of words , then   ... Read More

Find Minimum Number of Groups in Communication Towers in C++

Arnab Chakraborty
Updated on 10-Nov-2020 08:33:18

226 Views

Suppose we have a 2D binary matrix where 1 represents a communication tower, and 0 represents an empty cell. The towers can communicate in the following ways: 1. If tower A, and tower B are either on the same row or column, they can communicate with each other. 2. If tower A can talk with tower B and B can talk with C, then A can talk to C (transitive property). We have to find the total number of tower groups there are (here a group is a list of towers that can talk with each other).So, if the input ... Read More

Advertisements