Skip to content

Commit d5332ac

Browse files
authored
Create defuse-the-bomb.py
1 parent 79d4c11 commit d5332ac

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

Python/defuse-the-bomb.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
4+
class Solution(object):
5+
def decrypt(self, code, k):
6+
"""
7+
:type code: List[int]
8+
:type k: int
9+
:rtype: List[int]
10+
"""
11+
result = [0]*len(code)
12+
if k == 0:
13+
return result
14+
left, right = 1, k
15+
if k < 0:
16+
k = -k
17+
left, right = len(code)-k, len(code)-1
18+
total = sum(code[i] for i in xrange(left, right+1))
19+
for i in xrange(len(code)):
20+
result[i] = total
21+
total -= code[left%len(code)]
22+
total += code[(right+1)%len(code)]
23+
left += 1
24+
right += 1
25+
return result

0 commit comments

Comments
 (0)