Skip to content

Commit d0a6b31

Browse files
committed
added a folder(1600-1700) and 1652.py
1 parent a365e95 commit d0a6b31

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

1600-1700/1652.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
'''
2+
You have a bomb to defuse, and your time is running out! Your informer will provide you with a circular array code of length of n and a key k.
3+
4+
To decrypt the code, you must replace every number. All the numbers are replaced simultaneously.
5+
6+
If k > 0, replace the ith number with the sum of the next k numbers.
7+
If k < 0, replace the ith number with the sum of the previous k numbers.
8+
If k == 0, replace the ith number with 0.
9+
As code is circular, the next element of code[n-1] is code[0], and the previous element of code[0] is code[n-1].
10+
11+
Given the circular array code and an integer key k, return the decrypted code to defuse the bomb!
12+
13+
Example 1:
14+
15+
Input: code = [5,7,1,4], k = 3
16+
Output: [12,10,16,13]
17+
Explanation: Each number is replaced by the sum of the next 3 numbers. The decrypted code is [7+1+4, 1+4+5, 4+5+7, 5+7+1]. Notice that the numbers wrap around.
18+
Example 2:
19+
20+
Input: code = [1,2,3,4], k = 0
21+
Output: [0,0,0,0]
22+
Explanation: When k is zero, the numbers are replaced by 0.
23+
Example 3:
24+
25+
Input: code = [2,4,9,3], k = -2
26+
Output: [12,5,6,13]
27+
Explanation: The decrypted code is [3+9, 2+3, 4+2, 9+4]. Notice that the numbers wrap around again. If k is negative, the sum is of the previous numbers.
28+
'''
29+
class Solution(object):
30+
def decrypt(self, code, k):
31+
"""
32+
:type code: List[int]
33+
:type k: int
34+
:rtype: List[int]
35+
"""
36+
37+
n = len(code)
38+
39+
decryptedCode = [0] * n
40+
41+
for index in range(n):
42+
if k > 0:
43+
total = 0
44+
for x in range(1, k+1):
45+
total += code[(index + x)%n]
46+
decryptedCode[index] = total
47+
48+
elif k < 0:
49+
total = 0
50+
for x in range(1,-k+1):
51+
total += code[(index - x)%n]
52+
decryptedCode[index] = total
53+
else:
54+
decryptedCode[index] = 0
55+
return decryptedCode

0 commit comments

Comments
 (0)