-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlec10_4.cpp
More file actions
58 lines (45 loc) · 1.11 KB
/
lec10_4.cpp
File metadata and controls
58 lines (45 loc) · 1.11 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
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution {
public:
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 minCost(vector<int>& height) {
// Code here
int n = height.size();
vector<int> dp(n+1 , -1);
return f(n-1 , height, dp);
}
};
//{ Driver Code Starts.
int main() {
string str;
getline(cin, str);
int t = stoi(str);
while (t--) {
getline(cin, str);
stringstream ss(str);
vector<int> arr;
int num;
while (ss >> num) {
arr.push_back(num);
}
Solution ob;
cout << ob.minCost(arr) << endl;
cout << "~" << endl;
}
return 0;
}
// } Driver Code Ends