BackmediumArrays PhonePe Flipkart

Space Station Supply Chain Optimization Solution

Problem Statement

A space station has limited cargo capacity and receives shipments from various planets. The capacity is filled by loading cargo in a sequence such that each subsequent shipment has a different fuel efficiency (positive for efficient, negative for less efficient). The task is to maximize the total fuel efficiency by selecting the optimal sequence of shipments.

Example 1
Input
[1, -2, 3, -4, 5]
Output
17

Explanation: Step-by-step: To maximize the total fuel efficiency, we need to select the optimal sequence of shipments. We can use a greedy approach to solve this problem. First, we sort the array of fuel efficiencies in descending order. Then, we iterate over the sorted array and add the fuel efficiencies to the total. The optimal sequence is obtained by selecting the most efficient shipments first.

Example 2
Input
[-1, 2, -3, 4, -5]
Output
1

Explanation: Step-by-step: To maximize the total fuel efficiency, we need to select the optimal sequence of shipments. We can use a greedy approach to solve this problem. First, we sort the array of fuel efficiencies in descending order. Then, we iterate over the sorted array and add the fuel efficiencies to the total. The optimal sequence is obtained by selecting the most efficient shipments first.

Constraints

  • The array can be empty
  • The shipment efficiencies will always be integers
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free