-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrogJumpLecture3.cpp
More file actions
50 lines (50 loc) · 1.14 KB
/
Copy pathFrogJumpLecture3.cpp
File metadata and controls
50 lines (50 loc) · 1.14 KB
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
45
46
47
48
49
50
#include "vector"
#include <iostream>
#include <climits>
using namespace std;
int f(int ind,vector<int> &heights,vector<int> &dp)
{
if(ind == 0)
{
return 0;
}
if(dp[ind] != -1) return dp[ind];
int left = f(ind-1,heights,dp) + abs(heights[ind]-heights[ind-1]);
int right = INT_MAX;
if(ind > 1) right = f(ind-2,heights,dp) + abs(heights[ind]-heights[ind-2]);
return dp[ind] = min(left,right);
}
int frogJump(int n, vector<int> &heights)
{
vector<int> dp(n,-1);
dp[0] = 0;
for(int i=1;i<n;i++)
{
int fs = dp[i-1] + abs(heights[i]-heights[i-1]);
int ss = INT_MAX;
if(i > 1)
{
ss = dp[i-2] + abs(heights[i] - heights[i-2]);
}
dp[i] = min(fs,ss);
}
return dp[n-1];
}
int frogJump(int n, vector<int> &heights)
{
int prev = 0;
int prev2 = 0;
for(int i=1;i<n;i++)
{
int fs = prev + abs(heights[i]-heights[i-1]);
int ss = INT_MAX;
if(i > 1)
{
ss = prev2 + abs(heights[i] - heights[i-2]);
}
int curi = min(fs,ss);
prev2 = prev;
prev = curi;
}
return prev;
}