Category: Dynamic Programming

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

Minimum characters to be replaced in Ternary string to remove all palindromic substrings for Q queries

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

Continue Reading

Length of Longest Increasing Subsequences (LIS) using Segment Tree

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

Continue Reading

Count of non decreasing Arrays with ith element in range [A[i], B[i]]

#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

Continue Reading

Minimum jumps required to reach all array elements using largest element

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

Continue Reading

Minimize count of 0s in Binary String by changing K-size substring to 1s at most Q times

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

Continue Reading