Skip to content

Commit 58e9350

Browse files
committedNov 27, 2024
feat: add Ruby solutions
1 parent 6779119 commit 58e9350

11 files changed

+735
-0
lines changed
 

Diff for: ‎README.md

+10
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@
266266
| 744 | Find Smallest Letter Greater Than Target | [Ruby](./algorithms/ruby/0744-find-smallest-letter-greater-than-target.rb) | Easy |
267267
| 746 | Min Cost Climbing Stairs | [Ruby](./algorithms/ruby/0746-min-cost-climbing-stairs.rb) | Easy |
268268
| 767 | Reorganize String | [Ruby](./algorithms/ruby/0767-reorganize-string.rb) | Medium |
269+
| 773 | Sliding Puzzle | [Ruby](./algorithms/ruby/0773-sliding-puzzle.rb) | Hard |
269270
| 779 | K-th Symbol in Grammar | [Ruby](./algorithms/ruby/0779-k-th-symbol-in-grammar.rb) | Medium |
270271
| 783 | Minimum Distance Between BST Nodes | [Ruby](./algorithms/ruby/0783-minimum-distance-between-bst-nodes.rb) [Python3](./algorithms/python3/0783-minimum-distance-between-bst-nodes.py) | Easy |
271272
| 785 | Is Graph Bipartite? | [Ruby](./algorithms/ruby/0785-is-graph-bipartite.rb) | Medium |
@@ -323,6 +324,7 @@
323324
| 1046 | Last Stone Weight | [Ruby](./algorithms/ruby/1046-last-stone-weight.rb) | Easy |
324325
| 1048 | Longest String Chain | [Ruby](./algorithms/ruby/1048-longest-string-chain.rb) | Medium |
325326
| 1071 | Greatest Common Divisor of Strings | [Ruby](./algorithms/ruby/1071-greatest-common-divisor-of-strings.rb) | Easy |
327+
| 1072 | Flip Columns For Maximum Number of Equal Rows | [Ruby](./algorithms/ruby/1072-flip-columns-for-maximum-number-of-equal-rows.rb) | Medium |
326328
| 1091 | Shortest Path in Binary Matrix | [Ruby](./algorithms/ruby/1091-shortest-path-in-binary-matrix.rb) | Medium |
327329
| 1106 | Parsing A Boolean Expression | [Ruby](./algorithms/ruby/1106-parsing-a-boolean-expression.rb) | Hard |
328330
| 1125 | Smallest Sufficient Team | [Ruby](./algorithms/ruby/1125-smallest-sufficient-team.rb) | Hard |
@@ -414,6 +416,7 @@
414416
| 1637 | Widest Vertical Area Between Two Points Containing No Points | [Ruby](./algorithms/ruby/1637-widest-vertical-area-between-two-points-containing-no-points.rb) | Medium |
415417
| 1639 | Number of Ways to Form a Target String Given a Dictionary | [Ruby](./algorithms/ruby/1639-number-of-ways-to-form-a-target-string-given-a-dictionary.rb) | Hard |
416418
| 1647 | Minimum Deletions to Make Character Frequencies Unique | [Ruby](./algorithms/ruby/1647-minimum-deletions-to-make-character-frequencies-unique.rb) | Medium |
419+
| 1652 | Defuse the Bomb | [Ruby](./algorithms/ruby/1652-defuse-the-bomb.rb) | Easy |
417420
| 1657 | Determine if Two Strings Are Close | [Ruby](./algorithms/ruby/1657-determine-if-two-strings-are-close.rb) | Medium |
418421
| 1658 | Minimum Operations to Reduce X to Zero | [Ruby](./algorithms/ruby/1658-minimum-operations-to-reduce-x-to-zero.rb) | Medium |
419422
| 1662 | Check If Two String Arrays are Equivalent | [Ruby](./algorithms/ruby/1662-check-if-two-string-arrays-are-equivalent.rb) | Easy |
@@ -443,6 +446,7 @@
443446
| 1845 | Seat Reservation Manager | [Ruby](./algorithms/ruby/1845-seat-reservation-manager.rb) | Medium |
444447
| 1846 | Maximum Element After Decreasing and Rearranging | [Ruby](./algorithms/ruby/1846-maximum-element-after-decreasing-and-rearranging.rb) | Medium |
445448
| 1857 | Largest Color Value in a Directed Graph | [Ruby](./algorithms/ruby/1857-largest-color-value-in-a-directed-graph.rb) | Hard |
449+
| 1861 | Rotating the Box | [Ruby](./algorithms/ruby/1861-rotating-the-box.rb) | Medium |
446450
| 1870 | Minimum Speed to Arrive on Time | [Ruby](./algorithms/ruby/1870-minimum-speed-to-arrive-on-time.rb) | Medium |
447451
| 1877 | Minimize Maximum Pair Sum in Array | [Ruby](./algorithms/ruby/1877-minimize-maximum-pair-sum-in-array.rb) | Medium |
448452
| 1887 | Minimize Maximum Pair Sum in Array | [Ruby](./algorithms/ruby/1887-reduction-operations-to-make-the-array-elements-equal.rb) | Medium |
@@ -453,6 +457,7 @@
453457
| 1930 | Unique Length-3 Palindromic Subsequences | [Ruby](./algorithms/ruby/1930-unique-length-3-palindromic-subsequences.rb) | Medium |
454458
| 1964 | Find the Longest Valid Obstacle Course at Each Position | [Ruby](./algorithms/ruby/1964-find-the-longest-valid-obstacle-course-at-each-position.rb) | Hard |
455459
| 1970 | Last Day Where You Can Still Cross | [Ruby](./algorithms/ruby/1970-last-day-where-you-can-still-cross.rb) | Hard |
460+
| 1975 | Maximum Matrix Sum | [Ruby](./algorithms/ruby/1975-maximum-matrix-sum.rb) | Medium |
456461
| 1980 | Find Unique Binary String | [Ruby](./algorithms/ruby/1980-find-unique-binary-string.rb) | Medium |
457462
| 2009 | Minimum Number of Operations to Make Array Continuous | [Ruby](./algorithms/ruby/2009-minimum-number-of-operations-to-make-array-continuous.rb) | Hard |
458463
| 2024 | Maximize the Confusion of an Exam | [Ruby](./algorithms/ruby/2024-maximize-the-confusion-of-an-exam.rb) | Medium |
@@ -472,6 +477,7 @@
472477
| 2215 | Find the Difference of Two Arrays | [Ruby](./algorithms/ruby/2215-find-the-difference-of-two-arrays.rb) | Easy |
473478
| 2218 | Maximum Value of K Coins From Piles | [Ruby](./algorithms/ruby/2218-maximum-value-of-k-coins-from-piles.rb) | Hard |
474479
| 2251 | Number of Flowers in Full Bloom | [Ruby](./algorithms/ruby/2251-number-of-flowers-in-full-bloom.rb) | Hard |
480+
| 2257 | Count Unguarded Cells in the Grid | [Ruby](./algorithms/ruby/2257-count-unguarded-cells-in-the-grid.rb) | Medium |
475481
| 2264 | Largest 3-Same-Digit Number in String | [Ruby](./algorithms/ruby/2264-largest-3-same-digit-number-in-string.rb) | Easy |
476482
| 2265 | Count Nodes Equal to Average of Subtree | [Ruby](./algorithms/ruby/2265-count-nodes-equal-to-average-of-subtree.rb) | Medium |
477483
| 2272 | Substring With Largest Variance | [Ruby](./algorithms/ruby/2272-substring-with-largest-variance.rb) | Hard |
@@ -497,6 +503,7 @@
497503
| 2444 | Count Subarrays With Fixed Bounds | [Ruby](./algorithms/ruby/2444-count-subarrays-with-fixed-bounds.rb) | Hard |
498504
| 2448 | Minimum Cost to Make Array Equal | [Ruby](./algorithms/ruby/2448-minimum-cost-to-make-array-equal.rb) | Hard |
499505
| 2458 | Height of Binary Tree After Subtree Removal Queries | [Ruby](./algorithms/ruby/2458-height-of-binary-tree-after-subtree-removal-queries.rb) | Hard |
506+
| 2461 | Maximum Sum of Distinct Subarrays With Length K | [Ruby](./algorithms/ruby/2461-maximum-sum-of-distinct-subarrays-with-length-k.rb) | Medium |
500507
| 2462 | Total Cost to Hire K Workers | [Python3](./algorithms/python3/2462-total-cost-to-hire-k-workers.py) | Medium |
501508
| 2463 | Minimum Total Distance Traveled | [Ruby](./algorithms/ruby/2463-minimum-total-distance-traveled.rb) | Hard |
502509
| 2466 | Count Ways To Build Good Strings | [Ruby](./algorithms/ruby/2466-count-ways-to-build-good-strings.rb) | Medium |
@@ -505,6 +512,7 @@
505512
| 2490 | Circular Sentence | [Ruby](./algorithms/ruby/2490-circular-sentence.rb) | Easy |
506513
| 2492 | Minimum Score of a Path Between Two Cities | [Ruby](./algorithms/ruby/2492-minimum-score-of-a-path-between-two-cities.rb) | Medium |
507514
| 2501 | Longest Square Streak in an Array | [Ruby](./algorithms/ruby/2501-longest-square-streak-in-an-array.rb) | Medium |
515+
| 2516 | Take K of Each Character From Left and Right | [Ruby](./algorithms/ruby/2516-take-k-of-each-character-from-left-and-right.rb) | Medium |
508516
| 2542 | Maximum Subsequence Score | [Ruby](./algorithms/ruby/2542-maximum-subsequence-score.rb) | Medium |
509517
| 2551 | Put Marbles in Bags | [Ruby](./algorithms/ruby/2551-put-marbles-in-bags.rb) | Hard |
510518
| 2563 | Count the Number of Fair Pairs | [Ruby](./algorithms/ruby/2563-count-the-number-of-fair-pairs.rb) | Medium |
@@ -523,10 +531,12 @@
523531
| 2785 | Sort Vowels in a String | [Ruby](./algorithms/ruby/2785-sort-vowels-in-a-string.rb) | Medium |
524532
| 2842 | Difference Between Ones and Zeros in Row and Column | [Ruby](./algorithms/ruby/2842-difference-between-ones-and-zeros-in-row-and-column.rb) | Medium |
525533
| 2849 | Determine if a Cell Is Reachable at a Given Time | [Ruby](./algorithms/ruby/2849-determine-if-a-cell-is-reachable-at-a-given-time) | Medium |
534+
| 2924 | Find Champion II | [Ruby](./algorithms/ruby/2924-find-champion-ii.rb) | Medium |
526535
| 2966 | Divide Array Into Arrays With Max Difference | [Ruby](./algorithms/ruby/2966-divide-array-into-arrays-with-max-difference) | Medium |
527536
| 3011 | Find if Array Can Be Sorted | [Ruby](./algorithms/ruby/3011-find-if-array-can-be-sorted.rb) | Medium |
528537
| 3097 | Shortest Subarray With OR At Least K II | [Ruby](./algorithms/ruby/3097-shortest-subarray-with-or-at-least-k-ii.rb) | Medium |
529538
| 3163 | String Compression III | [Ruby](./algorithms/ruby/3163-string-compression-iii.rb) | Medium |
539+
| 3243 | Shortest Distance After Road Addition Queries I | [Ruby](./algorithms/ruby/3243-shortest-distance-after-road-addition-queries-i.rb) | Medium |
530540
| 3254 | Find the Power of K-Size Subarrays I | [Ruby](./algorithms/ruby/3254-find-the-power-of-k-size-subarrays-i.rb) | Medium |
531541

