Files
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

2.1 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
6a2037a68a0bc2aef0006005 Challenge 343: Elevator Stops 29 challenge-343

--description--

Given a number for the current floor of an elevator and an array of requested floors, return an array of the order the elevator should visit them to minimize number of floors traveled.

  • If tied, go up first
  • Floors with a request must be visited when the elevator first passes them

--hints--

elevator_stops(5, [2, 8, 3, 9]) should return [3, 2, 8, 9].

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(elevator_stops(5, [2, 8, 3, 9]), [3, 2, 8, 9])`)
}})

elevator_stops(6, [2, 10, 8, 3, 1, 9]) should return [8, 9, 10, 3, 2, 1].

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(elevator_stops(6, [2, 10, 8, 3, 1, 9]), [8, 9, 10, 3, 2, 1])`)
}})

elevator_stops(1, [4, 8, 3, 6, 9]) should return [3, 4, 6, 8, 9].

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(elevator_stops(1, [4, 8, 3, 6, 9]), [3, 4, 6, 8, 9])`)
}})

elevator_stops(12, [6, 10, 7, 3, 1, 4]) should return [10, 7, 6, 4, 3, 1].

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(elevator_stops(12, [6, 10, 7, 3, 1, 4]), [10, 7, 6, 4, 3, 1])`)
}})

elevator_stops(11, [2, 8, 23, 5, 12, 10, 6, 9, 19]) should return [10, 9, 8, 6, 5, 2, 12, 19, 23].

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(elevator_stops(11, [2, 8, 23, 5, 12, 10, 6, 9, 19]), [10, 9, 8, 6, 5, 2, 12, 19, 23])`)
}})

--seed--

--seed-contents--

def elevator_stops(current_floor, stops):

    return current_floor

--solutions--

def elevator_stops(current_floor, stops):
    stops = sorted(stops)
    above = [s for s in stops if s > current_floor]
    below = [s for s in stops if s < current_floor][::-1]
    up_distance = above[-1] - current_floor if above else float('inf')
    down_distance = current_floor - below[-1] if below else float('inf')
    return below + above if down_distance < up_distance else above + below