Skip to content

Commit 7009c85

Browse files
authored
Create choose-numbers-from-two-arrays-in-range.py
1 parent 08ccbcc commit 7009c85

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Time: O(n^2 * v), v is max(max(nums1), max(nums2))
2+
# Space: O(n * v)
3+
4+
import collections
5+
import itertools
6+
7+
8+
# dp
9+
class Solution(object):
10+
def countSubranges(self, nums1, nums2):
11+
"""
12+
:type nums1: List[int]
13+
:type nums2: List[int]
14+
:rtype: int
15+
"""
16+
MOD = 10**9+7
17+
18+
result = 0
19+
dp = collections.Counter()
20+
for x, y in itertools.izip(nums1, nums2):
21+
new_dp = collections.Counter()
22+
new_dp[x] += 1
23+
new_dp[-y] += 1
24+
for v, c in dp.iteritems():
25+
new_dp[v+x] = (new_dp[v+x]+c)%MOD
26+
new_dp[v-y] = (new_dp[v-y]+c)%MOD
27+
dp = new_dp
28+
result = (result+dp[0])%MOD
29+
return result

0 commit comments

Comments
 (0)