FROGJUMP-2
Fri Jun 07 2024 04:10:57 GMT+0000 (Coordinated Universal Time)
Saved by
@devdutt
#include <bits/stdc++.h>
using namespace std;
int mod = 1e9 + 7;
int n, k;
int t[(int)1e5 + 1];
int frogJump(vector<int>& a, int n, int k) {
if (n == 1) return 0; // Starting point, no cost
if (t[n] != -1) return t[n];
int minCost = INT_MAX;
for (int i = 1; i <= k; ++i) {
if (n - i >= 1) {
minCost = min(minCost, frogJump(a, n - i, k) + abs(a[n - 1] - a[n - i - 1]));
}
}
t[n] = minCost % mod;
return t[n];
}
int main() {
memset(t, -1, sizeof(t));
cin >> n >> k;
vector<int> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
int result = frogJump(a, n, k);
cout << result << endl;
return 0;
}
content_copyCOPY
so the earlier frog jump question that I did consistered of that the frog and jump Two Steps at a time or one step at a time but in this question the frog can jump k steps at a time so you further have to run a loop to for every element in the array,TC-->O(n)*k
https://atcoder.jp/contests/dp/submissions/54289370
Comments