MOONSOONP - Editorial

Problem Link - EVacuate to Moon in Greedy Algorithms

Problem Statement:

Pesla needs to charge N cars before sending them to space. The ith car has an energy capacity of A_i Watt-hours.

There are M power outlets. The jth outlet provides a power of B_j Watt.
If an outlet can charge at most one car and a car can be charged by at most one outlet, find the maximum total energy (in Watt-hours) stored in all cars after H hours.

Approach:

1. Understanding the Problem:

  • Each car has a maximum energy capacity.
  • Each power outlet provides a certain power and can charge one car for a given duration.
  • The total energy stored in each car after charging is the minimum of the energy supplied by the outlet (based on its power and time) and the car’s capacity.

2. Calculating Energy:

  • For each outlet, calculate the total energy it can provide over the given time, which is B[j] * H.
  • Sort both the car capacities and the calculated energies from the outlets in descending order.
  • Use a greedy strategy to assign the highest available energy to the highest capacity car, ensuring that no outlet is reused and no car is charged more than once.

Complexity:

  • Time Complexity: O(NlogN + MlogM) for sorting the car capacities and outlet energies. The rest of the operations are linear, i.e., O(min(N, M)) for each test case. Total time complexity: O(NlogN + MlogM).
  • Space Complexity: O(1) For auxiliary space , O(N + M) for input space.