Skip to content

Commit ee7e0cb

Browse files
committedMar 27, 2023
Feat: add Ruby solutions
1 parent e79cb8d commit ee7e0cb

7 files changed

+349
-0
lines changed
 

‎README.md

+6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
| 35 | Search Insert Position | [Ruby](./algorithms/ruby/0035-search-insert-position.rb) | Easy |
2727
| 45 | Jump Game II | [Ruby](./algorithms/ruby/0045-jump-game-ii.rb) | Medium |
2828
| 58 | Length of Last Word | [Ruby](./algorithms/ruby/0058-length-of-last-word.rb) | Easy |
29+
| 64 | Minimum Path Sum | [Ruby](./algorithms/ruby/0064-minimum-path-sum.rb) | Medium |
2930
| 66 | Plus One | [Ruby](./algorithms/ruby/0066-plus-one.rb) | Easy |
3031
| 67 | Add Binary | [Ruby](./algorithms/ruby/0067-add-binary.rb) [Python3](./algorithms/python3/0067-add-binary.py) | Easy |
3132
| 69 | Sqrt(x) | [Ruby](./algorithms/ruby/0069-sqrtx.rb) | Easy |
@@ -42,13 +43,15 @@
4243
| 129 | Sum Root to Leaf Numbers | [Ruby](./algorithms/ruby/0129-sum-root-to-leaf-numbers.rb) | Medium |
4344
| 142 | Linked List Cycle II | [Ruby](./algorithms/ruby/0142-linked-list-cycle-ii.rb) | Medium |
4445
| 205 | Isomorphic Strings | [Ruby](./algorithms/ruby/0205-isomorphic-strings.rb) | Easy |
46+
| 206 | Reverse Linked List | [Ruby](./algorithms/ruby/0206-reverse-linked-list.rb) | Easy |
4547
| 208 | Implement Trie (Prefix Tree) | [Ruby](./algorithms/ruby/0208-implement-trie-prefix-tree.rb) | Medium |
4648
| 211 | Design Add and Search Words Data Structure | [Ruby](./algorithms/ruby/0211-design-add-and-search-words-data-structure.rb) | Medium |
4749
| 226 | Invert Binary Tree | [Ruby](./algorithms/ruby/0226-invert-binary-tree.rb) | Easy |
4850
| 344 | Reverse String | [Ruby](./algorithms/ruby/0344-reverse-string.rb) | Easy |
4951
| 382 | Linked List Random Node | [Ruby](./algorithms/ruby/0382-linked-list-random-node.rb) | Medium |
5052
| 387 | First Unique Character in a String | [Ruby](./algorithms/ruby/0387-first-unique-character-in-a-string.rb) | Easy |
5153
| 392 | Is Subsequence | [Ruby](./algorithms/ruby/0392-is-subsequence.rb) | Easy |
54+
| 409 | Longest Palindrome | [Ruby](./algorithms/ruby/0409-longest-palindrome.rb) | Easy |
5255
| 427 | Construct Quad Tree | [Ruby](./algorithms/ruby/0427-construct-quad-tree.rb) | Medium |
5356
| 438 | Find All Anagrams in a String | [Ruby](./algorithms/ruby/0438-find-all-anagrams-in-a-string.rb) | Medium |
5457
| 443 | String Compression | [Ruby](./algorithms/ruby/0443-string-compression.rb) [Python3](./algorithms/python3/0443-string-compression.py) | Medium |
@@ -64,6 +67,7 @@
6467
| 733 | Flood Fill | [Ruby](./algorithms/ruby/0733-flood-fill.rb) | Easy |
6568
| 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 |
6669
| 875 | Koko Eating Bananas | [Ruby](./algorithms/ruby/0875-koko-eating-bananas.rb) [Python3](./algorithms/python3/0875-koko-eating-bananas.py) | Medium |
70+
| 876 | Middle of the Linked List | [Ruby](./algorithms/ruby/0876-middle-of-the-linked-list.rb) | Easy |
6771
| 904 | Fruit Into Baskets | [Ruby](./algorithms/ruby/0904-fruit-into-baskets.rb) | Medium |
6872
| 912 | Sort an Array | [Ruby](./algorithms/ruby/0912-sort-an-array.rb) | Medium |
6973
| 953 | Verifying an Alien Dictionary | [Ruby](./algorithms/ruby/0953-verifying-an-alien-dictionary.rb) | Easy |
@@ -86,7 +90,9 @@
8690
| 1675 | Minimize Deviation in Array | [Ruby](./algorithms/ruby/1675-minimize-deviation-in-array.rb) | Hard |
8791
| 1680 | Concatenation of Consecutive Binary Numbers | [Ruby](./algorithms/ruby/1680-concatenation-of-consecutive-binary-numbers.rb) | Medium |
8892
| 2187 | Minimum Time to Complete Trips | [Ruby](./algorithms/ruby/2187-minimum-time-to-complete-trips.rb) [Python3](./algorithms/python3/2187-minimum-time-to-complete-trips.py) | Medium |
93+
| 2316 | Count Unreachable Pairs of Nodes in an Undirected Graph | [Ruby](./algorithms/ruby/2316-count-unreachable-pairs-of-nodes-in-an-undirected-graph.rb) | Medium |
8994
| 2348 | Number of Zero-Filled Subarrays | [Ruby](./algorithms/ruby/2348-number-of-zero-filled-subarrays.rb) | Medium |
95+
| 2360 | Longest Cycle in a Graph | [Ruby](./algorithms/ruby/2360-longest-cycle-in-a-graph.rb) | Hard |
9096
| 2409 | Count Days Spent Together | [Ruby](./algorithms/ruby/2409-count-days-spent-together.rb) | Easy |
9197
| 2413 | Smallest Even Multiple | [Ruby](./algorithms/ruby/2413-smallest-even-multiple.rb) | Easy |
9298
| 2444 | Count Subarrays With Fixed Bounds | [Ruby](./algorithms/ruby/2444-count-subarrays-with-fixed-bounds.rb) | Hard |
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# frozen_string_literal: true
2+
3+
# 64. Minimum Path Sum
4+
# https://leetcode.com/problems/minimum-path-sum
5+
6+
=begin
7+
8+
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right, which minimizes the sum of all numbers along its path.
9+
10+
Note: You can only move either down or right at any point in time.
11+
12+
### Example 1:
13+
Input: grid = [[1,3,1],[1,5,1],[4,2,1]]
14+
Output: 7
15+
Explanation: Because the path 1 → 3 → 1 → 1 → 1 minimizes the sum.
16+
17+
### Example 2:
18+
Input: grid = [[1,2,3],[4,5,6]]
19+
Output: 12
20+
21+
### Constraints:
22+
* m == grid.length
23+
* n == grid[i].length
24+
* 1 <= m, n <= 200
25+
* 0 <= grid[i][j] <= 100
26+
27+
=end
28+
29+
# Runtime 72 ms
30+
# Memory 212.3 MB
31+
# @param {Integer[][]} grid
32+
# @return {Integer}
33+
def min_path_sum(grid)
34+
acc = [0] + [Float::INFINITY] * (grid[0].size - 1)
35+
36+
grid.each { |row|
37+
left = Float::INFINITY
38+
acc.map!.with_index { |n, i| left = row[i] + (left < n ? left : n) }
39+
}
40+
41+
acc.last
42+
end
43+
44+
# ********************#
45+
# TEST #
46+
# ********************#
47+
48+
require "test/unit"
49+
class Test_min_path_sum < Test::Unit::TestCase
50+
def test_
51+
assert_equal 7, min_path_sum([[1, 3, 1], [1, 5, 1], [4, 2, 1]])
52+
# assert_equal 12, min_path_sum([[1,2,3],[4,5,6]])
53+
end
54+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# frozen_string_literal: true
2+
3+
# 206. Reverse Linked List
4+
# https://leetcode.com/problems/reverse-linked-list
5+
6+
=begin
7+
8+
Given the head of a singly linked list, reverse the list, and return the reversed list.
9+
10+
### Example 1:
11+
Input: head = [1,2,3,4,5]
12+
Output: [5,4,3,2,1]
13+
14+
### Example 2:
15+
Input: head = [1,2]
16+
Output: [2,1]
17+
18+
### Example 3:
19+
Input: head = []
20+
Output: []
21+
22+
### Constraints:
23+
* The number of nodes in the list is the range [0, 5000].
24+
* -5000 <= Node.val <= 5000
25+
26+
=end
27+
28+
# Definition for singly-linked list.
29+
# class ListNode
30+
# attr_accessor :val, :next
31+
# def initialize(val = 0, _next = nil)
32+
# @val = val
33+
# @next = _next
34+
# end
35+
# end
36+
# @param {ListNode} head
37+
# @return {ListNode}
38+
def reverse_list(head, prev = nil)
39+
while head
40+
head, head.next, prev = head.next, prev, head
41+
end
42+
prev
43+
end
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# frozen_string_literal: true
2+
3+
# 409. Longest Palindrome
4+
# https://leetcode.com/problems/longest-palindrome
5+
6+
=begin
7+
8+
Given a string s which consists of lowercase or uppercase letters, return the length of the longest palindrome that can be built with those letters.
9+
10+
Letters are case sensitive, for example, "Aa" is not considered a palindrome here.
11+
12+
### Example 1:
13+
Input: s = "abccccdd"
14+
Output: 7
15+
Explanation: One longest palindrome that can be built is "dccaccd", whose length is 7.
16+
17+
### Example 2:
18+
Input: s = "a"
19+
Output: 1
20+
Explanation: The longest palindrome that can be built is "a", whose length is 1.
21+
22+
### Constraints:
23+
* 1 <= s.length <= 2000
24+
* s consists of lowercase and/or uppercase English letters only.
25+
26+
=end
27+
28+
# Runtime 92 ms
29+
# Memory 210.9 MB
30+
# @param {String} s
31+
# @return {Integer}
32+
def longest_palindrome(s)
33+
[s.size, s.each_char.tally.sum { _2 & ~1 } + 1].min
34+
end
35+
36+
# **************** #
37+
# TEST #
38+
# **************** #
39+
40+
require "test/unit"
41+
class Test_longest_palindrome < Test::Unit::TestCase
42+
def test_
43+
assert_equal 7, longest_palindrome("abccccdd")
44+
assert_equal 1, longest_palindrome("a")
45+
end
46+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# frozen_string_literal: true
2+
3+
# 876. Middle of the Linked List
4+
# https://leetcode.com/problems/middle-of-the-linked-list
5+
6+
=begin
7+
8+
Given the head of a singly linked list, return the middle node of the linked list.
9+
10+
If there are two middle nodes, return the second middle node.
11+
12+
### Example 1:
13+
Input: head = [1,2,3,4,5]
14+
Output: [3,4,5]
15+
Explanation: The middle node of the list is node 3.
16+
17+
### Example 2:
18+
Input: head = [1,2,3,4,5,6]
19+
Output: [4,5,6]
20+
Explanation: Since the list has two middle nodes with values 3 and 4, we return the second one.
21+
22+
### Constraints:
23+
* The number of nodes in the list is in the range [1, 100].
24+
* 1 <= Node.val <= 100
25+
26+
=end
27+
28+
# Definition for singly-linked list.
29+
# class ListNode
30+
# attr_accessor :val, :next
31+
# def initialize(val = 0, _next = nil)
32+
# @val = val
33+
# @next = _next
34+
# end
35+
# end
36+
# @param {ListNode} head
37+
# @return {ListNode}
38+
def middle_node(head)
39+
slow = fast = head
40+
while fast != nil && fast.next != nil
41+
slow = slow.next
42+
fast = fast.next.next
43+
end
44+
slow
45+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# frozen_string_literal: true
2+
3+
# 2316. Count Unreachable Pairs of Nodes in an Undirected Graph
4+
# https://leetcode.com/problems/count-unreachable-pairs-of-nodes-in-an-undirected-graph
5+
6+
=begin
7+
8+
You are given an integer n. There is an undirected graph with n nodes, numbered from 0 to n - 1. You are given a 2D integer array edges where edges[i] = [ai, bi] denotes that there exists an undirected edge connecting nodes ai and bi.
9+
10+
Return the number of pairs of different nodes that are unreachable from each other.
11+
12+
### Example 1:
13+
Input: n = 3, edges = [[0,1],[0,2],[1,2]]
14+
Output: 0
15+
Explanation: There are no pairs of nodes that are unreachable from each other. Therefore, we return 0.
16+
17+
### Example 2:
18+
Input: n = 7, edges = [[0,2],[0,5],[2,4],[1,6],[5,4]]
19+
Output: 14
20+
Explanation: There are 14 pairs of nodes that are unreachable from each other:
21+
[[0,1],[0,3],[0,6],[1,2],[1,3],[1,4],[1,5],[2,3],[2,6],[3,4],[3,5],[3,6],[4,6],[5,6]].
22+
Therefore, we return 14.
23+
24+
### Constraints:
25+
* 1 <= n <= 105
26+
* 0 <= edges.length <= 2 * 105
27+
* edges[i].length == 2
28+
* 0 <= ai, bi < n
29+
* ai != bi
30+
* There are no repeated edges.
31+
32+
=end
33+
34+
# Runtime: 403 ms
35+
# Memory Usage: 247.1 MB
36+
# @param {Integer} n
37+
# @param {Integer[][]} edges
38+
# @return {Integer}
39+
def count_pairs(n, edges)
40+
@adj = Array.new(n + 1).map { [] }
41+
@v = Array.new(n + 1, false)
42+
@total = 0
43+
@ans = 0
44+
edges.each do |a, b|
45+
@adj[a].push(b)
46+
@adj[b].push(a)
47+
end
48+
(0...n).each do |i|
49+
next if @v[i]
50+
bfs(i)
51+
end
52+
@ans
53+
end
54+
55+
def bfs(i)
56+
return if @v[i]
57+
count = 0
58+
q = [i]
59+
@v[i] = true
60+
while !q.empty?
61+
x = q.shift
62+
count += 1
63+
@ans += @total
64+
@adj[x].each do |j|
65+
next if @v[j]
66+
@v[j] = true
67+
q.push(j)
68+
end
69+
end
70+
@total += count
71+
end
72+
73+
# **************** #
74+
# TEST #
75+
# **************** #
76+
77+
require "test/unit"
78+
class Test_count_pairs < Test::Unit::TestCase
79+
def test_
80+
assert_equal 0, count_pairs(3, [[0, 1], [0, 2], [1, 2]])
81+
assert_equal 14, count_pairs(7, [[0, 2], [0, 5], [2, 4], [1, 6], [5, 4]])
82+
end
83+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# frozen_string_literal: true
2+
3+
# 2360. Longest Cycle in a Graph
4+
# https://leetcode.com/problems/longest-cycle-in-a-graph
5+
6+
=begin
7+
8+
You are given a directed graph of n nodes numbered from 0 to n - 1, where each node has at most one outgoing edge.
9+
10+
The graph is represented with a given 0-indexed array edges of size n, indicating that there is a directed edge from node i to node edges[i]. If there is no outgoing edge from node i, then edges[i] == -1.
11+
12+
Return the length of the longest cycle in the graph. If no cycle exists, return -1.
13+
14+
A cycle is a path that starts and ends at the same node.
15+
16+
### Example 1:
17+
Input: edges = [3,3,4,2,3]
18+
Output: 3
19+
Explanation: The longest cycle in the graph is the cycle: 2 -> 4 -> 3 -> 2.
20+
The length of this cycle is 3, so 3 is returned.
21+
22+
### Example 2:
23+
Input: edges = [2,-1,3,1]
24+
Output: -1
25+
Explanation: There are no cycles in this graph.
26+
27+
### Constraints:
28+
* n == edges.length
29+
* 2 <= n <= 105
30+
* -1 <= edges[i] < n
31+
* edges[i] != i
32+
33+
=end
34+
35+
# Runtime 379 ms
36+
# Memory 228.1 MB
37+
# @param {Integer[]} edges
38+
# @return {Integer}
39+
def longest_cycle(edges)
40+
max = -1
41+
42+
edges.size.times do |cur|
43+
next if edges[cur] < 0
44+
dists = {}
45+
dist = 0
46+
until cur < 0
47+
if dists[cur]
48+
cycle = dist - dists[cur]
49+
max = cycle if cycle > max
50+
break
51+
else
52+
dists[cur] = dist
53+
cur, edges[cur] = edges[cur], -1
54+
dist += 1
55+
end
56+
end
57+
end
58+
59+
max
60+
end
61+
62+
# **************** #
63+
# TEST #
64+
# **************** #
65+
66+
require "test/unit"
67+
class Test_longest_cycle < Test::Unit::TestCase
68+
def test_
69+
assert_equal 3, longest_cycle([3, 3, 4, 2, 3])
70+
assert_equal(-1, longest_cycle([2, -1, 3, 1]))
71+
end
72+
end

0 commit comments

Comments
 (0)
Please sign in to comment.