@@ -13,7 +13,7 @@ def minSumOfLengths(self, arr: List[int], target: int) -> int:
13
13
return - 1
14
14
15
15
slidingWindowSum = 0
16
- minLengthEndingBefore = float ('inf' )
16
+ minLengthEndingBefore = [ float ('inf' ) for i in range ( len ( arr ))]
17
17
start = 0
18
18
result = float ('inf' )
19
19
for end in range (len (arr )):
@@ -25,10 +25,12 @@ def minSumOfLengths(self, arr: List[int], target: int) -> int:
25
25
if slidingWindowSum == target :
26
26
currSubarrayLen = end - start + 1
27
27
if minLengthEndingBefore != float ('inf' ):
28
- result = min (result , currSubarrayLen + minLengthEndingBefore )
29
- minLengthEndingBefore = min (minLengthEndingBefore , currSubarrayLen )
30
- else :
31
- continue
28
+ if start - 1 >= 0 :
29
+ result = min (result , currSubarrayLen + minLengthEndingBefore [start - 1 ])
30
+ minLengthEndingBefore [end ] = min (minLengthEndingBefore [end ], currSubarrayLen )
31
+
32
+ if end >= 1 :
33
+ minLengthEndingBefore [end ] = min (minLengthEndingBefore [end - 1 ], minLengthEndingBefore [end ])
32
34
33
35
return result if result != float ('inf' ) else - 1
34
36
@@ -39,5 +41,8 @@ def test_Leetcode(self):
39
41
self .assertEqual (- 1 , self .minSumOfLengths ([5 , 5 , 4 , 4 , 5 ], 3 ))
40
42
self .assertEqual (3 , self .minSumOfLengths ([3 , 1 , 1 , 1 , 5 , 1 , 2 , 1 ], 3 ))
41
43
44
+ def test_Wrongcase (self ):
45
+ self .assertEqual (- 1 , self .minSumOfLengths ([1 , 6 , 1 ], 7 ))
46
+
42
47
if __name__ == '__main__' :
43
48
unittest .main ()
0 commit comments