532542
### Database

Diff for: ‎algorithms/ruby/0773-sliding-puzzle.rb

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# frozen_string_literal: true
2+
3+
# 773. Sliding Puzzle
4+
# https://leetcode.com/problems/sliding-puzzle
5+
# Hard
6+
7+
=begin
8+
On an 2 x 3 board, there are five tiles labeled from 1 to 5, and an empty square represented by 0. A move consists of choosing 0 and a 4-directionally adjacent number and swapping it.
9+
The state of the board is solved if and only if the board is [[1,2,3],[4,5,0]].
10+
Given the puzzle board board, return the least number of moves required so that the state of the board is solved. If it is impossible for the state of the board to be solved, return -1.
11+
12+
Example 1:
13+
Input: board = [[1,2,3],[4,0,5]]
14+
Output: 1
15+
Explanation: Swap the 0 and the 5 in one move.
16+
17+
Example 2:
18+
Input: board = [[1,2,3],[5,4,0]]
19+
Output: -1
20+
Explanation: No number of moves will make the board solved.
21+
22+
Example 3:
23+
Input: board = [[4,1,2],[5,0,3]]
24+
Output: 5
25+
Explanation: 5 is the smallest number of moves that solves the board.
26+
An example path:
27+
After move 0: [[4,1,2],[5,0,3]]
28+
After move 1: [[4,1,2],[0,5,3]]
29+
After move 2: [[0,1,2],[4,5,3]]
30+
After move 3: [[1,0,2],[4,5,3]]
31+
After move 4: [[1,2,0],[4,5,3]]
32+
After move 5: [[1,2,3],[4,5,0]]
33+
34+
Constraints:
35+
* board.length == 2
36+
* board[i].length == 3
37+
* 0 <= board[i][j] <= 5
38+
* Each value board[i][j] is unique.
39+
=end
40+
41+
42+
# @param {Integer[][]} board
43+
# @return {Integer}
44+
def sliding_puzzle(board)
45+
target = [[1, 2, 3], [4, 5, 0]].flatten.join
46+
start = board.flatten.join
47+
can_move = {
48+
0 => [1, 3],
49+
1 => [0, 2, 4],
50+
2 => [1, 5],
51+
3 => [0, 4],
52+
4 => [1, 3, 5],
53+
5 => [2, 4]
54+
}
55+
visited = Set.new
56+
steps = 0
57+
queue = [[start, steps]]
58+
59+
until queue.empty?
60+
current, steps = queue.shift
61+
return steps if current == target
62+
63+
zero_index = current.index("0")
64+
can_move[zero_index].each do |move|
65+
next_board = current.dup
66+
next_board[zero_index], next_board[move] = next_board[move], next_board[zero_index]
67+
next if visited.include?(next_board)
68+
69+
visited << next_board
70+
queue.push([next_board, steps + 1])
71+
end
72+
end
73+
74+
-1
75+
end
76+
77+
# **************** #
78+
# TEST #
79+
# **************** #
80+
81+
require "test/unit"
82+
class Test_sliding_puzzle < Test::Unit::TestCase
83+
def test_
84+
assert_equal 1, sliding_puzzle([[1, 2, 3], [4, 0, 5]])
85+
assert_equal(-1, sliding_puzzle([[1, 2, 3], [5, 4, 0]]))
86+
assert_equal 5, sliding_puzzle([[4, 1, 2], [5, 0, 3]])
87+
end
88+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# frozen_string_literal: true
2+
3+
# 1072. Flip Columns For Maximum Number of Equal Rows
4+
# https://leetcode.com/problems/flip-columns-for-maximum-number-of-equal-rows
5+
# Medium
6+
7+
=begin
8+
You are given an m x n binary matrix matrix.
9+
You can choose any number of columns in the matrix and flip every cell in that column (i.e., Change the value of the cell from 0 to 1 or vice versa).
10+
Return the maximum number of rows that have all values equal after some number of flips.
11+
12+
Example 1:
13+
Input: matrix = [[0,1],[1,1]]
14+
Output: 1
15+
Explanation: After flipping no values, 1 row has all values equal.
16+
17+
Example 2:
18+
Input: matrix = [[0,1],[1,0]]
19+
Output: 2
20+
Explanation: After flipping values in the first column, both rows have equal values.
21+
22+
Example 3:
23+
Input: matrix = [[0,0,0],[0,0,1],[1,1,0]]
24+
Output: 2
25+
Explanation: After flipping values in the first two columns, the last two rows have equal values.
26+
27+
28+
Constraints:
29+
* m == matrix.length
30+
* n == matrix[i].length
31+
* 1 <= m, n <= 300
32+
matrix[i][j] is either 0 or 1.
33+
=end
34+
35+
# @param {Integer[][]} matrix
36+
# @return {Integer}
37+
def max_equal_rows_after_flips(mat)
38+
pat_freq = Hash.new(0)
39+
40+
mat.each do |row|
41+
pattern = row[0] == 0 ? row.join : row.map { |bit| bit ^ 1 }.join
42+
pat_freq[pattern] += 1
43+
end
44+
45+
pat_freq.values.max
46+
end
47+
48+
# **************** #
49+
# TEST #
50+
# **************** #
51+
52+
require "test/unit"
53+
class Test_max_equal_rows_after_flips < Test::Unit::TestCase
54+
def test_
55+
assert_equal(1, max_equal_rows_after_flips([[0, 1], [1, 1]]))
56+
assert_equal(2, max_equal_rows_after_flips([[0, 1], [1, 0]]))
57+
assert_equal(2, max_equal_rows_after_flips([[0, 0, 0], [0, 0, 1], [1, 1, 0]]))
58+
end
59+
end

