Data Structures & AlgorithmsVery hardLeetCode-style · Codeforces Div2 C

A train makes three legs of lengths 1, 3 and 2 km. Each leg except the last must be finished in whole hours (a partial hour is rounded up), and the last leg can take a fraction of an hour. You must arrive within 6 hours. What is the minimum integer speed in km/h?

  1. A. 2
  2. B. 1
  3. C. 0
  4. D. 3

Correct answer: B. 1

Binary search the integer speed v: legs before the last cost ceil(d/v) whole hours and the last leg costs d/v. The total time is non-increasing in v, so the first feasible v is the answer. Rounding every leg up (including the last) overestimates the time.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic