Skip to content

Commit 50caa36

Browse files
authored
Create product-of-two-run-length-encoded-arrays.py
1 parent 42dbd7f commit 50caa36

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Time: O(m + n)
2+
# Space: O(1)
3+
4+
class Solution(object):
5+
def findRLEArray(self, encoded1, encoded2):
6+
"""
7+
:type encoded1: List[List[int]]
8+
:type encoded2: List[List[int]]
9+
:rtype: List[List[int]]
10+
"""
11+
result = []
12+
i = j = remain1 = remain2 = 0
13+
while (remain1 or i < len(encoded1)) and (remain2 or j < len(encoded2)):
14+
if not remain1:
15+
remain1 = encoded1[i][1]
16+
i += 1
17+
if not remain2:
18+
remain2 = encoded2[j][1]
19+
j += 1
20+
cnt = min(remain1, remain2)
21+
remain1 -= cnt
22+
remain2 -= cnt
23+
if result and result[-1][0] == encoded1[i-1][0]*encoded2[j-1][0]:
24+
result[-1][1] += cnt
25+
else:
26+
result.append([encoded1[i-1][0]*encoded2[j-1][0], cnt])
27+
return result

0 commit comments

Comments
 (0)