Here is the dynamic programming problem,
There is a frog on the ‘1st’ step of an ‘N’ stairs long staircase. The frog wants to reach the ‘Nth’ stair. ‘HEIGHT[i]’ is the height of the ‘(i+1)th’ stair.If Frog jumps from ‘ith’ to ‘jth’ stair, the energy lost in the jump is given by absolute value of ( HEIGHT[i-1] – HEIGHT[j-1] ). If the Frog i
I solved this using memoization technique. But i’m facing this issue, i’m getting wrong answer when using arrays and right answer when using vector. I am unable to understand why this is happening.
can anyone please help me with this?
Below is my code snippet:
#include <bits/stdc++.h>
int f(int idx, vector<int> &heights,vector<int> dp) {
if (idx == 0)
return 0;
if(dp[idx] != -1)
return dp[idx];
int left = f(idx - 1, heights, dp) + abs(heights[idx]-heights[idx-1]);
int right = INT_MAX;
if(idx > 1)
right = f(idx - 2, heights, dp) + abs(heights[idx]-heights[idx-2]);
return dp[idx] = min(left,right);
}
int frogJump(int n, vector<int> &heights)
{
// Write your code here.
vector<int> dp(n,-1);
return f(n-1,heights,dp);
}
I’m expecting that the code should work with Arrays as well, not only with vector.
Anonymous is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.