File tree 1 file changed +45
-0
lines changed
scripts/algorithms/M/Minimum Time to Repair Cars
1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change
1
+ // Runtime: 89 ms (Top 100.0%) | Memory: 48.20 MB (Top 87.5%)
2
+
3
+ /**
4
+ * @param {number[] } ranks
5
+ * @param {number } cars
6
+ * @return {number }
7
+ */
8
+ var repairCars = function ( ranks , cars ) {
9
+ let counts = new Array ( 100 ) . fill ( 0 ) ;
10
+
11
+ for ( let i = 0 ; i < ranks . length ; i ++ ) {
12
+ counts [ ranks [ i ] - 1 ] ++ ;
13
+ }
14
+
15
+ let high = cars * cars * ranks [ 0 ] ;
16
+ let low = 0 ;
17
+
18
+ let best = high ;
19
+
20
+ while ( high > low ) {
21
+ let mid = Math . floor ( low + ( ( high - low ) / 2 ) ) ;
22
+
23
+ if ( carsInMinutes ( counts , mid ) >= cars ) {
24
+ best = Math . min ( best , mid ) ;
25
+
26
+ high = mid ;
27
+ } else {
28
+ low = mid + 1 ;
29
+ }
30
+ }
31
+
32
+ return best ;
33
+ } ;
34
+
35
+ function carsInMinutes ( counts , minutes ) {
36
+ let cars = 0 ;
37
+
38
+ for ( let i = 0 ; i < 100 ; i ++ ) {
39
+ cars += Math . floor ( Math . sqrt ( minutes / ( i + 1 ) ) ) * counts [ i ] ;
40
+ }
41
+
42
+ return cars ;
43
+ }
44
+
45
+ // binary search number of minutes
You can’t perform that action at this time.
0 commit comments