Skip to content

Commit d1a4fab

Browse files
authored
Create 1652. Defuse the Bomb (#638)
2 parents 54223c1 + 9d27de3 commit d1a4fab

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

1652. Defuse the Bomb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
vector<int> decrypt(vector<int>& code, int k) {
4+
const int n = code.size();
5+
vector<int> ans(n);
6+
if (k == 0)
7+
return ans;
8+
int sum = 0;
9+
int start = k > 0 ? 1 : n + k;
10+
int end = k > 0 ? k : n - 1;
11+
for (int i = start; i <= end; ++i)
12+
sum += code[i];
13+
for (int i = 0; i < n; ++i) {
14+
ans[i] = sum;
15+
sum -= code[start++ % n];
16+
sum += code[++end % n];
17+
}
18+
return ans;
19+
}
20+
};

0 commit comments

Comments
 (0)