Category: priority-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

Maximize sum of ratios of N given fractions by incrementing numerator and denominators K times by 1

  #include using namespace std;  double maxAverageRatio(    vector& arr, int K){        int N = arr.size();          priority_queue q;          for (int i = 0; i < N; i++) {                                  double extra            = (((double)arr[i][0] + 1)               / ((double)arr[i][1] + 1))              - ((double)arr[i][0]                 / (double)arr[i][1]);        q.push(make_pair(extra, i));    }          while (K--) {        int i = q.top().second;        q.pop();                          arr[i][0] += 1;        arr[i][1] += 1;                  double extra            = (((double)arr[i][0] + 1)               / ((double)arr[i][1] + 1))              - ((double)arr[i][0]                 / (double)arr[i][1]);          q.push(make_pair(extra, i));    }          double ans = 0;    for (int i […]

Continue Reading

Rabbit House | Google Kickstart 2021 Round A

#include using namespace std;  void solve(long long int r, long long int c,           vector& grid){    priority_queue        pq;      for (long long int i = 0; i < r; i++) {        for (long long int j = 0; j < c; j++) {            pq.push(make_pair(grid[i][j],                              make_pair(i, j)));        }    }      long long int res = 0;      while (!pq.empty()) {        long long int height = pq.top().first,                      i = pq.top().second.first,                      j = pq.top().second.second;        pq.pop();        if (height != grid[i][j])            continue;        if […]

Continue Reading

Indexed Priority Queue with Implementation

  #include using namespace std;  template   class indexed_priority_queue {          unordered_map m;          vector v;          long long numberOfElement;          Comparator comp;          long long capacity = LLONG_MAX;          long long int getValueIndex(T1 key)    {        if (m[key] == 0) {            cout

Continue Reading

Maximize product of sum of speeds of K workers and their minimum efficiency

#include using namespace std;  void generateArrayofPairs(int n, vector& speed,                          vector& efficiency,                          vector& arr){      for (int i = 0; i < n; i++) {          arr[i] = { efficiency[i], speed[i] };    }      sort(arr.rbegin(), arr.rend());}  int maximizePerformance(vector& speed, int K,                        vector& efficiency){      int n = speed.size();    vector arr(n);              generateArrayofPairs(n, speed,                         efficiency, arr);          priority_queue        pq;          int ans = 0;    int SumOfSpeed = 0;          for (auto& it : arr) {          int e = it.first;        int s = it.second;                  SumOfSpeed += s;                  pq.push(s);                          if […]

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

Path from a given source to a given destination having Kth largest weight in a Graph

   import java.io.*; import java.util.*;    class Edge {             int src;             int nbr;             int wt;             Edge(int src, int nbr, int wt)     {         this.src = src;         this.nbr = nbr;         this.wt = wt;     } }    class Pair implements Comparable {             int wsf;             String psf; […]

Continue Reading

Merge transactions in bank sheets in the order of their occurrence such that their sum remains positive

   import java.util.*;    class Node {        int val;     Node next;             Node(int val)     {         this.val = val;         this.next = null;     } }    class GFG {             public static void mergeSheets(         Node lists[])     {                  PriorityQueue pq                = new PriorityQueue(                 new Comparator() {                                                                  public int compare(Node a, […]

Continue Reading