File tree 1 file changed +43
-0
lines changed
1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * https://leetcode-cn.com/problems/pizza-with-3n-slices/comments/
3
+ *
4
+ * 5351. 3n 块披萨
5
+ *
6
+ * Hard
7
+ *
8
+ * dp[i][j] 前 i 块披萨中拿 j 块最大和值。
9
+ *
10
+ * 96ms 100.00%
11
+ *
12
+ * 37.6mb 100.00%
13
+ */
14
+
15
+ const maxSizeSlices = slices => {
16
+ let ans = 0 ;
17
+ const max = slices . length ;
18
+ const maxCanHava = Math . floor ( max / 3 ) ;
19
+ let dp = Array . from ( { length : max } , ( ) => Array ( maxCanHava + 1 ) . fill ( 0 ) ) ;
20
+
21
+ // 第一块拿,最后一块不能拿
22
+ dp [ 0 ] [ 1 ] = slices [ 0 ] ;
23
+
24
+ for ( let i = 1 ; i < max - 1 ; i ++ ) {
25
+ for ( let j = 1 ; j <= maxCanHava ; j ++ ) {
26
+ const pre = i - 2 >= 0 ? dp [ i - 2 ] [ j - 1 ] : 0 ;
27
+ dp [ i ] [ j ] = Math . max ( dp [ i - 1 ] [ j ] , pre + slices [ i ] )
28
+ }
29
+ ans = Math . max ( dp [ i ] [ maxCanHava ] , ans ) ;
30
+ }
31
+
32
+ dp = Array . from ( { length : max } , ( ) => Array ( maxCanHava + 1 ) . fill ( 0 ) ) ;
33
+ // 第一块不拿,最后一块拿
34
+ for ( let i = 1 ; i < max ; i ++ ) {
35
+ for ( let j = 1 ; j <= maxCanHava ; j ++ ) {
36
+ const pre = i - 2 >= 0 ? dp [ i - 2 ] [ j - 1 ] : 0 ;
37
+ dp [ i ] [ j ] = Math . max ( dp [ i - 1 ] [ j ] , pre + slices [ i ] )
38
+ }
39
+ ans = Math . max ( dp [ i ] [ maxCanHava ] , ans ) ;
40
+ }
41
+
42
+ return ans ;
43
+ }
You can’t perform that action at this time.
0 commit comments