Skip to content

Commit c53c120

Browse files
committed
2025-02-07 v. 8.4.6: added "1329. Sort the Matrix Diagonally"
1 parent 8d953c8 commit c53c120

File tree

4 files changed

+112
-1
lines changed

4 files changed

+112
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
678678
| 1310. XOR Queries of a Subarray | [Link](https://leetcode.com/problems/xor-queries-of-a-subarray/) | [Link](./lib/medium/1310_xor_queries_of_a_subarray.rb) | [Link](./test/medium/test_1310_xor_queries_of_a_subarray.rb) |
679679
| 1315. Sum of Nodes with Even-Valued Grandparent | [Link](https://leetcode.com/problems/sum-of-nodes-with-even-valued-grandparent/) | [Link](./lib/medium/1315_sum_of_nodes_with_even_valued_grandparent.rb) | [Link](./test/medium/test_1315_sum_of_nodes_with_even_valued_grandparent.rb) |
680680
| 1325. Delete Leaves With a Given Value | [Link](https://leetcode.com/problems/delete-leaves-with-a-given-value/) | [Link](./lib/medium/1325_delete_leaves_with_a_given_value.rb) | [Link](./test/medium/test_1325_delete_leaves_with_a_given_value.rb) |
681+
| 1329. Sort the Matrix Diagonally | [Link](https://leetcode.com/problems/sort-the-matrix-diagonally/) | [Link](./lib/medium/1329_sort_the_matrix_diagonally.rb) | [Link](./test/medium/test_1329_sort_the_matrix_diagonally.rb) |
681682
| 1400. Construct K Palindrome Strings | [Link](https://leetcode.com/problems/construct-k-palindrome-strings/) | [Link](./lib/medium/1400_construct_k_palindrome_strings.rb) | [Link](./test/medium/test_1400_construct_k_palindrome_strings.rb) |
682683
| 2116. Check if a Parentheses String Can Be Valid | [Link](https://leetcode.com/problems/check-if-a-parentheses-string-can-be-valid/) | [Link](./lib/medium/2116_check_if_a_parentheses_string_can_be_valid.rb) | [Link](./test/medium/test_2116_check_if_a_parentheses_string_can_be_valid.rb) |
683684
| 2425. Bitwise XOR of All Pairings | [Link](https://leetcode.com/problems/bitwise-xor-of-all-pairings/) | [Link](./lib/medium/2425_bitwise_xor_of_all_pairings.rb) | [Link](./test/medium/test_2425_bitwise_xor_of_all_pairings.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 = '8.4.5'
8+
s.version = '8.4.6'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/sort-the-matrix-diagonally/
4+
# @param {Integer[][]} mat
5+
# @return {Integer[][]}
6+
def diagonal_sort(mat)
7+
rows = mat.size
8+
columns = mat[0].size
9+
10+
(0...rows).each do |row|
11+
i = row
12+
j = 0
13+
diagonals = []
14+
15+
while i < rows && j < columns
16+
diagonals << mat[i][j]
17+
18+
i += 1
19+
j += 1
20+
end
21+
22+
diagonals.sort!
23+
24+
i = row
25+
j = 0
26+
k = 0
27+
28+
while i < rows && j < columns
29+
mat[i][j] = diagonals[k]
30+
31+
i += 1
32+
j += 1
33+
k += 1
34+
end
35+
end
36+
37+
(0...columns).each do |column|
38+
i = 0
39+
j = column
40+
diagonals = []
41+
42+
while i < rows && j < columns
43+
diagonals << mat[i][j]
44+
45+
i += 1
46+
j += 1
47+
end
48+
49+
diagonals.sort!
50+
51+
i = 0
52+
j = column
53+
k = 0
54+
55+
while i < rows && j < columns
56+
mat[i][j] = diagonals[k]
57+
58+
i += 1
59+
j += 1
60+
k += 1
61+
end
62+
end
63+
64+
mat
65+
end
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/1329_sort_the_matrix_diagonally'
5+
require 'minitest/autorun'
6+
7+
class SortTheMatrixDiagonallyTest < ::Minitest::Test
8+
def test_diagonal_sort_small_matrix
9+
assert_equal(
10+
[
11+
[1, 1, 1, 1],
12+
[1, 2, 2, 2],
13+
[1, 2, 3, 3]
14+
],
15+
diagonal_sort(
16+
[
17+
[3, 3, 1, 1],
18+
[2, 2, 1, 2],
19+
[1, 1, 1, 2]
20+
]
21+
)
22+
)
23+
end
24+
25+
def test_diagonal_sort_large_matrix
26+
assert_equal(
27+
[
28+
[5, 17, 4, 1, 52, 7],
29+
[11, 11, 25, 45, 8, 69],
30+
[14, 23, 25, 44, 58, 15],
31+
[22, 27, 31, 36, 50, 66],
32+
[84, 28, 75, 33, 55, 68]
33+
],
34+
diagonal_sort(
35+
[
36+
[11, 25, 66, 1, 69, 7],
37+
[23, 55, 17, 45, 15, 52],
38+
[75, 31, 36, 44, 58, 8],
39+
[22, 27, 33, 25, 68, 4],
40+
[84, 28, 14, 11, 5, 50]
41+
]
42+
)
43+
)
44+
end
45+
end

0 commit comments

Comments
 (0)