Wednesday, 1 Dec 2021
Category: priority-queue

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()) […]

#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 […]

#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 […]

#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

#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 […]

#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 […]

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; […]

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, […]