At Most K Jumps with Quadratic Transition Cost
Updated
Problem statement
You are given two integer arrays positions and landingCosts of the same length n, and an integer maxJumps. The indices are 0 to n - 1, and positions is sorted in nondecreasing order.
You start at index 0 and want to reach index n - 1. Every jump goes forward, from an index i to any index j with i < j. A jump from i to j costs (positions[i] - positions[j]) * (positions[i] - positions[j]) + landingCosts[j]: the square of the gap between the two positions, plus the landing cost of the index you land on.
You may use at most maxJumps jumps. Return the minimum total cost to reach index n - 1.
The starting index is free: landingCosts[0] is never paid. If n == 1, you are already at the last index, so the answer is 0.
Function
minimumQuadraticJumpCost(positions: int[], landingCosts: int[], maxJumps: int) → longExamples
Example 1
positions = [0,2,5]landingCosts = [0,3,1]maxJumps = 2return = 17With maxJumps = 2 there are two routes to index 2. The direct jump 0 -> 2 costs (0 - 5) * (0 - 5) + 1 = 26. The route 0 -> 1 -> 2 costs (0 - 2) * (0 - 2) + 3 = 7 for the first jump and (2 - 5) * (2 - 5) + 1 = 10 for the second, 17 in total. So the answer is 17.
Example 2
positions = [0,1,4,10]landingCosts = [0,5,2,3]maxJumps = 2return = 57There are three routes to index 3 with at most 2 jumps. 0 -> 3 costs 100 + 3 = 103, 0 -> 1 -> 3 costs (1 + 5) + (81 + 3) = 90, and 0 -> 2 -> 3 costs (16 + 2) + (36 + 3) = 57. The route 0 -> 1 -> 2 -> 3 would cost only (1 + 5) + (9 + 2) + (36 + 3) = 56, but it uses 3 jumps, more than maxJumps = 2. So the answer is 57.
Example 3
positions = [1,1,2]landingCosts = [0,4,1]maxJumps = 2return = 2The direct jump 0 -> 2 costs (1 - 2) * (1 - 2) + 1 = 2. The route 0 -> 1 -> 2 has a gap of 0 on its first jump but still pays landingCosts[1] = 4, then (1 - 2) * (1 - 2) + 1 = 2, so it costs 6. So the answer is 2.
Constraints
1 <= n <= 100000, wheren = positions.lengthlandingCosts.length == n0 <= positions[i] <= 100000positions[i] <= positions[i + 1]for every0 <= i < n - 10 <= landingCosts[i] <= 10000001 <= maxJumps <= 100
More Infosys Online Assessment questions
- Hiking Club Fastest PaceOA · Seen Nov 2026
- Lowest Common Ancestor with a Changing RootOA · Seen Nov 2026
- Minimum Leaf Path Sum over All RootsOA · Seen Nov 2026
- Split Array to Minimize the Largest SumOA · Seen Nov 2026
- Cherry PickupOA · Seen Nov 2026
- Lexicographically Smallest Grid Path with One Letter ChangeOA · Seen Nov 2026
- Minimum Cost to Supply Water with Wells and PipesOA · Seen Nov 2026
- Minimum Cost to Tile a Strip with Widths 1, 2, and 3OA · Seen Nov 2026
- Minimum Rounds to Complete All TasksOA · Seen Nov 2026
- Lift Total TravelOA · Seen Nov 2026
Similar questions at other companies
- Jump Game with Prime-3 StepsUber · OAUber interview questions
- Count Permutations Without Three Equal Parities in a RowGoogle · OAGoogle interview questions
- Maximum Score with Prime JumpsMicrosoft · OAMicrosoft interview questions
- Jump Game with Prime-3 StepsAgoda · OAAgoda interview questions
- Coin Game From The EndsTekion · PHONE SCREENTekion interview questions
- Maximum-Product Subarray with an Empty ChoiceAkuna Capital · PHONE SCREENAkuna Capital interview questions
- Pascal's TriangleJP Morgan Chase · ONSITE INTERVIEWJP Morgan Chase interview questions
- Largest Consistent Visiting GroupAuxoAI · OA
- Delete and Freeze GameRubrik · OARubrik interview questions
- Lock CodeVisa · OAVisa interview questions