Suppose we have to design an Iterator class, that consists of few operations −Define a constructor that takes a string characters of sorted distinct lowercase English letters and a number combinationLength as parameter.Define a function next() that returns the next combination of length combinationLength in alphabetic order.Define another function hasNext() that returns True if and only if there exists a next combination.So if the input is like −CombinationIterator iterator = new CombinationIterator("xyz", 2); iterator.next(); // returns "xy" iterator.hasNext(); // returns true iterator.next(); // returns "xz" iterator.hasNext(); // returns true iterator.next(); // returns "yz" iterator.hasNext(); // returns falseTo solve this, we ... Read More
Suppose we have an array called people. Now the i-th person has weight people[i], and each boat can carry a maximum weight of limit. If each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. We have to find the minimum number of boats to carry every given person. So if the input is like [3,2,1,2], and limit is 3, then we need three boats. [(1,2), (2), (3)].To solve this, we will follow these steps −sort the people arrayi := 0, j := size of people array – 1, ret := 0while i
Suppose we have two players Alex and Lee they play a game with piles of stones. There are an even number of piles that are arranged in a row, and each pile has some number of stones piles[i]. The objective of the game is to end with the most stones. When the total number of stones is odd, there are no ties. Alex and Lee take turns, with Alex starting first. In each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This will be continued until there are no ... Read More
Suppose we have N piles of bananas, the i-th pile has piles[i] bananas. Here the guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed is K. Each hour, she takes some pile of bananas, and eats K bananas from that pile. If the pile has less than K bananas, then she consumes all of them instead, and won't eat any more bananas during this hour. Consider Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. We have to find the minimum integer ... Read More
Suppose we have an array of integers A. We have to find the sum of min(B), where B ranges over every (contiguous) subarray of A. Since the answer may be very large, then return the answer in modulo 10^9 + 7. So if the input is like [3, 1, 2, 4], then the output will be 17, because the subarrays are [3], [1], [2], [4], [3, 1], [1, 2], [2, 4], [3, 1, 2], [1, 2, 4], [3, 1, 2, 4], so minimums are [3, 1, 2, 4, 1, 1, 2, 1, 1, 1], and the sum is 17.To solve ... Read More
Suppose a full binary tree is a binary tree where each node has exactly 0 or 2 children. So we have to find a list of all possible full binary trees with N nodes. Each node of each tree in the answer must have node.val = 0. The returned trees can be in any order. So if the input is 7, then the trees are −To solve this, we will follow these steps −Define a map m of integer type key and tree type value.define a method called allPossibleFBT(), this will take N as inputis N is 1, then create ... Read More
Suppose we have a sequence X_1, X_2, ..., X_n is fibonacci-like if −n >= 3X_i + X_{i+1} = X_{i+2} for all i + 2 = 3 otherwise return 0.Let us see the following implementation to get better understanding −Example Live Demo#include using namespace std; class Solution { public: int lenLongestFibSubseq(vector & A) { int ret = 0; unordered_map m; int n = A.size(); vector < vector > dp(n, vector (n)); for(int i = 0; i < n; i++){ ... Read More
Suppose we have a binary tree rooted at root, the depth of each node is the shortest distance to the root. Here a node is deepest if it has the largest depth possible among any node in the entire tree. The subtree of a node is that node, plus the set of all descendants of that node. We have to find the node with the largest depth such that it contains all the deepest nodes in its subtree. So if the tree is like −Then the deepest subtree will be −To solve this, we will follow these steps −Define a ... Read More
Suppose we have an integer array. All elements in that array is unique. A maximum tree building on this array is defined as follow −The root will hold the maximum number in the array.The left subtree is the maximum tree constructed from left side of the subarray divided by the maximum number.The right subtree is the maximum tree constructed from right side of subarray divided by the maximum number.We have to construct the maximum binary tree. So if the input is like: [3, 2, 1, 6, 0, 5], then the output will be −To solve this, we will follow these ... Read More
Suppose we have a binary tree, we have to find the largest elements of each level of that tree. So if the tree is like −Then the output will be [3, 5, 8]To solve this, we will follow these steps −Define an array called ansdefine a recursive function solve(), this will take tree node, and level, the level is initially 0. this method will act like −if node is null, then returnif level = size of ans, then insert node value into ans, otherwise ans[level] := max of ans[level] and node valuecall solve(left subtree of node, level + 1)call solve(right ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP