Category: Queue

Maximum sum of at most two non-overlapping intervals in a list of Intervals | Interval Scheduling Problem

  import java.util.*;  class GFG {          public static void main(String[] args)    {        int[][] interval            = { { 1, 3, 2 },                { 4, 5, 2 },                { 1, 5, 5 } };        int maxValue            = maxTwoNonOverLapping(interval);        System.out.println(maxValue);    }                  public static int maxTwoNonOverLapping(        int[][] interval)    {                        Arrays.sort(interval,                    (a, b) -> a[0] – b[0]);                                          PriorityQueue pq            = new PriorityQueue(                (a, b) -> a[0] – b[0]);                          int max = 0;        int ans = 0;                  for (int[] e : interval) {            while (!pq.isEmpty()) […]

Continue Reading

Queue Operations

using System;using System.Collections.Generic;public class GFG {     public class Geeks {           public void insert(Queue q, int k)    {                q.Enqueue(k);    }            public int findFrequency(Queue q, int k)    {                int cntFrequency = 0;                int size = q.Count;                while (size– != 0) {                                    int x = q.Peek();            q.Dequeue();                                    if (x == k) {                                cntFrequency++;            }                                    q.Enqueue(x);        }                return cntFrequency;    }}        public static void Main()    {                       Queue q = new Queue();        int N = 8;        int[] a = new int[] { 1, 2, 3, […]

Continue Reading

Minimize steps required to convert number N to M using arithmetic operators

  #include using namespace std;  string changeNtoM(int N, int M){        if (N == M) {        return ” “;    }          if (M == 0) {        return “-“;    }          queue q;          unordered_map visited;          q.push({ N, “” }), visited[N] = 1;          q.push({ 1, “/” }), visited[1] = 1;          while (!q.empty()) {                pair cur = q.front();        q.pop();                                  if (cur.first == M) {                        return cur.second;        }                          if (!visited[cur.first + cur.first]            && cur.first + cur.first

Continue Reading

Trapping Rain Water in a Matrix

#include using namespace std;  vector dir    = { { -1, 0 }, { 0, 1 }, { 1, 0 }, { 0, -1 } };  struct node {      int height;    int x, y;};  struct Compare {          bool operator()(node const& a, node const& b)    {        return a.height > b.height;    }};  int trapRainWater(vector& heightMap){    int M = heightMap.size();    int N = heightMap[0].size();              vector visited(M,                                  vector(N, false));          priority_queue pq;          for (int i = 0; i […]

Continue Reading

Test Case Generation | Set 6 (Random Unweighted Binary Tree)

  #include using namespace std;    vector generateBinaryTree(int n){                        vector options = { 0, 1, 2 };                        map mp;                queue q;            q.push(1);            vector v;                int count = 1;                mp[1] = 1;            while (!q.empty() or count < n) {                                            int front;                  if(!q.empty()) {                                                    front = q.front();                                                                q.pop();        }                                                          int numberOfChilds          = options[rand() % (options.size())];                            if (count >= n)            continue;                                        while (numberOfChilds–) {                                                                int child = rand() % n + 1;                                        while (mp[child]) {                child++;                if (child > n) {                    child = 1;                }            }                                        count++;                            mp[child] […]

Continue Reading

Sum of nodes in a Binary Search Tree with values from a given range

#include using namespace std;class Node {public:    int val;    Node *left, *right;};Node* newNode(int item){    Node* temp = new Node();    temp->val = item;    temp->left = temp->right = NULL;        return temp;}int sum = 0;int rangeSumBST(Node* root, int low,                int high){        if (root == NULL)        return 0;            queue q;            q.push(root);        while (q.empty() == false) {                        Node* curr = q.front();        q.pop();                        if (curr->val >= low            && curr->val val;        }                        if (curr->left != NULL            && curr->val > low)                        q.push(curr->left);                        if (curr->right != […]

Continue Reading