-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlec10_6.cpp
64 lines (47 loc) Β· 1.13 KB
/
lec10_6.cpp
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
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution {
public:
int minCost(vector<int>& height) {
// Code here
int n = height.size();
int prev = 0 ;
int prev2 = 0 ;
int curr = 0 ;
for(int i = 1 ; i<n;i++)
{
int fs = prev + abs(height[i]- height[i-1]);
int ss = INT_MAX;
if(i>1)
{
ss = prev2 + abs(height[i]- height[i-2]);
}
curr= min(fs , ss);
prev2 = prev;
prev = curr;
}
return prev;
}
};
//{ 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