1
 class Solution:
2
def maxSizeSlices(self, slices: List[int]) -> int:
3
** #This solve function mainly on work on the idea of A Previous dp problem House Robber II
4
#If we take the first slice then we cant take the second slice and vice versa**
5
def solve(slices,start,end,n,dp):
6
if start>end or n==0:
7
return 0
8
if dp[start][n] !=-1:
9
return dp[start][n]
10
include = slices[start] + solve(slices,start+2,end,n-1,dp)
11

12
exclude = 0 + solve(slices,start+1,end,n,dp)
13

14
dp[start][n]= max(include,exclude)
15
return dp[start][n]
16
dp1=[[-1 for i in range(k+1)]for _ in range(k+1)]
17
dp2=[[-1 for i in range(k+1)]for _ in range(k+1)]
18

19
option1=solve(slices,0,k-2,k//3,dp1)#Taking the the first slice , now we cant take the last slice and next slice
20
option2=solve(slices,1,k-1,k//3,dp2)#Taking the the second slice , now we cant take the second last slice and next slice
21

22
return max(option1,option2)

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0