Diff for: ‎algorithms/ruby/1652-defuse-the-bomb.rb

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# frozen_string_literal: true
2+
3+
# 1652. Defuse the Bomb
4+
# https://leetcode.com/problems/defuse-the-bomb
5+
# Easy
6+
7+
=begin
8+
You have a bomb to defuse, and your time is running out! Your informer will provide you with a circular array code of length of n and a key k.
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+
As code is circular, the next element of code[n-1] is code[0], and the previous element of code[0] is code[n-1].
14+
Given the circular array code and an integer key k, return the decrypted code to defuse the bomb!
15+
16+
Example 1:
17+
Input: code = [5,7,1,4], k = 3
18+
Output: [12,10,16,13]
19+
Explanation: Each number is replaced by the sum of the next 3 numbers. The decrypted code is [7+1+4, 1+4+5, 4+5+7, 5+7+1]. Notice that the numbers wrap around.
20+
Example 2:
21+
Input: code = [1,2,3,4], k = 0
22+
Output: [0,0,0,0]
23+
Explanation: When k is zero, the numbers are replaced by 0.
24+
Example 3:
25+
Input: code = [2,4,9,3], k = -2
26+
Output: [12,5,6,13]
27+
Explanation: The decrypted code is [3+9, 2+3, 4+2, 9+4]. Notice that the numbers wrap around again. If k is negative, the sum is of the previous numbers.
28+
29+
Constraints:
30+
* n == code.length
31+
* 1 <= n <= 100
32+
* 1 <= code[i] <= 100
33+
* -(n - 1) <= k <= n - 1
34+
=end
35+
36+
# @param {Integer[]} code
37+
# @param {Integer} k
38+
# @return {Integer[]}
39+
def decrypt(circ, k)
40+
n = circ.size
41+
result = Array.new(n, 0)
42+
43+
return result if k == 0
44+
45+
w_sum = 0
46+
start = k > 0 ? 1 : n + k
47+
end_pos = k > 0 ? k : n - 1
48+
49+
(start..end_pos).each do |i|
50+
w_sum += circ[i % n]
51+
end
52+
53+
n.times do |i|
54+
result[i] = w_sum
55+
w_sum -= circ[(start + i) % n]
56+
w_sum += circ[(end_pos + i + 1) % n]
57+
end
58+
59+
result
60+
end
61+
62+
# **************** #
63+
# TEST #
64+
# **************** #
65+
66+
require "test/unit"
67+
68+
class Test_decrypt < Test::Unit::TestCase
69+
def test_
70+
assert_equal [12, 10, 16, 13], decrypt([5, 7, 1, 4], 3)
71+
assert_equal [0, 0, 0, 0], decrypt([1, 2, 3, 4], 0)
72+
assert_equal [12, 5, 6, 13], decrypt([2, 4, 9, 3], -2)
73+
end
74+
end

0 commit comments

Comments
 (0)
Please sign in to comment.