File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[] } slices
3
+ * @return {number }
4
+ */
5
+ var maxSizeSlices = function ( slices ) {
6
+ const count = slices . length / 3 ;
7
+ let result = 0 ;
8
+
9
+ const dp = [ ] ;
10
+ for ( let i = 0 ; i < count ; i ++ ) {
11
+ dp [ i ] = [ ] ;
12
+ let frontMax = 0 ;
13
+ for ( let j = 0 ; j < slices . length - 1 ; j ++ ) {
14
+ if ( i === 0 ) {
15
+ dp [ i ] [ j ] = slices [ j ] ;
16
+ } else {
17
+ frontMax = Math . max ( frontMax , ( dp [ i - 1 ] [ j - 2 ] || 0 ) ) ;
18
+ dp [ i ] [ j ] = Math . max ( frontMax + slices [ j ] ) ;
19
+ }
20
+ result = Math . max ( result , dp [ i ] [ j ] ) ;
21
+ }
22
+ }
23
+
24
+ for ( let i = 0 ; i < count ; i ++ ) {
25
+ dp [ i ] = [ ] ;
26
+ let frontMax = 0 ;
27
+ for ( let j = 1 ; j < slices . length ; j ++ ) {
28
+ if ( i === 0 ) {
29
+ dp [ i ] [ j - 1 ] = slices [ j ] ;
30
+ } else {
31
+ frontMax = Math . max ( frontMax , ( dp [ i - 1 ] [ j - 3 ] || 0 ) ) ;
32
+ dp [ i ] [ j - 1 ] = Math . max ( frontMax + slices [ j ] ) ;
33
+ }
34
+ result = Math . max ( result , dp [ i ] [ j - 1 ] ) ;
35
+ }
36
+ }
37
+
38
+ return result ;
39
+ } ;
You can’t perform that action at this time.
0 commit comments