-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path1652.cpp
More file actions
26 lines (26 loc) · 783 Bytes
/
1652.cpp
File metadata and controls
26 lines (26 loc) · 783 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution {
public:
vector<int> decrypt(vector<int>& code, int k) {
int n = code.size();
vector<int> res(n, 0);
int curr = 0;
if (k == 0) return res;
if (k > 0) {
for (int i = 1; i <= k; ++i) curr += code[(i + 2 * n) % n];
for (int i = 0; i < n; ++i) {
res[i] = curr;
curr -= code[(i + 1) % n];
curr += code[(i + k + 1) % n];
}
}
else {
for (int i = -1; i >= k; --i) curr += code[(i + 2 * n) % n];
for (int i = 0; i < n; ++i) {
res[i] = curr;
curr -= code[(i + k + 2 * n) % n];
curr += code[(i + 2 * n) % n];
}
}
return res;
}
};