-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrogKJumpLecture4FollowUp.cpp
More file actions
45 lines (45 loc) · 947 Bytes
/
Copy pathFrogKJumpLecture4FollowUp.cpp
File metadata and controls
45 lines (45 loc) · 947 Bytes
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
#include "vector"
#include <iostream>
#include <climits>
using namespace std;
int solve(int index,vector<int> arr,int k,vector<int> &dp)
{
if(index == 0) return 0;
int ans = INT_MAX;
if(dp[index] != -1) return dp[index];
for(int i=1;i<=k;i++)
{
if(index-i>=0)
{
ans = min(ans,solve(index-i,arr,k%index,dp) + abs(arr[index] - arr[index - i]));
// cout<<ans<<" ";
}
}
return dp[index] = ans;
}
int main()
{
int n,k;
cin>>n>>k;
vector<int> arr(n);
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
vector<int> dp(n,INT_MAX);
dp[0] = 0;
for(int index = 1;index <= n-1;index++)
{
// int ans = INT_MAX;
for(int j=1;j<=k;j++)
{
if(index-j>=0)
{
dp[index] = min(dp[index],dp[index-j] + abs(arr[index] - arr[index - j]));
}
}
// dp[index] = ans;
}
cout<<dp[n-1];
return 0;
}