Watch the video solution for Maximum Calories Burnt from Jumps, a medium level problem involving Array, Two Pointers, Greedy. This walkthrough by Code-Yao has 72 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given an integer array heights of size n, where heights[i] represents the height of the ith block in an exercise routine.
You start on the ground (height 0) and must jump onto each block exactly once in any order.
a to a block of height b is (a - b)2.heights[i] is (0 - heights[i])2.Return the maximum total calories you can burn by selecting an optimal jumping sequence.
Note: Once you jump onto the first block, you cannot return to the ground.
Example 1:
Input: heights = [1,7,9]
Output: 181
Explanation:
The optimal sequence is [9, 1, 7].
heights[2] = 9: (0 - 9)2 = 81.heights[0] = 1: (9 - 1)2 = 64.heights[1] = 7: (1 - 7)2 = 36.Total calories burned = 81 + 64 + 36 = 181.
Example 2:
Input: heights = [5,2,4]
Output: 38
Explanation:
The optimal sequence is [5, 2, 4].
heights[0] = 5: (0 - 5)2 = 25.heights[1] = 2: (5 - 2)2 = 9.heights[2] = 4: (2 - 4)2 = 4.Total calories burned = 25 + 9 + 4 = 38.
Example 3:
Input: heights = [3,3]
Output: 9
Explanation:
The optimal sequence is [3, 3].
heights[0] = 3: (0 - 3)2 = 9.heights[1] = 3: (3 - 3)2 = 0.Total calories burned = 9 + 0 = 9.
Constraints:
1 <= n == heights.length <= 1051 <= heights[i] <= 105Problem Overview: You are given an array representing the heights (or intensity) of jumps. The calories burned between consecutive jumps depend on the difference between their heights. The goal is to arrange the jumps in an order that maximizes the total calories burned.
Approach 1: Brute Force Permutations (O(n!))
The most direct idea is to generate every possible ordering of the jumps and compute the total calories burned for each arrangement. For each permutation, iterate through the array and sum the absolute differences between consecutive elements. Track the maximum value across all permutations. This guarantees the optimal answer but quickly becomes impractical since the number of permutations grows factorially. Time complexity is O(n!) and space complexity is O(n) for recursion and permutation storage.
Approach 2: Greedy + Sorting with Two Pointers (O(n log n))
A key observation: large differences between consecutive jumps produce more calories. To maximize the total, you want large and small values to appear next to each other as often as possible. Start by sorting the array. Then construct the sequence by alternately taking the smallest and largest remaining elements using a two-pointer strategy. This arrangement spreads extremes apart and increases the sum of absolute differences.
Use two pointers: one at the beginning and one at the end of the sorted array. Pick elements from opposite ends and place them into the result sequence in alternating order. Finally, compute the total calories burned by iterating through the constructed sequence and summing |a[i] - a[i-1]|. Sorting costs O(n log n), while building the sequence and computing the sum takes O(n). Space complexity is O(n) for the arranged sequence.
This greedy idea works because maximizing local differences between neighbors contributes directly to the global objective. The technique frequently appears in problems involving maximizing pairwise gaps or arranging numbers for maximum contrast.
Related concepts include array manipulation, sorting, and the two pointers technique, which help efficiently build the optimal arrangement.
Recommended for interviews: The Greedy + Sorting solution. Interviewers expect you to recognize that maximizing adjacent differences requires pairing extremes. Mentioning the brute force permutation approach first shows you understand the search space, but identifying the greedy ordering demonstrates strong algorithmic intuition.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Permutations | O(n!) | O(n) | Useful only for very small arrays or for validating the optimal solution during testing |
| Greedy + Sorting with Two Pointers | O(n log n) | O(n) | Best general solution for maximizing adjacent differences after sorting |