File tree Expand file tree Collapse file tree 3 files changed +94
-0
lines changed
codewars/chasers-schedule Expand file tree Collapse file tree 3 files changed +94
-0
lines changed Original file line number Diff line number Diff line change
1
+ # Chasers Schedule
2
+
3
+ ## Description
4
+
5
+ A runner, who runs with base speed s with duration t will cover a distances d: d = s * t
6
+ However, this runner can sprint for one unit of time with double speed s * 2
7
+ 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.
8
+
9
+ Your task, given base speed s and time t, is to find the maximum possible distance d.
10
+
11
+ Input:
12
+ 1 <= s < 1000
13
+ 1 <= t < 1000
14
+
15
+ Example:
16
+ Given s = 2 and t = 4.
17
+ We could schedule when runner should sprint so we could get these possible sequences:
18
+
19
+ ``` txt
20
+ Seq.: RRRR
21
+ => s + s + s + s
22
+ => 2 + 2 + 2 + 2 = 8
23
+ Seq.: RRRS
24
+ => s + s + s + s*2
25
+ => 2 + 2 + 2 + 2*2 = 10
26
+ Seq.: RRSR
27
+ => s + s + s*2 + (s-1)
28
+ => 2 + 2 + 2*2 + (2-1) = 9
29
+ Seq.: RSRR
30
+ => s + s*2 + (s-1) + (s-1)
31
+ => 2 + 2*2 + (2-1) + (2-1) = 8
32
+ Seq.: RSRS
33
+ => s + s*2 + (s-1) + (s-1)*2
34
+ => 2 + 2*2 + (2-1) + (2-1)*2 = 9
35
+ Seq.: SRRR
36
+ => s*2 + (s-1) + (s-1) + (s-1)
37
+ => 2*2 + (2-1) + (2-1) + (2-1) = 7
38
+ Seq.: SRRS
39
+ => s*2 + (s-1) + (s-1) + (s-1)*2
40
+ => 2*2 + (2-1) + (2-1) + (2-1)*2 = 8
41
+ Seq.: SRSR
42
+ => s*2 + (s-1) + (s-1)*2 + (s-1-1)
43
+ => 2*2 + (2-1) + (2-1)*2 + (2-1-1) = 7
44
+ ```
45
+
46
+ Where:
47
+ - R: Normal Run / Recovery
48
+ - S: Sprint
49
+ Based on above sequences, the maximum possible distance d is 10.
50
+
Original file line number Diff line number Diff line change
1
+ function chaserSchedule ( speed , time ) {
2
+ let distance = speed * time ;
3
+ const maxSprints = Math . ceil ( time / 2 ) ;
4
+
5
+ for ( let i = 0 ; i < maxSprints ; i ++ ) {
6
+ if ( speed - 3 * i > 0 ) {
7
+ distance += speed - 3 * i ;
8
+ }
9
+ }
10
+ return distance ;
11
+ }
12
+
13
+ module . exports = chaserSchedule ;
Original file line number Diff line number Diff line change
1
+ const chaserSchedule = require ( './solution' ) ;
2
+
3
+ describe ( 'Chasers Schedule' , ( ) => {
4
+ const testCases = [
5
+ {
6
+ input : [ 2 , 4 ] ,
7
+ expected : 10 ,
8
+ } ,
9
+ {
10
+ input : [ 1 , 1 ] ,
11
+ expected : 2 ,
12
+ } ,
13
+ {
14
+ input : [ 829 , 135 ] ,
15
+ expected : 161453 ,
16
+ } ,
17
+ {
18
+ input : [ 742 , 775 ] ,
19
+ expected : 667182 ,
20
+ } ,
21
+ ] ;
22
+
23
+ it ( 'should return a number type' , ( ) => {
24
+ expect ( typeof chaserSchedule ( 1 , 1 ) ) . toBe ( 'number' ) ;
25
+ } ) ;
26
+
27
+ it . each ( testCases ) ( 'should return $output' , ( testCase ) => {
28
+ const { input, expected } = testCase ;
29
+ expect ( chaserSchedule ( ...input ) ) . toBe ( expected ) ;
30
+ } ) ;
31
+ } ) ;
You can’t perform that action at this time.
0 commit comments