Speed control during mission in Drone Programming - Time & Space Complexity
When controlling a drone's speed during a mission, it's important to know how the time to adjust speed changes as the mission length grows.
We want to understand how the program's work increases when the drone has more steps to fly.
Analyze the time complexity of the following code snippet.
for step in mission_steps:
if step.requires_speed_change:
set_speed(step.new_speed)
fly_to(step.location)
This code goes through each step in the mission, changes speed if needed, then flies to the next location.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each mission step once.
- How many times: Exactly once per step, so as many times as there are steps.
As the number of mission steps increases, the program does more work in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 speed checks and moves |
| 100 | About 100 speed checks and moves |
| 1000 | About 1000 speed checks and moves |
Pattern observation: The work grows evenly with the number of steps; doubling steps doubles work.
Time Complexity: O(n)
This means the time to complete the mission grows directly with the number of steps.
[X] Wrong: "Changing speed multiple times in a step makes the program slower in a way that multiplies work."
[OK] Correct: The speed change happens at most once per step, so it adds only a small fixed amount of work per step, not extra loops.
Understanding how your drone's speed control scales with mission length shows you can think about efficiency in real tasks, a skill useful in many programming challenges.
"What if the code checked speed changes inside a nested loop for each step? How would the time complexity change?"