Friday 17 November 2023

 // Grid Unique Paths-->2-D dp concept(space optimised)

// tc = O(mxn) 

// sc = O(n) --> array


import java.util.*;


class dp {

     public static int f(int m, int n)

     {

        int prev[] = new int[n];

        for(int i=0;i<m;i++)

        {

            int temp[] = new int[n];

            for(int j=0;j<n;j++)

            {

                if(i==0&&j==0) {temp[j] = 1;continue;}

            else

            {

                int up=0;int left=0;

               if(i>0)

               up = prev[j];

                if(j>0)

                left = temp[j - 1];

                temp[j] = up + left;

            }

            }

            prev = temp;

        }

        return prev[n - 1];

     }


    public static void main(String args[]) {

        int m = 3;

        int n = 2;

        System.out.println(f(m,n));

    }

}


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...