Burst Balloons in C++

Arnab Chakraborty
Updated on 27-May-2020 06:00:28

467 Views

Suppose we have n balloons, these are indexed from 0 to n-1. Here each balloon is painted with a number on it represented by one array called nums. we have to burst all the balloons. If we burst balloon i we will get nums[i – 1] * nums[i] * nums[i + 1] number of coins. After the burst, the i – 1 and i + 1 then becomes adjacent. We have to find the maximum coins to collect by bursting the balloons wisely.So if the input is like [3, 1, 5, 7], then the result will be 148. Initially the ... Read More

Remove Invalid Parentheses in C++

Arnab Chakraborty
Updated on 27-May-2020 05:56:46

369 Views

Suppose we have a string of parentheses. We have to remove minimum number of invalid parentheses and return the well formed parentheses strings, So if the input is like “()(()()”, so the result will be [“()()()”, “()(())”]To solve this, we will follow these steps −Define a method called solve(), this will take pos, left, right, l, r, string array res and string temp.if pos is same as size of s, then, if left is same as 0 and right is same as 0, then, if (m[temp] is non-zero) is false, then, insert temp at the end of resm[temp] := 1returnif ... Read More

Find Median from Data Stream in C++

Arnab Chakraborty
Updated on 27-May-2020 05:49:50

467 Views

Suppose we have a data stream, in that stream some data element may come and join, we have to make one system, that will help to find the median from the data. As we know that the median is the middle data of a sorted list, if it list length is odd, we can get the median directly, otherwise take middle two elements, then find the average. So there will be two methods, addNum() and findMedian(), these two methods will be used to add numbers into the stream, and find the median of all added numbersTo solve this, we will ... Read More

Expression Add Operators in C++

Arnab Chakraborty
Updated on 27-May-2020 05:46:17

318 Views

Suppose we have a string that holds only digits from 0 to 9. And one target value is given. We have to return all possibilities to add binary operators +, - and * into the digits to get the target values. So if the input is like “232” and the target is 8, then the answer will be [“2*3+2”, “2+3*2”]To solve this, we will follow these steps −Define a method called solve(), this will take index, s, curr, target, temp, mult −if idx >= size of s, then, if target is same as curr, then, insert temp at the end ... Read More

Sliding Window Maximum in C++

Arnab Chakraborty
Updated on 27-May-2020 05:36:18

823 Views

Suppose we have an array called nums, there is a sliding window of size k which is moving from the left of the array to the right. We can only see the k numbers in the window. Each time the sliding window moves to the right side by one position. We have to find the max sliding window. So if the input is like −[1, 3, -1, -3, 5, 3, 6, 8] and k is 3, then the window will be like −Window PositionMax13-1-35368313-1-35368313-1-35368313-1-35368513-1-35368613-1-353688To solve this, we will follow these steps −Define an array ansDefine one double ended queue dqif ... Read More

Number of Digit One in C++

Arnab Chakraborty
Updated on 27-May-2020 05:32:06

430 Views

Suppose we have a number n, We have to count the total number of digit 1 appearing in all non-negative numbers less than or equal to n. So if the input is 15, then the output will be 8, because the numbers containing 1 is [1,10,11,12,13,14,15], there are 8 1s.To solve this, we will follow these steps −ret := 0for initializing i := 1, when i

Basic Calculator in C++

Arnab Chakraborty
Updated on 26-May-2020 14:11:17

1K+ Views

Suppose we want to create one basic calculator that will find the basic expression result. The expression can hold opening and closing parentheses, plus or minus symbol and empty spaces.So if the string is like “5 + 2 - 3”, then the result will be 7To solve this, we will follow these steps −ret := 0, sign := 1, num := 0, n := size of sDefine one stack stfor initializing i := 0, when i < n, increase i by 1 do −Define an array x = s of size iif x >= '0' and x = '0' && x

Shortest Palindrome in C++

Arnab Chakraborty
Updated on 26-May-2020 14:06:52

772 Views

Suppose we have a string s. We can convert it to palindrome by adding characters in front of it. We have to find the shortest palindrome, that we can find performing this information. So if the string is like “abcc”, then the result will be − "ccbabcc".To solve this, we will follow these steps −n := size of s, s1 := s, s2 := sReverse the string s2s2 := s concatenate "#" concatenate s2Define an array lps of size same as s2j := 0, i := 1while i < size of s2, do −if s2[i] is same as s2[j], then, ... Read More

Word Search II in Python

Arnab Chakraborty
Updated on 26-May-2020 14:03:03

725 Views

Suppose we have a 2D board and a list of words. So from the dictionary, we have to find all words in the board. Here each word must be constructed from letters of sequentially adjacent cell, where the adjacent cells are those horizontally or vertically neighboring. We have to keep in mind that the same letter cell may not be used more than once in a word.So if the input is like −To solve this, we will follow these steps −make an array resultDefine a method called solve(), this will take board, d, i, j swhen either i or j ... Read More

Best Time to Buy and Sell Stock IV in C++

Arnab Chakraborty
Updated on 26-May-2020 13:57:46

204 Views

Suppose we have an array for which the i-th element is the price of a given stock for the day i. We have to devise an algorithm to find the maximum profit. We can complete at most k transactions. So if the input is like [3, 2, 6, 4, 0, 3] and k = 2, then the output will be 7, as buy on day 2 (when price = 2) and sell on day 3 (when price = 6), profit will be 6-2 = 4. Then buy on day 5 (price is 0) and sell on day 6 (price is ... Read More

Advertisements