Skip to main content

Maximum Calories Burnt from Jumps - Solution & Explanation

MediumPremiumFree on FleetCodeArrayTwo PointersGreedySorting12 min readAsked at: Amazon
Practice this problem

Problem Statement

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.

  • The calories burned for a jump from a block of height a to a block of height b is (a - b)2.
  • The calories burned for the first jump from the ground to the chosen first block 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].

  • Initial jump from the ground to heights[2] = 9: (0 - 9)2 = 81.
  • Next jump to heights[0] = 1: (9 - 1)2 = 64.
  • Final jump to 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].

  • Initial jump from the ground to heights[0] = 5: (0 - 5)2 = 25.
  • Next jump to heights[1] = 2: (5 - 2)2 = 9.
  • Final jump to 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].

  • Initial jump from the ground to heights[0] = 3: (0 - 3)2 = 9.
  • Next jump to heights[1] = 3: (3 - 3)2 = 0.

Total calories burned = 9 + 0 = 9.

 

Constraints:

  • 1 <= n == heights.length <= 105
  • 1 <= heights[i] <= 105

Approach Overview

Problem 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.

Solution

According to the problem statement, the order of jumps affects the total calories burned. To maximize calorie consumption, we can use a greedy strategy by prioritizing jumps with the largest height differences.

Therefore, we can first sort the block heights, then start jumping from the highest block, then to the lowest block, and so on, until all blocks have been jumped on.

The specific steps are as follows:

  1. Sort the array heights.
  2. Initialize the variable pre = 0 to represent the height of the previous block, and ans = 0 to represent the total calories burned.
  3. Use two pointers: the left pointer l points to the beginning of the array, and the right pointer r points to the end of the array.
  4. While l < r, do the following:
    1. Calculate the calories burned from the previous block to the block pointed to by the right pointer and add it to ans.
    2. Calculate the calories burned from the block pointed to by the right pointer to the block pointed to by the left pointer and add it to ans.
    3. Update pre to the height of the block pointed to by the left pointer.
    4. Move the left pointer one step to the right and the right pointer one step to the left.
  5. Finally, calculate the calories burned from the previous block to the middle block and add it to ans.

The time complexity is O(n log n) and the space complexity is O(log n), where n is the length of the array.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force PermutationsO(n!)O(n)Useful only for very small arrays or for validating the optimal solution during testing
Greedy + Sorting with Two PointersO(n log n)O(n)Best general solution for maximizing adjacent differences after sorting

Video Solution

leetcode 3730 What is your implementation? Maximum Calories Burnt from Jumps • Code-Yao • 72 views views

Frequently Asked Questions

Is Maximum Calories Burnt from Jumps easy or hard?
The problem is typically classified as Medium. The brute force idea is straightforward, but recognizing that sorting and greedily pairing extremes maximizes adjacent differences requires algorithmic insight.
Maximum Calories Burnt from Jumps Python/Java solution
Implement the greedy approach by sorting the array and using two pointers to build the sequence. This logic works the same across Python, Java, C++, Go, and TypeScript because it only requires array sorting, pointer movement, and absolute difference calculations.
How to solve Maximum Calories Burnt from Jumps in O(n log n)?
First sort the jump heights. Then use two pointers at the start and end of the array to alternately select the smallest and largest elements, creating a sequence that maximizes adjacent differences. Finally compute the total calories by summing absolute differences between neighboring values.
What is the best approach for Maximum Calories Burnt from Jumps?
The most efficient method uses a greedy strategy after sorting the array. By arranging the smallest and largest remaining values next to each other using a two-pointer technique, you maximize the absolute difference between consecutive jumps. Sorting takes O(n log n) time and the arrangement step runs in O(n).
Is Maximum Calories Burnt from Jumps asked at Google/Amazon/Meta?
Problems involving maximizing adjacent differences or arranging numbers using greedy ordering appear frequently in interviews at companies like Amazon, Google, and Meta. The exact problem title may vary, but the underlying pattern of sorting plus greedy arrangement is common.
What data structure is used in Maximum Calories Burnt from Jumps?
The primary data structure is a simple array. The algorithm relies on sorting the array and then using the two-pointer technique to pick elements from both ends efficiently while constructing the optimal order.
What is the time complexity of Maximum Calories Burnt from Jumps?
The optimal solution runs in O(n log n) time due to sorting the jump heights. After sorting, building the alternating sequence with two pointers and calculating the total calories both take linear O(n) time. Space complexity is typically O(n) for storing the rearranged order.

Ready to solve this problem?

Practice Maximum Calories Burnt from Jumps with our built-in code editor and test cases.

Practice on FleetCode