Saturday, 4 Dec 2021
Category: Dynamic Programming

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 #define SIZE 100005using namespace std;  void preprocess(string& s, string& t,                int prefix[][SIZE],                int n, int i){        prefix[i][0] = (s[0] != t[0]);      for (int j = 1; j < n; j++) {                                  prefix[i][j]            = prefix[i][j - 1]              + (s[j] != t[j % 3]);    }    return;}  void minChangesNonPalindrome(    string str, int N, int Q,    vector queries){          int prefix[6][SIZE];                  vector sequences        = { "012", "021", "102",            "120", "201", "210" };      for (int i = 0; […]

#include using namespace std;  #define M 100000  vector tree(4 * M + 1);  void update_tree(int start, int end,                 int update_idx, int length_t,                 int count_c, int idx){            if (start == end        && start == update_idx) {        tree[idx].first            = max(tree[idx].first, length_t);        tree[idx].second = count_c;        return;    }          if (update_idx < start        || end < update_idx) {        return;    }          int mid = (start + end) / 2;      update_tree(start, mid, update_idx,                length_t, count_c,                2 * idx);    update_tree(mid + 1, end, update_idx,                length_t, […]

Given an array arr[] of N integers, the task is to find the maximum count of K, i.e, consecutive integers from 0 to K, that is present in the set S, where S contains all the possible subset-sum values of the array arr[].Examples:Attention reader! Don’t stop learning now. Get hold of all the important DSA […]

#include using namespace std;  int totalValidArrays(int a[], int b[],                     int N){        int dp[N + 1][b[N – 1] + 1];          int pref[N + 1][b[N – 1] + 1];          memset(dp, 0, sizeof(dp)),        memset(pref, 0, sizeof(pref));          dp[0][0] = 1;          for (int i = 0; i

#include using namespace std;#define ar array  ar expand(int idx, vector& A){                  int l = idx – 1;    int r = idx + 1;              while (l >= 0) {          if ((int)(A[idx]) > A[l]) {            –l;        }        else {            break;        }    }      if (l < 0 || l == idx) {        l = -2;    }              while (r < (int)(A.size())) {        if ((int)A[idx] > A[r]) {            ++r;        }        else {            break;        }    }      if (r >= (int)(A.size()) || r == idx) […]

const INT_MAX = 2147483647;                    const minZeroeCount = (S, K, Q) => {                let N = S.length;                        if (K == 0 || Q == 0) {            let cnt0 = 0;            for (let i = 0; i < N; ++i) {                if (S[i] == '0') cnt0++;            }            return cnt0;        }                        if (Q >= Math.ceil(N / K)) {            return 0;        }                                const dp = new Array(N + 1).fill(INT_MAX).map(() => new Array(Q […]