Flatten Nested List Iterator in Python

Arnab Chakraborty
Updated on 29-Apr-2020 12:33:50

452 Views

Suppose we have a nested list of integers; we have to implement an iterator to flatten it. Each element is either an integer, or a list. The elements of that list may also be integers or other lists. So if the input is like [[1, 1], 2, [1, 1]], then the output will be [1, 1, 2, 1, 1]To solve this, we will follow these steps −In the initializing section, it will take the nested list, this will work as follows −set res as empty list, index := 0, call getVal(nestedList)The getVal() will take nestedIntegers, this will work as −for ... Read More

House Robber III in C++

Arnab Chakraborty
Updated on 29-Apr-2020 12:32:55

449 Views

Suppose one thief has found himself a new place for his thievery again. There is only one entrance to this area, the entrance is called the "root." Besides the root, each house has one and only one parent house. After a tour, the smart thief felt that "all houses in this place forms a binary tree". And It will automatically contact the police if two directly-linked houses were broken into on the same night. We have to find the maximum amount of money the thief can rob tonight without alerting the police. So if the tree is like −Then the ... Read More

Super Ugly Number in C++

Arnab Chakraborty
Updated on 29-Apr-2020 12:30:42

351 Views

We have to create one function to find the nth super ugly number. The super ugly numbers are positive numbers whose all prime factors are in the given prime list primes of size k. So if the n is 12 and primes are [2, 7, 13, 19], then the output will be 32, this is because [1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32] is the sequence of 12 super ugly numbers.To solve this, we will follow these steps −Create a data structure triplet, with num, prime and idxif n is 1, then return 1, create ... Read More

Reduce Array Size to the Half in C++

Arnab Chakraborty
Updated on 29-Apr-2020 12:29:34

1K+ Views

Suppose we have an array arr. We can choose a set of integers and remove all the occurrences of these integers in the array. We have to find the minimum size of the set so that at least half of the integers of the array are removed. So for example, if arr = [3, 3, 3, 3, 5, 5, 5, 2, 2, 7], then the output will be 2. This is because if we choose {3, 7} this will make the new array [5, 5, 5, 2, 2] which has size 5 (this is equal to half of the size ... Read More

Add and Search Word Data Structure Design in C++

Arnab Chakraborty
Updated on 29-Apr-2020 12:28:23

500 Views

Suppose we have to design a data structure that supports the following two operations −addWord(word)search(word)The search(word) method can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter. So for example, if we add some words like “bad”, “dad”, “mad”, then search for search(“pad”) → false, search(“bad”) → true, search(“.ad”) → true and search(“b..”) → trueTo solve this, we will follow these steps −There are some methods, initially define insertNode(), this will take the head reference and the string s, this will work as follows −curr ... Read More

Course Schedule II in Python

Arnab Chakraborty
Updated on 29-Apr-2020 12:26:29

367 Views

Suppose there are a total of n courses, these are labeled from 0 to n-1. Some courses may have prerequisites, given the total number of courses and a list of prerequisite pairs, we have to find the ordering of courses that we should take to finish all courses. There may be multiple correct orders, we just need to find one of them. If it is impossible to finish all courses, then return an empty array.So if the input is like 2, [[1, 0]], then the result will be [0, 1]. There are a total of 2 courses to take. To ... Read More

Number of Sub-arrays of Size K with Average Greater than or Equal to Threshold in C++

Arnab Chakraborty
Updated on 29-Apr-2020 12:26:21

752 Views

Suppose we have an array of integers arr and two integers k and threshold. We have to find the number of sub-arrays of size k and average greater than or equal to the threshold. So if the input is like: [2, 2, 2, 2, 5, 5, 5, 8] and k = 3 and threshold = 4, then the output will be 3. Because the subarrays [2, 5, 5], [5, 5, 5] and [5, 5, 8] has the averages 4, 5 and 6 respectively.To solve this, we will follow these steps −sum := 0, div := k and n := number ... Read More

Course Schedule in Python

Arnab Chakraborty
Updated on 29-Apr-2020 12:25:39

1K+ Views

Suppose there are a total of numCourses courses we have to take, labeled from 0 to numCourses-1. Some courses may have prerequisites, for example to take course 0 we have to first take course 1, which is expressed using a pair: [0, 1]. Suppose there are total number of courses that is provided and a list of prerequisite pairs, we have to check whether is it possible for you to finish all courses?So if the input is like − numCourses = 2 and prerequisites = [[1, 0]], then the result will be true, because there are a total of 2 ... Read More

Word Ladder in C++

Arnab Chakraborty
Updated on 29-Apr-2020 12:23:44

880 Views

Suppose we have two words (beginWord and endWord), and we have dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord, such that −Only one letter can be converted at a time.In each transformed word must exist in the word list. The beginWord is not a transformed word.We have to keep in mind that −Return 0 when there is no such change sequence.All words have the same length.All words contain only lowercase characters.We can assume no duplicates in the word list.So if the input is like: beginWord = "hit", endWord = "cog", and wordlist = ["hot", ... Read More

Find City with Smallest Number of Neighbors at Threshold Distance in C++

Arnab Chakraborty
Updated on 29-Apr-2020 12:19:58

520 Views

Suppose there are n cities numbered from 0 to n-1. If we have the array edges where edges[i] = [fromi, toi, weighti] represents a bidirectional and weighted edge between cities fromi and toi, and given the integer distance threshold. We have to find the city with the smallest number of cities that are reachable through some path and whose distance is at most distance threshold, If there are more than one such cities, return the city with the greatest number.So if the input is like −n is 4 and the distance threshold is also 4, then the output will be ... Read More

Advertisements