Skip to content

Commit b2f219f

Browse files
committed
Runtime: 878 ms (Top 47.91%) | Memory: 16.1 MB (Top 47.44%)
1 parent 828f7f0 commit b2f219f

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

scripts/algorithms/S/Strange Printer/Strange Printer.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# Runtime: 878 ms (Top 47.91%) | Memory: 16.1 MB (Top 47.44%)
12
class Solution(object):
23
def strangePrinter(self, s):
34
"""
@@ -19,22 +20,21 @@ def _dp(i, j, background):
1920
return 1 if background != s[i] else 0
2021
elif (i, j, background) in _m:
2122
return _m[(i, j, background)]
22-
23+
2324
ans = len(s)
24-
25+
2526
# shrink s[i:j+1] to s[i_:j_+1] according to the background letter
2627
i_ = i + 1 if s[i] == background else i
2728
j_ = j - 1 if s[j] == background else j
28-
29-
29+
3030
if s[i_] == s[j_]:
31-
# case "AxxxA" => best strategy is printing A first
31+
# case "AxxxA" => best strategy is printing A first
3232
ans = _dp(i_ + 1, j_ - 1, s[i_]) + 1
3333
else:
34-
# otherwise, print first letter, try every possible print length
34+
# otherwise, print first letter, try every possible print length
3535
for p in range(i_, j_ + 1):
3636
# searching is needed only if s[p] == s[i_]
37-
# e.g. s="ABCDEA"print 'A' on s[0:1] is equivalent to s[0:5]
37+
# e.g. s="ABCDEA"print 'A' on s[0:1] is equivalent to s[0:5]
3838
if s[p] != s[i_]:
3939
continue
4040
l = _dp(i_, p, s[i_])
@@ -43,4 +43,4 @@ def _dp(i, j, background):
4343
_m[(i, j, background)] = ans
4444
return ans
4545

46-
return _dp(0, len(s) - 1, '')
46+
return _dp(0, len(s) - 1, '')

0 commit comments

Comments
 (0)