MCPcopy Create free account
hub / github.com/HuberTRoy/leetCode / Solution

Class Solution

DP/SubarrayProductLessThanK.py:26–63  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

24可剪枝优化。
25"""
26class Solution(object):
27 def numSubarrayProductLessThanK(self, nums, k):
28 """
29 :type nums: List[int]
30 :type k: int
31 :rtype: int
32 """
33
34 dp = []
35 result = 0
36 start = 0
37 for i in range(len(nums)):
38 if nums[i] < k:
39 result += 1
40 dp = [nums[i]]
41 start = i
42 break
43
44 for i in range(start+1, len(nums)):
45 if nums[i] == 1 and nums[i] < k:
46 dp.append(1)
47 result += len(dp)
48 continue
49
50 new = []
51
52 if nums[i] < k:
53 result += 1
54 new.append(nums[i])
55
56
57 for j in dp:
58 if j * nums[i] < k:
59 result += 1
60 new.append(j * nums[i])
61
62 dp = new
63 return result
64

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected