Walls and Gates in C++

Arnab Chakraborty
Updated on 18-Nov-2020 11:56:50

558 Views

Suppose we have one m x n 2D grid, and that is initialized with these three possible values.-1 for a wall or an obstacle.0 for a gate.INF This is infinity means an empty room.Here 2^31 - 1 = 2147483647 is INF as we may assume that the distance to a gate is less than 2147483647. Fill each empty room with the distance to its nearest gate. If it is impossible to reach a gate, it should be filled with INF.So, if the input is likeINF-10INFINFINFINF-1INF-1INF-10-1INFINFthen the output will be3-101221-11-12-10-134To solve this, we will follow these steps −Define an array dir ... Read More

Inorder Successor in BST in C++

Arnab Chakraborty
Updated on 18-Nov-2020 11:53:50

172 Views

Suppose we have a binary search tree and a node in it, we have to search the in-order successor of that node in the BST. As we know that the successor of a node p is the node with the smallest key greater than p.val.So, if the input is like root = [2, 1, 3], p = 1, then the output will be 2, To solve this, we will follow these steps −Define recursive method inorderSuccessor(), this will take root and pif root null, then −return nullif val of root val val){          return inorderSuccessor(root->right, p);   ... Read More

Zigzag Iterator in C++

Arnab Chakraborty
Updated on 18-Nov-2020 11:51:10

284 Views

Suppose there are two 1D arrays, we have to implement an iterator that will return their elements alternately. There will be two methods −next() − to get next elementhasNext() − to check whether the next element is present or not.So, if the input is like v1 = [1, 2] v2 = [3, 4, 5, 6] , then the output will be [1, 3, 2, 4, 5, 6], To solve this, we will follow these steps −Define one queue q of pairsFrom the initializer ake two arrays v1 and v2, if size of v1, then −insert { 0, 0 } into ... Read More

Wiggle Sort in C++

Arnab Chakraborty
Updated on 18-Nov-2020 11:48:16

1K+ Views

Suppose we have an unsorted array called nums, we have to reorder it in-place such that nums[0] = nums[2] nums[i+1] is true or i is odd and nums[i] > nums[i+1] is false, thenswap(nums[i], nums[i + 1])Example Let us see the following implementation to get better understanding − Live Demo#include using namespace std; void print_vector(vector

Find the Celebrity in C++

Arnab Chakraborty
Updated on 18-Nov-2020 11:46:42

186 Views

Suppose we have n people (labeled from 0 to n - 1) and among them, there may exist one celebrity. We can say a person x is a celebrity when all the other n - 1 people know x but x does not know any of them. Here we have to find who the celebrity is or verify that there is not one.We are allowed to ask only one question to person ‘A’, that "Hi, A. Do you know B?" to get information of whether A knows B or not. We have to ask minimum number of questions tofind out ... Read More

Encode and Decode Strings in C++

Arnab Chakraborty
Updated on 18-Nov-2020 11:44:30

5K+ Views

Suppose we have a list of strings. We have to design an algorithm that can encode the list of strings to a string. We also have to make one decoder that will decode back to the original list of strings. Suppose we have the encoder and decoder installed on these machines, and there are two different functions as follows −Machine 1 (sender) has the functionstring encode(vector

Palindrome Permutation II in C++

Arnab Chakraborty
Updated on 18-Nov-2020 11:41:53

231 Views

Suppose we have a string s, we have to find all the palindromic permutations of it, there will be no repetition. If no palindromic permutation is there, then simply return empty string.So, if the input is like "aabb", then the output will be ["abba", "baab"]To solve this, we will follow these steps −Define an array retDefine a function solve(), this will take s, sz, one unordered map m, idx initialize it with 0, if sz is same as 0, then −insert s at the end of retreturnevenFound := falseDefine one set visitedfor each key-value pair it of m, do −if ... Read More

Graph Valid Tree in C++

Arnab Chakraborty
Updated on 18-Nov-2020 11:39:10

318 Views

Suppose we have n nodes they are labeled from 0 to n-1 and a list of undirected edges [u, v], We have to define a function to check whether these edges make up a valid tree or not.So, if the input is like n = 5, and edges = [[0, 1], [0, 2], [0, 3], [1, 4]], then the output will be trueTo solve this, we will follow these steps −Define a function dfs(), this will take node, par, graph, and another array called visited, if visited[node] is same as 1, then −return trueif visited[node] is same as 2, then ... Read More

Factor Combinations in C++

Arnab Chakraborty
Updated on 18-Nov-2020 11:33:01

288 Views

Suppose we have a number. The numbers can be regarded as a product of its factors. So, 8 = 2 x 2 x 2; = 2 x 4. We have to make one function that takes an integer n and return all possible combinations of its factors.So, if the input is like 12, then the output will be [[2, 6], [2, 2, 3], [3, 4]]To solve this, we will follow these steps −Define a function solve(), this will take n, target, start, define one list of lists called retif n is same as 1, then −return retif n is not ... Read More

Meeting Rooms II in C++

Arnab Chakraborty
Updated on 18-Nov-2020 11:30:42

1K+ Views

Suppose there is an array of meeting time intervals. There are two times start and end times [[s1,e1],[s2,e2],...] and each pair satisfies the rule (si < ei), We have to find the minimum number of conference rooms required.So, if the input is like [[0, 30], [5, 10], [15, 20]], then the output will be 2.To solve this, we will follow these steps −define one priority queue pqsort the intervals arrayret := 0for initialize i := 0, when i < size of intervals, update (increase i by 1), do −while (not pq is empty and top element of pq

Advertisements