-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlec10_7.cpp
More file actions
77 lines (59 loc) Β· 1.5 KB
/
lec10_7.cpp
File metadata and controls
77 lines (59 loc) Β· 1.5 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution {
public:
int solveUtil(int ind ,vector<int>& arr, vector<int>&dp,int k)
{
if(ind == 0) return 0 ;
if(dp[ind] !=-1) return dp[ind];
int mmSteps =INT_MAX;
for(int j = 1 ; j<=k;j++)
{
if(ind - j >=0){
int jump = solveUtil(ind-j, arr, dp ,k) +abs(arr[ind]-arr[ind-j]);
mmSteps = min(jump , mmSteps);
}
}
return dp[ind] = mmSteps;
}
int solve(int n , vector<int> & arr, int k )
{
vector<int>dp(n , -1);
return solveUtil(n-1,arr,dp,k);
}
int minimizeCost(int k, vector<int>& arr) {
// Code here
int n = arr.size();
vector<int>dp(n , -1);
return solve(n, arr, k);
}
};
//{ Driver Code Starts.
int main() {
string ts;
getline(cin, ts);
int t = stoi(ts);
while (t--) {
string ks;
getline(cin, ks);
int k = stoi(ks);
vector<int> arr;
string input;
getline(cin, input);
stringstream ss(input);
int number;
while (ss >> number) {
arr.push_back(number);
}
Solution obj;
int res = obj.minimizeCost(k, arr);
cout << res << endl;
cout << "~" << endl;
// string tl;
// getline(cin, tl);
}
return 0;
}
// } Driver Code Ends