Skip to content

Commit 8c8579c

Browse files
committed
2023-11-03 v. 3.4.5: added "1652. Defuse the Bomb"
1 parent 688333a commit 8c8579c

File tree

4 files changed

+55
-1
lines changed

4 files changed

+55
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,3 +291,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
291291
| 1629. Slowest Key | [Link](https://leetcode.com/problems/slowest-key/) | [Link](./lib/easy/1629_slowest_key.rb) |
292292
| 1636. Sort Array by Increasing Frequency | [Link](https://leetcode.com/problems/sort-array-by-increasing-frequency/) | [Link](./lib/easy/1636_sort_array_by_increasing_frequency.rb) |
293293
| 1646. Get Maximum in Generated Array | [Link](https://leetcode.com/problems/get-maximum-in-generated-array/) | [Link](./lib/easy/1646_get_maximum_in_generated_array.rb) |
294+
| 1652. Defuse the Bomb | [Link](https://leetcode.com/problems/defuse-the-bomb/) | [Link](./lib/easy/1652_defuse_the_bomb.rb) |

leetcode-ruby.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ require 'English'
55
::Gem::Specification.new do |s|
66
s.required_ruby_version = '>= 3.0'
77
s.name = 'leetcode-ruby'
8-
s.version = '3.4.4'
8+
s.version = '3.4.5'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[bin/leetcode-ruby README.md LICENSE]
1111
s.executable = 'leetcode-ruby'

lib/easy/1652_defuse_the_bomb.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/defuse-the-bomb/
4+
# @param {Integer[]} code
5+
# @param {Integer} k
6+
# @return {Integer[]}
7+
def decrypt(code, k)
8+
result = ::Array.new(code.length, 0)
9+
10+
return result if k.zero?
11+
12+
positive = k.positive?
13+
limit = positive ? k : -k
14+
break_value = positive ? code.length : -1
15+
next_index = positive ? 0 : code.length - 1
16+
17+
(0...code.length).each do |i|
18+
count = 0
19+
j = positive ? i + 1 : i - 1
20+
sum = 0
21+
22+
until count == limit
23+
j = next_index if j == break_value
24+
25+
index = j
26+
if positive
27+
j += 1
28+
else
29+
j -= 1
30+
end
31+
32+
sum += code[index]
33+
count += 1
34+
end
35+
36+
result[i] = sum
37+
end
38+
39+
result
40+
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/easy/1652_defuse_the_bomb'
5+
require 'minitest/autorun'
6+
7+
class DefuseTheBombTest < ::Minitest::Test
8+
def test_default
9+
assert_equal([12, 10, 16, 13], decrypt([5, 7, 1, 4], 3))
10+
assert_equal([0, 0, 0, 0], decrypt([1, 2, 3, 4], 0))
11+
assert_equal([12, 5, 6, 13], decrypt([2, 4, 9, 3], -2))
12+
end
13+
end

0 commit comments

Comments
 (0)