Statistics

Problem Statement for "VariableSpeedLimit"

Problem Statement

In order to make its roads safer, the government of a particular country has decided to introduce a speed limit that varies throughout the day. At busy times, the speed limit will decrease to reduce the risk of a dangerous accident. You need to drive a distance of journeyLength units and want to know how long it will take you.

You are given details of the speed limit in a int[] speedLimit. Element i (zero-based) gives the speed limit in DISTANCE UNITS/TIME UNITS that is in force between times T = i and T = i + 1. speedLimit describes the speed limit for a full day, so after this the pattern repeats (i.e., if N is the number of elements in speedLimit, the speed limit between times N and N+1 is given by speedLimit[0], etc.). You start your journey at time T = 0 and should assume that you travel exactly at the speed limit for your entire journey. Return a double containing the amount of time it takes to complete your journey.

Definition

Class:
VariableSpeedLimit
Method:
journeyTime
Parameters:
int, int[]
Returns:
double
Method signature:
double journeyTime(int journeyLength, int[] speedLimit)
(be sure your method is public)

Notes

  • Your return value must be accurate to an absolute or relative tolerance of 1E-9.

Constraints

  • journeyLength will be between 1 and 100000 (10^5), inclusive.
  • speedLimit will contain between 1 and 50 elements, inclusive.
  • Each element of speedLimit will be between 1 and 100, inclusive.

