Filip_Markoski

[ADSx] - DynamicProgrammingFun

Jan 29th, 2018
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 22.86 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3. import java.util.StringTokenizer;
  4.  
  5. class WeightedSum {
  6.  
  7.     public static int solve(int N, int K, int numbers[]) {
  8.         int UNDEFINED = -1;
  9.         int matrix[][] = new int[N][K+1];
  10.         int result = 0;
  11.         for(int i = 0; i < N; i++){
  12.             matrix[i][1] = numbers[i];
  13.         }
  14.         for (int i = 1; i < N; i++) { // for numbers[]
  15.             for (int j = 2; j < K+1; j++) {  // for the sliding window, which to choose
  16.                 for (int k = 0; k < i; k++) { // for tabular lookup
  17.                     if (matrix[i][j] == UNDEFINED) {
  18.                         //if (matrix[k][j - 1] != UNDEFINED) {}
  19.                         matrix[i][j] = matrix[k][j - 1] + numbers[i] * j;
  20.  
  21.                     } else if (matrix[i][j] != UNDEFINED) {
  22.                         /* Meaning it is defined */
  23.                         matrix[i][j] = Math.max(matrix[i][j], matrix[k][j - 1] + numbers[i] * j);
  24.                     }
  25.                 }
  26.                 result = Math.max(result, matrix[i][j]);
  27.             }
  28.  
  29.         }
  30.  
  31.         return result;
  32.     }
  33.  
  34.     public static void main(String args[]) {
  35.         String text = "10 3\n" +
  36.                 "1 9 2 3 6 1 3 2 1 3";
  37.         Scanner scanner = new Scanner(text);
  38.         int N = scanner.nextInt();
  39.         int K = scanner.nextInt();
  40.         scanner.nextLine(); // fire off empty
  41.  
  42.         int array[] = new int[N];
  43.         for (int i = 0; i < N; i++) {
  44.             array[i] = scanner.nextInt();
  45.         }
  46.  
  47.         System.out.println(solve(N, K, array));
  48.     }
  49. }
  50.  
  51. class AlchniVitezi {
  52.  
  53.     static int maxZlatnici(int[] zlatnici, int N) {
  54.  
  55.         int[][] maxzlatnici = new int[N][N - 1];
  56.         for (int i = 0; i < N; i++) {
  57.             maxzlatnici[i][0] = zlatnici[i];
  58.             if (N > 2) {
  59.                 if (i != (N - 1))
  60.                     maxzlatnici[i][1] = zlatnici[i + 1];
  61.                 else
  62.                     maxzlatnici[i][1] = zlatnici[0];
  63.             }
  64.         }
  65.  
  66.         for (int i = 0; i < N; i++) {
  67.             for (int j = 2; j < N - 1; j++) {
  68.                 maxzlatnici[i][j] = 0;
  69.                 for (int k = 0; k < j - 1; k++) {
  70.                     if (maxzlatnici[i][j] < zlatnici[(i + j) % N] + maxzlatnici[i][k])
  71.                         maxzlatnici[i][j] = zlatnici[(i + j) % N] + maxzlatnici[i][k];
  72.                 }
  73.                 printMatrix(maxzlatnici);
  74.             }
  75.             if (i > 0) {
  76.                 if (maxzlatnici[i][N - 2] < maxzlatnici[i - 1][N - 2])
  77.                     maxzlatnici[i][N - 2] = maxzlatnici[i - 1][N - 2];
  78.             }
  79.  
  80.         }
  81.  
  82.         printMatrix(maxzlatnici);
  83.  
  84.         return maxzlatnici[N - 1][N - 2];
  85.     }
  86.  
  87.     static void printMatrix(int matrix[][]) {
  88.         for (int i = 0; i < matrix.length; i++) {
  89.             for (int j = 0; j < matrix[0].length; j++) {
  90.                 System.out.printf("%5d", matrix[i][j]);
  91.             }
  92.             System.out.println();
  93.         }
  94.         System.out.println();
  95.     }
  96.  
  97.     public static void main(String[] args) throws Exception {
  98.         String text = "6\n" +
  99.                 "10 3 2 5 7 8\n";
  100.         //Scanner scanner = new Scanner(System.in);
  101.         Scanner scanner = new Scanner(text);
  102.         int N = Integer.parseInt(scanner.nextLine());
  103.         StringTokenizer st = new StringTokenizer(scanner.nextLine());
  104.         int zlatnici[] = new int[N];
  105.         for (int i = 0; i < N; i++) {
  106.             zlatnici[i] = Integer.parseInt(st.nextToken());
  107.         }
  108.  
  109.         System.out.println(maxZlatnici(zlatnici, N));
  110.     }
  111.  
  112. }
  113.  
  114. public class DynamicProgrammingFun {
  115.     public static void main(String args[]) {
  116.         MinimumCostPath minimumCostPath = new MinimumCostPath();
  117.         int cost[][] = {{1, 2, 3},
  118.                 {4, 8, 2},
  119.                 {1, 5, 3}};
  120.         System.out.println("minimum cost to reach (2,2) = " +
  121.                 minimumCostPath.minCost(cost, 2, 2)
  122.         );
  123.  
  124.     }
  125. }
  126.  
  127. class Knapsack {
  128.     /**
  129.      * https://www.geeksforgeeks.org/knapsack-problem/
  130.      */
  131.  
  132.     /* A Naive recursive implementation of 0-1 Knapsack problem */
  133.     // Returns the maximum value that can be put in a knapsack of capacity W
  134.     static int knapSackNaive(int W, int wt[], int val[], int n) {
  135.         // Base Case
  136.         if (n == 0 || W == 0)
  137.             return 0;
  138.  
  139.         // If weight of the nth item is more than Knapsack capacity W, then
  140.         // this item cannot be included in the optimal solution
  141.         if (wt[n - 1] > W)
  142.             return knapSackNaive(W, wt, val, n - 1);
  143.  
  144.             // Return the maximum of two cases:
  145.             // (1) nth item included
  146.             // (2) not included
  147.         else return Math.max(
  148.                 val[n - 1] + knapSackNaive(W - wt[n - 1], wt, val, n - 1),
  149.                 knapSackNaive(W, wt, val, n - 1)
  150.         );
  151.     }
  152.  
  153.     // Returns the maximum value that can be put in a knapsack of capacity W
  154.     static int knapSack(int W, int wt[], int val[], int n) {
  155.         int i, w;
  156.         int K[][] = new int[n + 1][W + 1];
  157.  
  158.         // Build table K[][] in bottom up manner
  159.         for (i = 0; i <= n; i++) {
  160.             for (w = 0; w <= W; w++) {
  161.                 if (i == 0 || w == 0)
  162.                     K[i][w] = 0;
  163.                 else if (wt[i - 1] <= w)
  164.                     K[i][w] = Math.max(
  165.                             val[i - 1] + K[i - 1][w - wt[i - 1]],
  166.                             K[i - 1][w]
  167.                     );
  168.                 else
  169.                     K[i][w] = K[i - 1][w];
  170.             }
  171.         }
  172.  
  173.         return K[n][W];
  174.     }
  175.  
  176.  
  177.     // Driver program to test above function
  178.     public static void main(String args[]) {
  179.         int val[] = new int[]{60, 100, 120};
  180.         int wt[] = new int[]{10, 20, 30};
  181.         int W = 50;
  182.         int n = val.length;
  183.         System.out.println(knapSack(W, wt, val, n));
  184.     }
  185. }
  186.  
  187. class BinomialCoefficient {
  188.     /**
  189.      * https://www.geeksforgeeks.org/dynamic-programming-set-9-binomial-coefficient/
  190.      */
  191.  
  192.     // Returns value of Binomial
  193.     // Coefficient C(n, k)
  194.     static int binomialCoeffSimple(int n, int k) {
  195.  
  196.         // Base Cases
  197.         if (k == 0 || k == n)
  198.             return 1;
  199.  
  200.         // Recur
  201.         return binomialCoeffSimple(n - 1, k - 1) +
  202.                 binomialCoeffSimple(n - 1, k);
  203.     }
  204.  
  205.     // Returns value of Binomial Coefficient C(n, k)
  206.     static int binomialCoeff(int n, int k) {
  207.         int C[][] = new int[n + 1][k + 1];
  208.         int i, j;
  209.  
  210.         // Calculate  value of Binomial Coefficient in bottom up manner
  211.         for (i = 0; i <= n; i++) {
  212.             for (j = 0; j <= Math.min(i, k); j++) {
  213.                 // Base Cases
  214.                 if (j == 0 || j == i)
  215.                     C[i][j] = 1;
  216.  
  217.                     // Calculate value using previosly stored values
  218.                 else
  219.                     C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
  220.             }
  221.         }
  222.  
  223.         return C[n][k];
  224.     }
  225.  
  226.     /* Driver program to test above function*/
  227.     public static void main(String args[]) {
  228.         int n = 5, k = 2;
  229.         System.out.println("Value of C(" + n + "," + k + ") is " + binomialCoeff(n, k));
  230.     }
  231. }
  232.  
  233. class UglyNumber {
  234.     /**
  235.      * https://www.geeksforgeeks.org/?p=753
  236.      */
  237.  
  238.     /*This function divides a by greatest divisible
  239.     power of b*/
  240.     int maxDivide(int a, int b) {
  241.         while (a % b == 0)
  242.             a = a / b;
  243.         return a;
  244.     }
  245.  
  246.     /* Function to check if a number is ugly or not */
  247.     int isUgly(int no) {
  248.         no = maxDivide(no, 2);
  249.         no = maxDivide(no, 3);
  250.         no = maxDivide(no, 5);
  251.  
  252.         return (no == 1) ? 1 : 0;
  253.     }
  254.  
  255.     /* Function to get the nth ugly number*/
  256.     int getNthUglyNoSimple(int n) {
  257.         int i = 1;
  258.         int count = 1; // ugly number count
  259.  
  260.         // check for all integers until count becomes n */
  261.         while (n > count) {
  262.             i++;
  263.             if (isUgly(i) == 1)
  264.                 count++;
  265.         }
  266.         return i;
  267.     }
  268.  
  269.     /* Function to get the nth ugly number*/
  270.     int getNthUglyNo(int n) {
  271.         int ugly[] = new int[n];  // To store ugly numbers
  272.         int i2 = 0, i3 = 0, i5 = 0;
  273.         int next_multiple_of_2 = 2;
  274.         int next_multiple_of_3 = 3;
  275.         int next_multiple_of_5 = 5;
  276.         int next_ugly_no = 1;
  277.  
  278.         ugly[0] = 1;
  279.  
  280.         for (int i = 1; i < n; i++) {
  281.             next_ugly_no = Math.min(next_multiple_of_2,
  282.                     Math.min(next_multiple_of_3,
  283.                             next_multiple_of_5));
  284.  
  285.             ugly[i] = next_ugly_no;
  286.             if (next_ugly_no == next_multiple_of_2) {
  287.                 i2 = i2 + 1;
  288.                 next_multiple_of_2 = ugly[i2] * 2;
  289.             }
  290.             if (next_ugly_no == next_multiple_of_3) {
  291.                 i3 = i3 + 1;
  292.                 next_multiple_of_3 = ugly[i3] * 3;
  293.             }
  294.             if (next_ugly_no == next_multiple_of_5) {
  295.                 i5 = i5 + 1;
  296.                 next_multiple_of_5 = ugly[i5] * 5;
  297.             }
  298.         } /*End of for loop (i=1; i<n; i++) */
  299.         return next_ugly_no;
  300.     }
  301.  
  302.     /* Driver program to test above functions */
  303.     public static void main(String args[]) {
  304.         UglyNumber obj = new UglyNumber();
  305.         int no = obj.getNthUglyNo(150);
  306.         System.out.println("150th ugly no. is " + no);
  307.     }
  308. }
  309.  
  310. class CoinChange {
  311.  
  312.     /**
  313.      * https://www.geeksforgeeks.org/dynamic-programming-set-7-coin-change/
  314.      * Time Complexity: O(m x n)
  315.      * <p>
  316.      * S - array holding the types of coins
  317.      * m - number of types of coins
  318.      * n - total amount of money
  319.      * S[i] - a certain type of coin, so n - S[i] is total amount minus certain coin
  320.      */
  321.     static long countWays(int S[], int m, int n) {
  322.         //Time complexity of this function: O(mn)
  323.         //Space Complexity of this function: O(n)
  324.  
  325.         // table[i] will be storing the number of solutions
  326.         // for value i. We need n+1 rows as the table is
  327.         // constructed in bottom up manner using the base
  328.         // case (n = 0)
  329.         long[] table = new long[n + 1];
  330.  
  331.         // Initialize all table values as 0
  332.         Arrays.fill(table, 0);   //O(n)
  333.  
  334.         // Base case (If given value is 0)
  335.         table[0] = 1;
  336.  
  337.         // Pick all coins one by one and update the table[]
  338.         // values after the index greater than or equal to
  339.         // the value of the picked coin
  340.         for (int i = 0; i < m; i++)
  341.             for (int j = S[i]; j <= n; j++)
  342.                 table[j] += table[j - S[i]];
  343.  
  344.         return table[n];
  345.     }
  346.  
  347.     // Driver Function to test above function
  348.     public static void main(String args[]) {
  349.         int arr[] = {1, 2, 3};
  350.         int m = arr.length;
  351.         int n = 4;
  352.         System.out.println(countWays(arr, m, n));
  353.     }
  354. }
  355.  
  356. class MinimumCostPath {
  357.  
  358.     /**
  359.      * https://www.geeksforgeeks.org/dynamic-programming-set-6-min-cost-path/
  360.      */
  361.  
  362.     /* A utility function that returns minimum of 3 integers */
  363.     private static int min(int x, int y, int z) {
  364.         if (x < y)
  365.             return (x < z) ? x : z;
  366.         else
  367.             return (y < z) ? y : z;
  368.     }
  369.  
  370.     /* Returns cost of minimum cost path from (0,0) to (m, n) in mat[R][C]*/
  371.     int minCostRecursive(int cost[][], int m, int n) {
  372.         if (n < 0 || m < 0)
  373.             return Integer.MAX_VALUE;
  374.         else if (m == 0 && n == 0)
  375.             return cost[m][n];
  376.         else
  377.             return cost[m][n] + min(minCostRecursive(cost, m - 1, n - 1),
  378.                     minCostRecursive(cost, m - 1, n),
  379.                     minCostRecursive(cost, m, n - 1));
  380.     }
  381.  
  382.     /**
  383.      * Time Complexity: O(m x n)
  384.      */
  385.  
  386.     public int minCost(int cost[][], int m, int n) {
  387.         int i, j;
  388.         int tc[][] = new int[m + 1][n + 1];
  389.  
  390.         tc[0][0] = cost[0][0];
  391.  
  392.         /* Initialize first column of total cost(tc) array */
  393.         for (i = 1; i <= m; i++)
  394.             tc[i][0] = tc[i - 1][0] + cost[i][0];
  395.  
  396.         /* Initialize first row of tc array */
  397.         for (j = 1; j <= n; j++)
  398.             tc[0][j] = tc[0][j - 1] + cost[0][j];
  399.  
  400.         /* Construct rest of the tc array */
  401.         for (i = 1; i <= m; i++)
  402.             for (j = 1; j <= n; j++)
  403.                 tc[i][j] = min(tc[i - 1][j - 1], // diagonally
  404.                         tc[i - 1][j], // one up
  405.                         tc[i][j - 1]) + cost[i][j]; // one left
  406.  
  407.         return tc[m][n];
  408.     }
  409.  
  410. }
  411.  
  412. class EDIST {
  413.  
  414.     /**
  415.      * https://www.geeksforgeeks.org/dynamic-programming-set-5-edit-distance/
  416.      */
  417.  
  418.     static int min(int x, int y, int z) {
  419.         if (x <= y && x <= z) return x;
  420.         if (y <= x && y <= z) return y;
  421.         else return z;
  422.     }
  423.  
  424.     // A Naive recursive Java program to find minimum number
  425.     // operations to convert str1 to str2
  426.     static int editDist(String str1, String str2, int m, int n) {
  427.         // If first string is empty, the only option is to
  428.         // insert all characters of second string into first
  429.         if (m == 0) return n;
  430.  
  431.         // If second string is empty, the only option is to
  432.         // remove all characters of first string
  433.         if (n == 0) return m;
  434.  
  435.         // If last characters of two strings are same, nothing
  436.         // much to do. Ignore last characters and get count for
  437.         // remaining strings.
  438.         if (str1.charAt(m - 1) == str2.charAt(n - 1))
  439.             return editDist(str1, str2, m - 1, n - 1);
  440.  
  441.         // If last characters are not same, consider all three
  442.         // operations on last character of first string, recursively
  443.         // compute minimum cost for all three operations and take
  444.         // minimum of three values.
  445.         return 1 + min(editDist(str1, str2, m, n - 1),    // Insert
  446.                 editDist(str1, str2, m - 1, n),   // Remove
  447.                 editDist(str1, str2, m - 1, n - 1) // Replace
  448.         );
  449.     }
  450.  
  451.     /**
  452.      * Tabulated solution of Edit Distance
  453.      * Time Complexity: O(m x n)
  454.      */
  455.     static int editDistDP(String str1, String str2, int m, int n) {
  456.         // Create a table to store results of subproblems
  457.         int dp[][] = new int[m + 1][n + 1];
  458.  
  459.         // Fill d[][] in bottom up manner
  460.         for (int i = 0; i <= m; i++) {
  461.             for (int j = 0; j <= n; j++) {
  462.                 // If first string is empty, only option is to
  463.                 // insert all characters of second string
  464.                 if (i == 0)
  465.                     dp[i][j] = j;  // Min. operations = j
  466.  
  467.                     // If second string is empty, only option is to
  468.                     // remove all characters of second string
  469.                 else if (j == 0)
  470.                     dp[i][j] = i; // Min. operations = i
  471.  
  472.                     // If last characters are same, ignore last char
  473.                     // and recur for remaining string
  474.                 else if (str1.charAt(i - 1) == str2.charAt(j - 1))
  475.                     dp[i][j] = dp[i - 1][j - 1];
  476.  
  477.                     // If last character are different, consider all
  478.                     // possibilities and find minimum
  479.                 else
  480.                     dp[i][j] = 1 + min(dp[i][j - 1],  // Insert
  481.                             dp[i - 1][j],  // Remove
  482.                             dp[i - 1][j - 1]); // Replace
  483.             }
  484.         }
  485.  
  486.         return dp[m][n];
  487.     }
  488.  
  489. }
  490.  
  491. class LongestCommonSubsequence {
  492.     /**
  493.      * https://www.geeksforgeeks.org/longest-common-subsequence/
  494.      */
  495.  
  496.  
  497.     /**
  498.      * A recursive implementation of LCS problem in java
  499.      * Returns length of LCS for X[0..m-1], Y[0..n-1]
  500.      * Time Complexity: O(2^n)
  501.      */
  502.     int lcsRecursive(char[] X, char[] Y, int m, int n) {
  503.         if (m == 0 || n == 0) return 0;
  504.         if (X[m - 1] == Y[n - 1]) return 1 + lcsRecursive(X, Y, m - 1, n - 1);
  505.         else return Math.max(lcsRecursive(X, Y, m, n - 1), lcsRecursive(X, Y, m - 1, n));
  506.     }
  507.  
  508.     /**
  509.      * Dynamic Programming Java implementation of LCS problem
  510.      * Returns length of LCS for X[0..m-1], Y[0..n-1]
  511.      * Time Complexity: O(m*n)
  512.      */
  513.     int lcsTabulation(char[] X, char[] Y, int m, int n) {
  514.         int L[][] = new int[m + 1][n + 1];
  515.  
  516.     /* Following steps build L[m+1][n+1] in bottom up fashion. Note
  517.          that L[i][j] contains length of LCS of X[0..i-1] and Y[0..j-1] */
  518.         for (int i = 0; i <= m; i++) {
  519.             for (int j = 0; j <= n; j++) {
  520.                 if (i == 0 || j == 0)
  521.                     L[i][j] = 0;
  522.                 else if (X[i - 1] == Y[j - 1])
  523.                     L[i][j] = L[i - 1][j - 1] + 1;
  524.                 else
  525.                     L[i][j] = Math.max(L[i - 1][j], L[i][j - 1]);
  526.             }
  527.         }
  528.         return L[m][n];
  529.     }
  530.  
  531.  
  532.     /**
  533.      * https://www.geeksforgeeks.org/printing-longest-common-subsequence/
  534.      */
  535.     // Returns length of LCS for X[0..m-1], Y[0..n-1]
  536.     void lcsPrint(String X, String Y, int m, int n) {
  537.         int[][] L = new int[m + 1][n + 1];
  538.  
  539.         // Following steps build L[m+1][n+1] in bottom up fashion. Note
  540.         // that L[i][j] contains length of LCS of X[0..i-1] and Y[0..j-1]
  541.         for (int i = 0; i <= m; i++) {
  542.             for (int j = 0; j <= n; j++) {
  543.                 if (i == 0 || j == 0)
  544.                     L[i][j] = 0;
  545.                 else if (X.charAt(i - 1) == Y.charAt(j - 1))
  546.                     L[i][j] = L[i - 1][j - 1] + 1;
  547.                 else
  548.                     L[i][j] = Math.max(L[i - 1][j], L[i][j - 1]);
  549.             }
  550.         }
  551.  
  552.         // Following code is used to print LCS
  553.         int index = L[m][n];
  554.         int temp = index;
  555.  
  556.         // Create a character array to store the lcsPrint string
  557.         char[] lcs = new char[index + 1];
  558.         lcs[index] = '\0'; // Set the terminating character
  559.  
  560.         // Start from the right-most-bottom-most corner and
  561.         // one by one store characters in lcsPrint[]
  562.         int i = m, j = n;
  563.         while (i > 0 && j > 0) {
  564.             // If current character in X[] and Y are same, then
  565.             // current character is part of LCS
  566.             if (X.charAt(i - 1) == Y.charAt(j - 1)) {
  567.                 // Put current character in result
  568.                 lcs[index - 1] = X.charAt(i - 1);
  569.  
  570.                 // reduce values of i, j and index
  571.                 i--;
  572.                 j--;
  573.                 index--;
  574.             }
  575.  
  576.             // If not same, then find the larger of two and
  577.             // go in the direction of larger value
  578.             else if (L[i - 1][j] > L[i][j - 1])
  579.                 i--;
  580.             else
  581.                 j--;
  582.         }
  583.  
  584.         // Print the lcsPrint
  585.         System.out.print("LCS of " + X + " and " + Y + " is ");
  586.         for (int k = 0; k <= temp; k++)
  587.             System.out.print(lcs[k]);
  588.     }
  589. }
  590.  
  591. class LongestIncreasingSubsequence {
  592.     /**
  593.      * https://www.geeksforgeeks.org/longest-increasing-subsequence/
  594.      */
  595.  
  596.     static int max_ref; // stores the LIS
  597.  
  598.     /**
  599.      * To make use of recursive calls, this function must return
  600.      * two things:
  601.      * 1) Length of LIS ending with element arr[n-1]. We use
  602.      * max_ending_here for this purpose
  603.      * 2) Overall maximum as the LIS may end with an element
  604.      * before arr[n-1] max_ref is used this purpose.
  605.      * The value of LIS of full array of size n is stored in
  606.      * max_ref which is our final result
  607.      */
  608.     static int _lisRecursive(int arr[], int n) {
  609.  
  610.         // base case
  611.         if (n == 1) return 1;
  612.  
  613.         // 'max_ending_here' is length of LIS ending with arr[n-1]
  614.         int res, max_ending_here = 1;
  615.  
  616.         /**
  617.          * Recursively get all LIS ending with arr[0], arr[1] ... arr[n-2].
  618.          * If arr[i-1] is smaller than arr[n-1], and
  619.          * max ending with arr[n-1] needs to be updated, then update it
  620.          * */
  621.         for (int i = 1; i < n; i++) {
  622.             res = _lisRecursive(arr, i);
  623.             if (arr[i - 1] < arr[n - 1] && res + 1 > max_ending_here)
  624.                 max_ending_here = res + 1;
  625.         }
  626.  
  627.         // Compare max_ending_here with the overall max. And
  628.         // update the overall max if needed
  629.         if (max_ref < max_ending_here)
  630.             max_ref = max_ending_here;
  631.  
  632.         // Return length of LIS ending with arr[n-1]
  633.         return max_ending_here;
  634.     }
  635.  
  636.     // The wrapper function for _lisRecursive()
  637.     static int lisRecursive(int arr[], int n) {
  638.         // The max variable holds the result
  639.         max_ref = 1;
  640.  
  641.         // The function _lisRecursive() stores its result in max
  642.         _lisRecursive(arr, n);
  643.  
  644.         // returns max
  645.         return max_ref;
  646.     }
  647.  
  648.     /**
  649.      * lisTabulated() returns the length of the longest increasing
  650.      * subsequence in arr[] of size n with time of O(n^2)
  651.      */
  652.     static int lisTabulated(int arr[], int n) {
  653.         int lis[] = new int[n];
  654.         int i, j, max = 0;
  655.  
  656.           /* Initialize LIS values for all indexes */
  657.         for (i = 0; i < n; i++)
  658.             lis[i] = 1;
  659.  
  660.            /* Compute optimized LIS values in bottom up manner */
  661.         for (i = 1; i < n; i++)
  662.             for (j = 0; j < i; j++)
  663.                 if (arr[i] > arr[j] && lis[i] < lis[j] + 1)
  664.                     lis[i] = lis[j] + 1;
  665.  
  666.            /* Pick maximum of all LIS values */
  667.         for (i = 0; i < n; i++)
  668.             if (max < lis[i])
  669.                 max = lis[i];
  670.  
  671.         return max;
  672.     }
  673.  
  674.     /**
  675.      * Additional C++ constructPrintLIS
  676.      * https://www.geeksforgeeks.org/construction-of-longest-increasing-subsequence-using-dynamic-programming/
  677.      */
  678.  
  679.     public static void main(String args[]) {
  680.         int arr[] = {10, 22, 9, 33, 21, 50, 41, 60};
  681.         int n = arr.length;
  682.         System.out.println("Length of lis is " + lisTabulated(arr, n) + "n");
  683.     }
  684.  
  685. }
  686.  
  687. class Fibonacci {
  688.     /**
  689.      * https://www.geeksforgeeks.org/dynamic-programming-set-1/
  690.      */
  691.  
  692.     /**
  693.      * Simple recursive program for Fibonacci numbers
  694.      */
  695.     public static int fibRecursive(int n) {
  696.         if (n <= 1)
  697.             return n;
  698.         return fibRecursive(n - 1) + fibRecursive(n - 2);
  699.     }
  700.  
  701.     final int MAX = 100;
  702.     final int NIL = -1;
  703.  
  704.     int lookup[] = new int[MAX];
  705.  
  706.     /* Function to initialize NIL values in lookup table */
  707.     void initialize() {
  708.         for (int i = 0; i < MAX; i++)
  709.             lookup[i] = NIL;
  710.     }
  711.  
  712.     /**
  713.      * Memoization (Top Down) = Similar to Recursion
  714.      */
  715.  
  716.     /* function for nth Fibonacci number */
  717.     int fibMemoization(int n) {
  718.         if (lookup[n] == NIL) {
  719.             if (n <= 1)
  720.                 lookup[n] = n;
  721.             else
  722.                 lookup[n] = fibMemoization(n - 1) + fibMemoization(n - 2);
  723.         }
  724.         return lookup[n];
  725.     }
  726.  
  727.     /**
  728.      * Tabulation (Bottom Up)
  729.      */
  730.  
  731.     int fibTabulation(int n) {
  732.         int f[] = new int[n + 1];
  733.         f[0] = 0;
  734.         f[1] = 1;
  735.         for (int i = 2; i <= n; i++)
  736.             f[i] = f[i - 1] + f[i - 2];
  737.         return f[n];
  738.     }
  739. }
Advertisement
Add Comment
Please, Sign In to add comment