Coin Change Problem II in C++

Arnab Chakraborty
Updated on 29-Apr-2020 05:51:30

530 Views

Suppose we have coins of different denominations and a total amount of money. we have to Write a module to compute the number of combinations that make up that amount. we can assume that we have infinite number of each kind of coin. So if the amount is 5 and coins are [1, 2, 5], then there are four combinations. (1+1+1+1+1), (1+1+1+2), (1+2+2), (5)To solve this, we will follow these steps −create one array dp of size amount + 1dp[0] := 1n := size of coins arrayfor i in range 0 to n – 1for j in range coins[i] to ... Read More

Next Greater Element II in C++

Arnab Chakraborty
Updated on 29-Apr-2020 05:46:18

256 Views

Suppose we have a circular array (the next element of the last element is the first element of the array), we have to display the Next Greater Number for every element. Here the Next Greater Number of a number x is the first greater number to its traversing-order next in the array, this means we could search circularly to find its next greater number. If it is not present, then it will be -1. So if the numbers are [1, 2, 1, 3, 2, 1], then output will be: [2, 3, 3, -1, 3, 2]To solve this, we will follow ... Read More

Target Sum in C++

Arnab Chakraborty
Updated on 29-Apr-2020 05:41:25

729 Views

Suppose we have a list of non-negative integers, a1, a2, ..., an, and another value, that is target, S. Now we have 2 symbols + and -. For each integer, we should choose one from + and - as its new symbol. we have to find out how many ways to assign symbols to make sum of integers same as the target value S. So if the numbers are [1, 1, 1, 1, 1], and S = 3, then the output will be 5, as the combinations are – 1 + 1 + 1 + 1 + 1 = 3, ... Read More

Occurrences After Bigram in Python

Arnab Chakraborty
Updated on 28-Apr-2020 17:45:32

180 Views

Suppose there are words given. These are first and second, consider occurrences in some text of the form "first second third", here second comes immediately after the first, and third comes immediately after the second.For each such cases, add "third" into the answer, and show the answer. So if the text is like “lina is a good girl she is a good singer”, first = “a”, second = “good”, the answer will be [girl, singer]To solve this, we will follow these steps −text := split the string by spacesres is an empty listfor i := 0 to size of text ... Read More

Greatest Common Divisor of Strings in Python

Arnab Chakraborty
Updated on 28-Apr-2020 17:42:47

3K+ Views

Suppose there are two strings A and B. We can say that A is divisible by B, when A is created by concatenating B one or more times. So if A = “abcabc”, and B = “abc”, then A is divisible by B. In this section, we will see what is the greatest common divisor of a String. So return the largest string that divides both of the strings. So if two strings are “ABABAB”, and “ABAB”, then GCD will be “AB”To solve this, we will follow these steps −temp := shorter string between A and Bm := length of ... Read More

Height Checker in Python

Arnab Chakraborty
Updated on 28-Apr-2020 17:40:34

1K+ Views

Suppose a set of students have to be arranged in non-decreasing order of their heights for a photograph. If we have an array of students, we have to return the minimum number of students that are not present in correct position. So if the array is like [1, 1, 4, 2, 1, 3], then output will be 3. So students with height 4, 3 and the last 1 are not standing in the correct position.To solve this, we will follow these steps −answer := 0let x := Array in sorted formley y := Arrayfor i := 0 to size of ... Read More

Remove All Adjacent Duplicates in String in Python

Arnab Chakraborty
Updated on 28-Apr-2020 17:38:36

3K+ Views

Suppose we have a string S of lowercase letters; a duplicate removal operation will be performed. This will be done by choosing two adjacent and equal letters, and removing them.We will repeatedly remove duplicates from S until no duplicates are remaining.Return the string after all such duplicate removals have been completed. It is guaranteed that the answer is unique.Suppose the string is “abbacaca”, then answer will be “caca”. At first delete duplicates bb, then string is “aacaca”, then remove aa, then string is “caca”, then no such duplicates are there.To solve this, we will follow these steps −Define an array ... Read More

Last Stone Weight in Python

Arnab Chakraborty
Updated on 28-Apr-2020 17:36:07

1K+ Views

Suppose we have some rocks, each rock has a positive integer weight. In each turn, we will take two heaviest rocks and smash them together. consider the stones have weights x and y and x 1:          stones.sort()          s1,s2=stones[-1],stones[-2]          if s1==s2:             stones.pop(-1)             stones.pop(-1)          else:             s1 = abs(s1-s2)             stones.pop(-1)             stones[-1] = s1       if len(stones):          return stones[-1]       return 0 ob1 = Solution() print(ob1.lastStoneWeight([2,7,4,1,6,1]))Input[2,7,4,1,6,1]Output1

Two City Scheduling in C++

Arnab Chakraborty
Updated on 28-Apr-2020 17:33:00

530 Views

Suppose there are 2N persons. A company wants to organize one interview. The cost for flying the i-th person to city A is costs[i][0], and the cost for flying the i-th person to city B is costs[i][1]. We have to find the minimum cost to fly every person to a city, such that N people arrive in every city. So if the given list is [[10, 20], [30, 200], [400, 50], [30, 20]] The output will be 110. So we will send the person P1 to city A with cost 10, Second person to city A with cost 30, third ... Read More

Partition Array into Three Parts with Equal Sum in Python

Arnab Chakraborty
Updated on 28-Apr-2020 17:30:30

473 Views

Suppose we have an array A of integers, our output will be true if and only if we can partition the array into three non-empty parts whose sum is equal.Formally, we can partition the array if we can find the indexes i+1 < j with (A[0] + A[1] + ... + A[i] is same as A[i+1] + A[i+2] + ... + A[j-1] and A[j] + A[j-1] + ... + A[A.length - 1])So if the input is [0, 2, 1, -6, 6, -7, 9, 1, 2, 0, 1], then the output will be true. Three arrays will be [0, 2, 1], ... Read More

Advertisements