-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.py
50 lines (47 loc) · 1.48 KB
/
solution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# A runner, who runs with base speed s with duration t will cover a distances d: d = s * t
# However, this runner can sprint for one unit of time with double speed s * 2
# After sprinting, base speed s will permanently reduced by 1, and for next one unit of time runner will enter recovery
# phase and can't sprint again.
# Your task, given base speed s and time t, is to find the maximum possible distance d.
# Input:
# 1 <= s < 1000
# 1 <= t < 1000
# Example:
# Given s = 2 and t = 4.
# We could schedule when runner should sprint so we could get these possible sequences:
# Seq.: RRRR
# => s + s + s + s
# => 2 + 2 + 2 + 2 = 8
# Seq.: RRRS
# => s + s + s + s*2
# => 2 + 2 + 2 + 2*2 = 10
# Seq.: RRSR
# => s + s + s*2 + (s-1)
# => 2 + 2 + 2*2 + (2-1) = 9
# Seq.: RSRR
# => s + s*2 + (s-1) + (s-1)
# => 2 + 2*2 + (2-1) + (2-1) = 8
# Seq.: RSRS
# => s + s*2 + (s-1) + (s-1)*2
# => 2 + 2*2 + (2-1) + (2-1)*2 = 9
# Seq.: SRRR
# => s*2 + (s-1) + (s-1) + (s-1)
# => 2*2 + (2-1) + (2-1) + (2-1) = 7
# Seq.: SRRS
# => s*2 + (s-1) + (s-1) + (s-1)*2
# => 2*2 + (2-1) + (2-1) + (2-1)*2 = 8
# Seq.: SRSR
# => s*2 + (s-1) + (s-1)*2 + (s-1-1)
# => 2*2 + (2-1) + (2-1)*2 + (2-1-1) = 7
# Where:
# - R: Normal Run / Recovery
# - S: Sprint
# Based on above sequences, the maximum possible distance d is 10.
import math
def solution(speed, time):
sprints = math.ceil(time / 2)
distance = speed * time
for i in range(sprints):
if speed - 3 * i > 0:
distance += speed - 3 * i
return distance