Monday, 13 November 2023

// Program to convert recursive fibonacci into dp by tabulation method(bottom-up)

// tc= O(n)

// sc= O(n) = extra array space respectively (stack space is removed)


import java.util.*;

public class dp {

    public static void main(String[] args) {

       int n = 5;

       int dp[] = new int[n+1];

       dp[0] = 0;

       dp[1] = 1;

       for(int i = 2; i<n+1;i++)

       {

        dp[i] = dp[i-2] + dp[i-1];

       }

       for (int i : dp) {

         System.out.print(i+" ");

       }

    }

}


No comments:

Post a Comment

Good thoughtful question on Binary search on answers

Problem link:  https://leetcode.com/problems/maximize-score-of-numbers-in-ranges/description/ Solution: //import java.util.Arrays; class So...