Skip to content

Commit 65b6ec4

Browse files
authored
Merge pull request #608 from fartem/2319_Check_if_Matrix_Is_X-Matrix
2024-04-22 v. 5.5.1: added "2319. Check if Matrix Is X-Matrix"
2 parents 5f26d43 + dad2032 commit 65b6ec4

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,3 +411,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
411411
| 2303. Calculate Amount Paid in Taxes | [Link](https://leetcode.com/problems/calculate-amount-paid-in-taxes/) | [Link](./lib/easy/2303_calculate_amount_paid_in_taxes.rb) |
412412
| 2309. Greatest English Letter in Upper and Lower Case | [Link](https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case/) | [Link](./lib/easy/2309_greatest_english_letter_in_upper_and_lower_case.rb) |
413413
| 2315. Count Asterisks | [Link](https://leetcode.com/problems/count-asterisks/) | [Link](./lib/easy/2315_count_asterisks.rb) |
414+
| 2319. Check if Matrix Is X-Matrix | [Link](https://leetcode.com/problems/check-if-matrix-is-x-matrix/) | [Link](./lib/easy/2319_check_if_matrix_is_x_matrix.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 = '5.5.0'
8+
s.version = '5.5.1'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[bin/leetcode-ruby README.md LICENSE]
1111
s.executable = 'leetcode-ruby'
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/check-if-matrix-is-x-matrix/
4+
# @param {Integer[][]} grid
5+
# @return {Boolean}
6+
def check_x_matrix(grid)
7+
n = grid.length
8+
(0...n).each do |i|
9+
return false if grid[i][i].zero? || grid[i][n - i - 1].zero?
10+
end
11+
12+
(0...n).each do |i|
13+
(0...n).each do |j|
14+
next unless i != j && j != n - i - 1
15+
16+
return false if grid[i][j].positive?
17+
end
18+
end
19+
20+
true
21+
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/easy/2319_check_if_matrix_is_x_matrix'
5+
require 'minitest/autorun'
6+
7+
class CheckIfMatrixIsXMatrixTest < ::Minitest::Test
8+
def test_default
9+
assert(check_x_matrix([[2, 0, 0, 1], [0, 3, 1, 0], [0, 5, 2, 0], [4, 0, 0, 2]]))
10+
assert(!check_x_matrix([[5, 7, 0], [0, 3, 1], [0, 5, 0]]))
11+
end
12+
end

0 commit comments

Comments
 (0)