File tree 1 file changed +48
-0
lines changed
1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < bits/stdc++.h>
2
+ using namespace std ;
3
+
4
+ #define int long long
5
+ #define F (a,b,i ) for (int i = a; i < b; i++)
6
+ #define all (v ) v.begin(), v.end()
7
+
8
+ typedef long double ld;
9
+ typedef vector<int > vi;
10
+
11
+ // ------------------------------------------------------
12
+
13
+ /*
14
+ N stones are there(1 to n) a frog is initally is at stone 1, he can jump to the next or
15
+ second next stone at a time, every jump costs ( |Hj - Hi| while jumping from i to j) -- (h1,h2...hN will be given)..
16
+ find the minimum cost to reach n...
17
+ */
18
+
19
+ // TC = O(N);
20
+
21
+ int32_t main ()
22
+ {
23
+ cin.tie (0 );
24
+ ios::sync_with_stdio (false );
25
+ cin.tie (NULL );
26
+ cout.tie (NULL );
27
+
28
+ int t; cin>>t;
29
+ while (t--)
30
+ {
31
+ int n; cin>>n;
32
+ vi H (n);
33
+ F (0 ,n,i){cin>>H[i];} // 1 indexed
34
+
35
+ int dp[n+1 ];
36
+ dp[0 ] = 0 ; // initialization as it will cost 0 to be on 1st stone
37
+ dp[1 ] = abs (H[1 ] - H[0 ]); // initialization as there is only onw way to go on 2nd step
38
+
39
+ F (2 ,n,i){
40
+ int x = dp[i-1 ]+abs (H[i]-H[i-1 ]);
41
+ int y = dp[i-2 ]+abs (H[i]-H[i-2 ]);
42
+ dp[i] = min (x,y);
43
+ }
44
+
45
+ cout<<dp[n-1 ]<<" \n " ;
46
+ }
47
+
48
+ }
You can’t perform that action at this time.
0 commit comments