-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrog jump.cpp
More file actions
61 lines (55 loc) · 1.57 KB
/
frog jump.cpp
File metadata and controls
61 lines (55 loc) · 1.57 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
51
52
53
54
55
56
57
58
59
60
61
// top down solution
#include<bits/stdc++.h>
using namespace std;
int dp[100001];
int solve(int n, vector<int>& heights) {
if (n == 0) return 0;
int left = INT_MAX;
int right = INT_MAX;
if (dp[n] != -1) return dp[n];
left = abs(heights[n] - heights[n - 1]) + solve(n - 1, heights);
if (n > 1) {
right = abs(heights[n] - heights[n - 2]) + solve(n - 2, heights);
}
return dp[n] = min(left, right);
}
int frogJump(int n, vector<int>& heights)
{
// Write your code here.
memset(dp, -1, sizeof(dp));
return solve(n - 1, heights);
}
// bottom up solution
#include<bits/stdc++.h>
using namespace std;
int frogJump(int n, vector<int>& heights)
{
// Write your code here.
vector<int> dp(n, 0);
for (int i = 1; i < n; i++){
int first_step = INT_MAX;
int second_step = INT_MAX;
first_step = abs(heights[i] - heights[i - 1]) + dp[i - 1];
if (i > 1) second_step = abs(heights[i] - heights[i - 2]) + dp[i - 2];
dp[i] = min(first_step, second_step);
}
return dp[n - 1];
}
// space optized solution
#include<bits/stdc++.h>
using namespace std;
int frogJump(int n, vector<int>& heights)
{
// Write your code here.
int prev = 0, sprev = 0;
for (int i = 1; i < n; i++){
int first_step = INT_MAX;
int second_step = INT_MAX;
first_step = abs(heights[i] - heights[i - 1]) + prev;
if (i > 1) second_step = abs(heights[i] - heights[i - 2]) + sprev;
int current = min(first_step, second_step);
sprev = prev;
prev = current;
}
return prev;
}