Skip to content

Optimize on drone battery life by choosing best paths

Notifications You must be signed in to change notification settings

Sepidband/drone_battery

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 

Repository files navigation

Drone Delivery Optimization Problem

Problem Description

A delivery drone starts at a depot with a fixed battery capacity and must make a sequence of deliveries. The goal is to determine the maximum number of deliveries the drone can complete in a single trip without recharging.

Problem Details

  • The drone starts at a depot with battery capacity B (integer)
  • Each delivery involves:
    • Flying from depot to customer location at distance d
    • Returning to the depot
    • Total battery consumption: 2*d units
  • After each delivery, the drone can start the next delivery if it has sufficient battery
  • The drone stops when its battery is insufficient for any additional round trip

Input Format

  • Line 1: Integer B (0 ≤ B ≤ 10^9) - total available battery units
  • Line 2: N space-separated positive integers (0 ≤ N ≤ 10^5, each distance ≤ 10^6) - distances to delivery locations

Output Format

  • A single integer: the maximum number of deliveries the drone can make without exceeding its battery capacity

Solution Approach

Algorithm: Greedy Strategy with Sorting

  1. Sort distances in ascending order to prioritize closest locations first
  2. Iterate through sorted distances and calculate round-trip cost (2 × distance)
  3. Check battery constraint for each delivery:
    • If sufficient battery: make delivery and update battery usage
    • If insufficient battery: stop (remaining deliveries will cost even more)
  4. Return the count of completed deliveries

Why This Works

The greedy approach is optimal because:

  • To maximize the number of deliveries, we should minimize battery usage per delivery
  • Choosing the closest available location first always uses the least battery
  • Once we can't afford a delivery, we can't afford any remaining deliveries (since they're farther)

Complexity Analysis

  • Time Complexity: O(N log N) due to sorting
  • Space Complexity: O(1) additional space (in-place sorting)

Edge Cases Handled

  • Empty array of distances → return 0
  • Zero battery capacity → return 0
  • Battery sufficient for all deliveries → return total count
  • Large numbers within constraint limits

Example Walkthrough

Input:
B = 10
distances = [3, 1, 4, 2]

Step 1: Sort distances → [1, 2, 3, 4]
Step 2: Calculate round-trip costs → [2, 4, 6, 8]
Step 3: Check deliveries:
  - Delivery 1 (distance 1): cost 2, total used = 2 ≤ 10 ✓
  - Delivery 2 (distance 2): cost 4, total used = 6 ≤ 10 ✓
  - Delivery 3 (distance 3): cost 6, total used = 12 > 10 ✗

Output: 2 deliveries

Performance Expectations

  • Handles up to 10^5 delivery locations efficiently
  • Works with battery capacities up to 10^9
  • Optimized for competitive programming constraints
  • Memory efficient with minimal additional space usage

Usage

The solution can be used in two ways:

  1. Function call: max_deliveries(B, distances) - returns the maximum number of deliveries
  2. Input parsing: solve_from_input() - reads from standard input in the specified format

Perfect for competitive programming problems involving optimization with resource constraints.

About

Optimize on drone battery life by choosing best paths

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published