File tree 1 file changed +11
-10
lines changed
scripts/algorithms/F/Filling Bookcase Shelves
1 file changed +11
-10
lines changed Original file line number Diff line number Diff line change
1
+ // Runtime: 92 ms (Top 69.01%) | Memory: 42.4 MB (Top 98.59%)
1
2
var minHeightShelves = function ( books , shelfWidth ) {
2
3
let booksArr = [ ]
3
-
4
+
4
5
for ( let i = 0 ; i < books . length ; i ++ ) {
5
6
let remainingWidth = shelfWidth - books [ i ] [ 0 ]
6
7
let bookHeight = books [ i ] [ 1 ]
7
-
8
+
8
9
let maxHeight = bookHeight
9
-
10
+
10
11
let prevSum = booksArr [ i - 1 ] !== undefined ? booksArr [ i - 1 ] : 0
11
12
let minSumHeight = bookHeight + prevSum
12
-
13
+
13
14
for ( let x = i - 1 ; x >= 0 ; x -- ) {
14
-
15
+
15
16
let prevBookWidth = books [ x ] [ 0 ]
16
17
let prevBookHeight = books [ x ] [ 1 ]
17
18
if ( remainingWidth - prevBookWidth < 0 ) break
18
19
remainingWidth -= prevBookWidth
19
-
20
+
20
21
prevSum = booksArr [ x - 1 ] !== undefined ? booksArr [ x - 1 ] : 0
21
-
22
+
22
23
maxHeight = Math . max ( maxHeight , prevBookHeight )
23
-
24
- minSumHeight = Math . min ( prevSum + maxHeight , minSumHeight )
24
+
25
+ minSumHeight = Math . min ( prevSum + maxHeight , minSumHeight )
25
26
}
26
27
booksArr [ i ] = minSumHeight
27
28
}
28
29
29
30
return booksArr [ books . length - 1 ]
30
- } ;
31
+ } ;
You can’t perform that action at this time.
0 commit comments