Maximum Number of Events That Can Be Attended in C++

Arnab Chakraborty
Updated on 29-Apr-2020 12:45:51

885 Views

Suppose we have an array of events where events[i] = [startDayi, endDayi]. Here every event I start at startDayi and ends at endDayi. We can attend an event I at any day d where d in range startTimei and endTimei (both inclusive). We have to keep in mind that we can only attend one event at any time. So find the maximum number of events we can attend. So for example, if the input is like [[1, 4], [4, 4], [2, 2], [3, 4], [1, 1]], then the output will be 1, as we can attend maximum of four events, ... Read More

Product of the Last K Numbers in C++

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

379 Views

Suppose we have the implement the class called ProductOfNumbers that supports two methods −add(int num): This adds the number num to the back of the current list of numbers.getProduct(int k): This returns the product of the last k numbers in the current list.We can assume that always the current list has at least k numbers. So for example, if the input is like − add(3), add(0), add(2), add(5), add(4), getProduct(2), getProduct(3), getProduct(4), add(8), getProduct(2), then the output will be (after each function call) −[3], [3, 0], [3, 0, 2], [3, 0, 2, 5], [3, 0, 2, 5, 4], then (5 ... Read More

Minimum Steps to Make Two Strings Anagram in C++

Arnab Chakraborty
Updated on 29-Apr-2020 12:37:48

296 Views

Suppose we have two equal-size strings s and t. In one step we can choose any character of t and replace it with another character. We have to find the minimum number of steps required to make t an anagram of s. Note: An Anagram of a string is a string that contains the same characters with a different (or the same) ordering.So if the input is like - “yxy” and “xyx”, then the output will be 1, as only one character is needed to be replaced.To solve this, we will follow these steps −n := size of characters in ... Read More

Insert, Delete, GetRandom O(1) in Python

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

591 Views

Suppose we have a data structure that supports all following operations in average O(1) time.insert(val) − This will Insert an item val to the set if that is not already present.remove(val) − This will remove an item val from the set if it presents.getRandom − This will return a random element from current set of elements. Each element must have the same probability of being returned.To solve this, we will follow these steps −For the initialization, define a parent map, and elements arrayFor the insert() function, it will take val as inputif val is not present in parent map, or ... Read More

Flatten Nested List Iterator in Python

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

467 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

471 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

372 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

516 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

382 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

Advertisements