-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path566.1_reshape_matrix.rb
59 lines (45 loc) · 1.32 KB
/
566.1_reshape_matrix.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# frozen_string_literal: true
# https://leetcode.com/problems/reshape-the-matrix/
require_relative '../test_helper'
# Runtime: 125 ms, faster than 49.12% of Ruby online submissions for Reshape the Matrix.
# Memory Usage: 211.6 MB, less than 21.05% of Ruby online submissions for Reshape the Matrix.
class Solution
class << self
def matrix_reshape(mat, r, c)
return mat unless validate(mat, r, c)
flatttened_mat = flattened_matrix(mat)
return [flatttened_mat] if r == 1
result = []
matrix_index = 0
r.times do |i|
result.append([])
c.times do |j|
result[i].append(flatttened_mat[matrix_index])
matrix_index += 1
end
end
result
end
def validate(mat, r, c)
(mat.size * mat[0].size) == (r * c)
end
def flattened_matrix(mat)
flatttened_mat = []
mat.each do |row|
row.each { |num| flatttened_mat.append(num)}
end
flatttened_mat
end
end
end
class TestSolution < Minitest::Test
def test_1
assert_equal [[1, 2, 3, 4]], Solution.matrix_reshape([[1, 2], [3, 4]], 1, 4)
end
def test_2
assert_equal [[1, 2], [3, 4]], Solution.matrix_reshape([[1, 2], [3, 4]], 2, 4)
end
def test_3
assert_equal [[1, 2], [3, 4]], Solution.matrix_reshape([[1, 2, 3, 4]], 2, 2)
end
end