Category: Heap

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

Maximize average of the ratios of N pairs by M increments

  #include using namespace std;  double change(int pass, int total){    double currentPassRatio, newPassRatio;    double increase;          currentPassRatio = ((double)pass)                       / total;          newPassRatio = ((double)(pass + 1))                   / (total + 1);          increase = newPassRatio               – currentPassRatio;          return increase;}  double maximumAverage(    vector v, int M,    int N){        double sum = 0;      double increase, average;              priority_queue pq;    for (int i = 0; i < N; i++) {                          increase = change(v[i][0], v[i][1]);                          pq.push({ increase, i });                  average = v[i][0] * 1.0 […]

Continue Reading

Minimum time required to schedule K processes

  import java.util.*;import java.lang.*;  class GFG {              static int minTime(int[] A, int n, int K)    {                int max_ability = A[0];                  for (int i = 1; i < n; i++) {            max_ability = Math.max(                max_ability, A[i]);        }                  int tmp[] = new int[max_ability + 1];                          int count = 0;                  for (int i = 0; i < n; i++) {            tmp[A[i]]++;        }                  for (int i = max_ability;             i >= 0; i–) {              if (tmp[i] != […]

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