Monday 13 November 2023

// Frog Jump via Bottom-up(Tabulation)

// tc = O(n)

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

import java.util.*;


public class dp {

     

    public static void main(String[] args) {

        int height[]={30,10,60,10,60,50};

        int n = 5; // or when --> n = height.length

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

        Arrays.fill(dp,-1);

        dp[0] = 0;

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

        {

            int left_jmpOne = dp[i-1] + Math.abs(height[i] - height[i-1]);

            int right_jmpTwo = Integer.MAX_VALUE;

            if(i>1){

            right_jmpTwo = dp[i-2] + Math.abs(height[i] - height[i-2]);

             }

            dp[i] = Math.min(left_jmpOne,right_jmpTwo);

        }

        System.out.println(dp[n]); // then n-1

    }

}


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