Advertisement
Josif_tepe

Untitled

Oct 18th, 2021
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. int n;
  5. int arr[100005];
  6. int memo[100005];
  7. int frog(int i) {
  8.     if(i == n - 1) {
  9.         return 0;
  10.     }
  11.     if(memo[i] != -1) {
  12.         return memo[i];
  13.     }
  14.     int result = 2e9;
  15.     if(i + 1 < n) {
  16.         result = min(result, frog(i + 1) + abs(arr[i] - arr[i + 1]));
  17.     }
  18.     if(i + 2 < n) {
  19.         result = min(result, frog(i + 2) + abs(arr[i] - arr[i + 2]));
  20.     }
  21.     return memo[i] = result;
  22. }
  23. int main() {
  24.     cin >> n;
  25.     for(int i = 0; i < n; i++) {
  26.         cin >> arr[i];
  27.         memo[i] = -1;
  28.     }
  29.     cout << frog(0) << endl;
  30.     return 0;
  31. }
  32. // fib(7) = fib(6) + fib(5)
  33. //
  34.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement