-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrog_jump.cpp
44 lines (36 loc) · 1004 Bytes
/
frog_jump.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
link:- https://www.codingninjas.com/codestudio/problems/frog-jump_3621012
*/
/************************* RECURSION ****************************/
int func(int ind,vector<int> &heights,vector<int>&dp)
{
if(ind==0) return 0;
if(dp[ind]!= -1) return dp[ind];
int l= (ind-1)+abs(heights[ind]-heights[ind-1]);
int r=INT_MAX;
if(ind>1)
r= func(ind-2,heights,dp)+abs(heights[ind]-heights[ind-2]);
return dp[ind]=min(l,r);
}
int frogJump(int n, vector<int> &heights)
{
// Write your code here.
vector<int> dp(n+1,-1);
return func(n-1,heights,dp);
}
/****************************** TABULAR ********************/
int frogJump(int n, vector<int> &heights)
{
// Write your code here.
vector<int> dp(n,0);
dp[0]=0;
for(int i=1;i<n;i++)
{
int l=dp[i-1] + abs(heights[i]-heights[i-1]);
int r= INT_MAX;
if(i>1)
{ r=dp[i-2]+ abs(heights[i]-heights[i-2]);}
dp[i]=min(l,r);
}
return dp[n-1];
}