Category: Greedy

Minimum number of deques required to make the array sorted

#include using namespace std;int minDeques(vector arr){            vector fronts, backs;        for (int i = 0; i < arr.size(); i++) {                        bool hasBeenPushed = false;        for (int j = 0; j < fronts.size(); j++) {                                    if (arr[i] < fronts[j]) {                bool isSafe = true;                for (int k = 0; k < arr.size(); k++) {                    if (arr[i] < arr[k]                        && arr[k] < fronts[j]) {                        isSafe = false;                        break;                    }                }                                                if (isSafe) […]

Continue Reading

Check if any row of the matrix can be converted to the elements present in the target row

  import java.util.*;  class GFG {                  public static boolean checkPossibleOrNot(        int[][] mat, int[] target)    {                  int[] comp = new int[mat[0].length];                          Arrays.fill(comp, Integer.MIN_VALUE);                  for (int[] val : mat) {                                                  if (val[0]

Continue Reading

Minimum adjacent swaps required to get Kth smallest number greater than given number

#include using namespace std;  void minSwapsKthLargest(string str, int k){        string res = str;              for (int i = 0; i < k; i++) {        next_permutation(            str.begin(), str.end());    }          int swap_count = 0;              for (int i = 0; i < res.length(); i++) {                  if (res[i] != str[i]) {            int start = i + 1;                          while (res[i] != str[start]) {                                  start++;            }            while (i != start) {                swap(str[start], str[start - 1]);                                                  start--;                swap_count++;            }        }    }          cout

Continue Reading

Maximum length of consecutive 1s or 0s after flipping at most K characters

#include using namespace std;  int maxLength(string str, int n,              char c, int k){        int ans = -1;          int cnt = 0;          int left = 0;      for (int right = 0; right < n; right++) {          if (str[right] == c) {            cnt++;        }                  while (cnt > k) {            if (str[left] == c) {                cnt–;            }                                      left++;        }                          ans = max(ans, right – left + 1);    }      return ans;}  int maxConsecutiveSegment(string S, int K){    int N = […]

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