Skip to content

Commit 84eacb7

Browse files
committed
Add solution #1652
1 parent 03533c4 commit 84eacb7

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,449 LeetCode solutions in JavaScript
1+
# 1,450 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1272,6 +1272,7 @@
12721272
1647|[Minimum Deletions to Make Character Frequencies Unique](./solutions/1647-minimum-deletions-to-make-character-frequencies-unique.js)|Medium|
12731273
1648|[Sell Diminishing-Valued Colored Balls](./solutions/1648-sell-diminishing-valued-colored-balls.js)|Medium|
12741274
1649|[Create Sorted Array through Instructions](./solutions/1649-create-sorted-array-through-instructions.js)|Hard|
1275+
1652|[Defuse the Bomb](./solutions/1652-defuse-the-bomb.js)|Easy|
12751276
1657|[Determine if Two Strings Are Close](./solutions/1657-determine-if-two-strings-are-close.js)|Medium|
12761277
1668|[Maximum Repeating Substring](./solutions/1668-maximum-repeating-substring.js)|Easy|
12771278
1669|[Merge In Between Linked Lists](./solutions/1669-merge-in-between-linked-lists.js)|Medium|

solutions/1652-defuse-the-bomb.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* 1652. Defuse the Bomb
3+
* https://leetcode.com/problems/defuse-the-bomb/
4+
* Difficulty: Easy
5+
*
6+
* You have a bomb to defuse, and your time is running out! Your informer will provide you with
7+
* a circular array code of length of n and a key k.
8+
*
9+
* To decrypt the code, you must replace every number. All the numbers are replaced simultaneously.
10+
* - If k > 0, replace the ith number with the sum of the next k numbers.
11+
* - If k < 0, replace the ith number with the sum of the previous k numbers.
12+
* - If k == 0, replace the ith number with 0.
13+
*
14+
* As code is circular, the next element of code[n-1] is code[0], and the previous element of
15+
* code[0] is code[n-1].
16+
*
17+
* Given the circular array code and an integer key k, return the decrypted code to defuse the bomb!
18+
*/
19+
20+
/**
21+
* @param {number[]} code
22+
* @param {number} k
23+
* @return {number[]}
24+
*/
25+
var decrypt = function(code, k) {
26+
const n = code.length;
27+
const result = new Array(n).fill(0);
28+
29+
if (k === 0) return result;
30+
31+
const isPositive = k > 0;
32+
const steps = Math.abs(k);
33+
34+
for (let i = 0; i < n; i++) {
35+
let sum = 0;
36+
for (let j = 1; j <= steps; j++) {
37+
const index = isPositive ? (i + j) % n : (i - j + n) % n;
38+
sum += code[index];
39+
}
40+
result[i] = sum;
41+
}
42+
43+
return result;
44+
};

0 commit comments

Comments
 (0)