File tree 2 files changed +46
-1
lines changed
2 files changed +46
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,449 LeetCode solutions in JavaScript
1
+ # 1,450 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcodejavascript.com ] ( https://leetcodejavascript.com )
4
4
1272
1272
1647|[ Minimum Deletions to Make Character Frequencies Unique] ( ./solutions/1647-minimum-deletions-to-make-character-frequencies-unique.js ) |Medium|
1273
1273
1648|[ Sell Diminishing-Valued Colored Balls] ( ./solutions/1648-sell-diminishing-valued-colored-balls.js ) |Medium|
1274
1274
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|
1275
1276
1657|[ Determine if Two Strings Are Close] ( ./solutions/1657-determine-if-two-strings-are-close.js ) |Medium|
1276
1277
1668|[ Maximum Repeating Substring] ( ./solutions/1668-maximum-repeating-substring.js ) |Easy|
1277
1278
1669|[ Merge In Between Linked Lists] ( ./solutions/1669-merge-in-between-linked-lists.js ) |Medium|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments