Skip to content

Commit 79d4c11

Browse files
authored
Create defuse-the-bomb.cpp
1 parent ee1e201 commit 79d4c11

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

C++/defuse-the-bomb.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
vector<int> decrypt(vector<int>& code, int k) {
7+
vector<int> result(size(code));
8+
if (k == 0) {
9+
return result;
10+
}
11+
int left = 1, right = k;
12+
if (k < 0) {
13+
k = -k;
14+
left = size(code) - k, right = size(code) - 1;
15+
}
16+
int total = accumulate(begin(code) + left, begin(code) + right + 1, 0);
17+
for (int i = 0; i < size(code); ++i) {
18+
result[i] = total;
19+
total -= code[left++ % size(code)];
20+
total += code[++right % size(code)];
21+
}
22+
return result;
23+
}
24+
};

0 commit comments

Comments
 (0)