Waypoint mission creation in Drone Programming - Time & Space Complexity
When creating a waypoint mission for a drone, it's important to know how the time to set up the mission grows as you add more waypoints.
We want to understand how the number of waypoints affects the work the program does.
Analyze the time complexity of the following code snippet.
mission = createMission()
for waypoint in waypoints:
mission.addWaypoint(waypoint.latitude, waypoint.longitude, waypoint.altitude)
mission.upload()
This code creates a mission and adds each waypoint from a list, then uploads the mission to the drone.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Adding each waypoint to the mission inside a loop.
- How many times: Once for each waypoint in the list.
As the number of waypoints increases, the program adds more points one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The work grows directly with the number of waypoints.
Time Complexity: O(n)
This means the time to create the mission grows in a straight line as you add more waypoints.
[X] Wrong: "Adding waypoints happens instantly no matter how many there are."
[OK] Correct: Each waypoint requires a step to add, so more waypoints mean more steps and more time.
Understanding how adding waypoints scales helps you write efficient drone missions and shows you can think about how programs grow with input size.
"What if the mission.addWaypoint method itself loops over all existing waypoints each time it adds a new one? How would the time complexity change?"