Examples

  1. 100

    {50}

    Returns: 2.0

    Here the speed limit doesn't change. The journey therefore takes a time of DISTANCE/SPEED = 100/50 = 2 time units.

  2. 100

    {50,25}

    Returns: 2.5

    Now the speed limit drops to 25 every other time unit. You drive 50 units in the first time unit and 25 in the second. You reach the end of your journey halfway through the third time unit.

  3. 1000

    {50,40,30,40,50}

    Returns: 24.0

  4. 2058

    {80,43,57,23,28,45,60,75,73,80}

    Returns: 37.4

  5. 56935

    {82,20,17,15,48,3,9,64,98,84,81,53,32,20}

    Returns: 1272.65625

  6. 99345

    {37,1,28,49,82,50,98,97,23,22}

    Returns: 2039.8636363636363

  7. 64609

    {49,40,5,34,50,85,86,69,81,32,96,35,27}

    Returns: 1219.0104166666667

  8. 45767

    {58,46,9,4,100,56,18,28,90,77,39,60,49,48}

    Returns: 939.3260869565217

  9. 98803

    {87,10,45,21,22,32,82,52,42,95,61,46}

    Returns: 1992.3793103448277

  10. 71899

    {69,92,27,67,16,16,34,49,2,99}

    Returns: 1526.5882352941176

  11. 30901

    {98,81,27,84,78,83,24,11,21,72,56}

    Returns: 533.6385542168674

  12. 17216

    {26,30,62,55,51,56,58,4,60,23,31}

    Returns: 415.03333333333336

  13. 9839

    {45,90,13,4,81,50,81,10,64,86,69}

    Returns: 182.8395061728395

  14. 80821

    {57,91,27,20,45,16,7,16,83,45,95}

    Returns: 1770.9894736842105

  15. 100000

    {1}

    Returns: 100000.0

  16. 43552

    {30,26,79,65,45,86,76,100,3,85,5,59,80,19,76,96,22,22,77,10,81,97,2,22,54,38,96,87,3,11,62,45,99,18,80,50,47,38,29,79,70,78,56,7,10,49,97,25,20,83}

    Returns: 839.3544303797469

  17. 43750

    {81,33,51,19,97,90,16,61,22,65,49,59,51,72,37,71,87,79,67,89,94,51,41,42,95,29,77,22,77,23,34,11,74,40,84,66,79,16,27,66,33,99,42,42,81,81,60,70,98,80}

    Returns: 747.6857142857143

  18. 72693

    {93,24,5,84,55,17,31,1,10,85,75,16,51,50,67,65,72,22,56,86,70,36,89,99,28,65,23,45,16,8,91,18,84,79,20,98,25,15,85,44,90,43,59,56,23,55,17,2,49,94}

    Returns: 1460.6533333333334

  19. 10137

    {67,12,37,44,8,20,100,30,49,10,50,68,6,25,18,73,60,12,97,89,100,36,98,79,77,79,47,78,3,2,33,22,58,16,23,38,72,63,49,67,78,31,5,90,54,2,19,17,49,57}

    Returns: 219.93258426966293

  20. 46571

    {69,4,31,81,27,67,47,45,53,24,63,10,64,17,48,52,3,80,28,77,1,40,7,19,33,6,88,75,16,60,8,93,81,90,12,94,15,77,67,39,82,54,49,22,62,50,28,1,9,57}

    Returns: 1044.8548387096773

  21. 30930

    {31,4,24,83,4,77,8,12,4,79,45,48,29,64,29,18,43,10,53,97,6,22,86,37,66,17,32,33,14,87,20,90,44,26,68,98,18,34,2,48,13,60,42,90,99,37,20,5,78,34}

    Returns: 742.3571428571429

  22. 97419

    {86,46,38,26,53,73,73,21,93,25,61,91,41,40,34,12,15,96,99,72,22,37,66,10,28,13,74,91,63,53,47,93,96,32,92,59,12,10,91,67,96,42,100,49,11,49,68,73,41,80}

    Returns: 1766.4

  23. 12957

    {74,35,100,14,89,100,55,60,100,20,83,27,85,55,3,43,54,75,95,56,50,91,71,8,78,29,29,20,85,3,59,99,49,34,45,28,20,74,71,80,32,73,85,44,25,78,76,1,33,92}

    Returns: 231.7171717171717

  24. 55875

    {95,91,48,10,51,63,53,35,38,85,73,68,80,56,62,58,62,51,42,15,26,24,58,12,80,74,99,20,9,70,59,95,35,57,17,22,87,72,87,77,19,34,46,74,89,80,30,95,88,38}

    Returns: 995.325

  25. 36231

    {17,52,26,40,73,84,58,21,23,81,69,22,1,22,32,9,74,93,78,62,10,42,10,49,8,11,96,67,12,53,92,49,17,70,58,50,14,71,27,4,65,14,57,75,63,58,11,80,92,49}

    Returns: 784.7413793103449

  26. 100000

    {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}

    Returns: 100000.0

  27. 100000

    {100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100}

    Returns: 1000.0

  28. 9839

    {45, 90, 13, 4, 81, 50, 81, 10, 64, 86, 69 }

    Returns: 182.8395061728395

  29. 100

    {50 }

    Returns: 2.0

  30. 17216

    {26, 30, 62, 55, 51, 56, 58, 4, 60, 23, 31 }

    Returns: 415.03333333333336

  31. 89839

    {45, 90, 13, 4, 81, 50, 81, 10, 64, 86, 69, 13, 1 }

    Returns: 1924.0666666666666

  32. 1

    {10 }

    Returns: 0.1

  33. 2058

    {80, 43, 57, 23, 28, 45, 60, 75, 73, 80 }

    Returns: 37.4

  34. 50

    {23 }

    Returns: 2.1739130434782608

  35. 5

    {10 }

    Returns: 0.5

  36. 76

    {50, 25 }

    Returns: 2.02

  37. 10

    {50 }

    Returns: 0.2

  38. 10

    {20 }

    Returns: 0.5

  39. 9850

    {45, 45, 13, 4, 81, 50, 81, 10, 64, 86, 69 }

    Returns: 197.79710144927537

  40. 2

    {4 }

    Returns: 0.5

  41. 98483

    {45, 90, 13, 4, 81, 50, 81, 10, 64, 86, 69, 11 }

    Returns: 1956.6888888888889

  42. 1000

    {50, 40, 30, 40, 50 }

    Returns: 24.0

  43. 80

    {25 }

    Returns: 3.2

  44. 1

    {2 }

    Returns: 0.5

  45. 10000

    {5, 15 }

    Returns: 1000.0

  46. 31

    {10, 10, 10, 10 }

    Returns: 3.1

  47. 100000

    {1 }

    Returns: 100000.0


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: