An n x n
matrix is valid if every row and every column contains all the integers from 1
to n
(inclusive).
Given an n x n
integer matrix matrix
, return true
if the matrix is valid. Otherwise, return false
.
Example 1:
Input: matrix = [[1,2,3],[3,1,2],[2,3,1]] Output: true Explanation: In this case, n = 3, and every row and column contains the numbers 1, 2, and 3. Hence, we return true.
Example 2:
Input: matrix = [[1,1,1],[1,2,3],[1,2,3]] Output: false Explanation: In this case, n = 3, but the first row and the first column do not contain the numbers 2 or 3. Hence, we return false.
Constraints:
n == matrix.length == matrix[i].length
1 <= n <= 100
1 <= matrix[i][j] <= n
program main
implicit none
integer, parameter :: n = 3
integer, dimension(n, n) :: matrix
logical :: result
! Example 1
matrix(:, :) = reshape([1, 2, 3, 3, 1, 2, 2, 3, 1], shape(matrix))
write(*, '(L1)') is_valid(matrix)
! Example 2
matrix(:, :) = reshape([1, 1, 1, 1, 2, 2, 2, 2, 3], shape(matrix))
write(*, '(L1)') is_valid(matrix)
contains
function is_valid(matrix) result(result)
implicit none
integer, intent(in) :: matrix(:, :)
integer :: i, j
logical :: result
result = .true.
! Check rows
do i = 1, size(matrix, 1)
do j = 1, size(matrix, 2)
if (count(matrix(i, :) == j) /= 1) then
result = .false.
exit
end if
end do
end do
! Check columns
do j = 1, size(matrix, 2)
do i = 1, size(matrix, 1)
if (count(matrix(:, j) == i) /= 1) then
result = .false.
exit
end if
end do
end do
end function is_valid
end program main
T F
module is_valid_matrix
implicit none
contains
function is_valid_matrix(matrix) result(valid)
integer, intent(in) :: matrix(:, :)
logical :: valid
! Variables
integer :: n, i, j
! Initialize variables
n = size(matrix, 1)
valid = .true.
! Check if every row contains all the integers from 1 to n
do i = 1, n
if (count(matrix(i, :) == i) /= n) then
valid = .false.
exit
end if
end do
! Check if every column contains all the integers from 1 to n
do j = 1, n
if (count(matrix(:, j) == j) /= n) then
valid = .false.
exit
end if
end do
end function is_valid_matrix
end module is_valid_matrix
program test_is_valid_matrix
use is_valid_matrix
implicit none
! Variables
integer, allocatable :: matrix(:, :)
integer :: n
logical :: valid
! Test case 1
n = 3
allocate(matrix(n, n))
matrix = reshape([1, 2, 3, 3, 1, 2, 3, 2, 3], [n, n])
valid = is_valid_matrix(matrix)
if (valid) then
write (*,*) "Test case 1: Pass"
else
write (*,*) "Test case 1: Fail"
end if
! Test case 2
n = 3
allocate(matrix(n, n))
matrix = reshape([1, 1, 1, 1, 2, 3, 3, 3, 3], [n, n])
valid = is_valid_matrix(matrix)
if (.not. valid) then
write (*,*) "Test case 2: Pass"
else
write (*,*) "Test case 2: Fail"
end if
! Test case 3
n = 4
allocate(matrix(n, n))
matrix = reshape([1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4], [n, n])
valid = is_valid_matrix(matrix)
if (valid) then
write (*,*) "Test case 3: Pass"
else
write (*,*) "Test case 3: Fail"
end if
! Test case 4
n = 4
allocate(matrix(n, n))
matrix = reshape([1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3], [n, n])
valid = is_valid_matrix(matrix)
if (.not. valid) then
write (*,*) "Test case 4: Pass"
else
write (*,*) "Test case 4: Fail"
end if
! Test case 5
n = 5
allocate(matrix(n, n))
matrix = reshape([1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5], [n, n])
valid = is_valid_matrix(matrix)
if (valid) then
write (*,*) "Test case 5: Pass"
else
write (*,*) "Test case 5: Fail"
end if
! Test case 6
n = 5
allocate(matrix(n, n))
matrix = reshape([1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3], [n, n])
valid = is_valid_matrix(matrix)
if (.not. valid) then
write (*,*) "Test case 6: Pass"
else
write (*,*) "Test case 6: Fail"
end if
end program test_is_valid_matrix
temp.f95:7:24: 7 | function is_valid_matrix(matrix) result(valid) | 1 Error: MODULE attribute of βis_valid_matrixβ conflicts with PROCEDURE attribute at (1) temp.f95:9:35: 9 | integer, intent(in) :: matrix(:, :) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:10:16: 10 | logical :: valid | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:13:18: 13 | integer :: n, i, j | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:16:19: 16 | n = size(matrix, 1) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:17:14: 17 | valid = .true. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:20:11: 20 | do i = 1, n | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:21:25: 21 | if (count(matrix(i, :) == i) /= n) then | 1 Error: Syntax error in argument list at (1) temp.f95:22:23: 22 | valid = .false. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:23:12: 23 | exit | 1 Error: EXIT statement at (1) is not within a construct temp.f95:24:7: 24 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:25:3: 25 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:28:11: 28 | do j = 1, n | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:29:22: 29 | if (count(matrix(:, j) == j) /= n) then | 1 Error: Syntax error in argument list at (1) temp.f95:30:23: 30 | valid = .false. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:31:12: 31 | exit | 1 Error: EXIT statement at (1) is not within a construct temp.f95:32:7: 32 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:33:3: 33 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:35:3: 35 | end function is_valid_matrix | 1 Error: Expecting END MODULE statement at (1) temp.f95:41:5: 41 | use is_valid_matrix | 1 Fatal Error: Cannot open module file βis_valid_matrix.modβ for reading at (1): No such file or directory compilation terminated.
def count_pairs(nums, target):
count = 0
for i in range(len(nums)):
for j in range(i + 1, len(nums)):
if nums[i] + nums[j] == target or nums[j] + nums[i] == target:
count += 1
return count
The algorithm iterates through all possible pairs of indices i and j where i != j
by using nested loops. For each pair, we check if the concatenation of nums[i] + nums[j]
or nums[j] + nums[i]
equals the target
. If either condition is true, we increment the count. Finally, we return the count of such pairs. This solution has a complexity of O(n^2).
#include <string>
#include <vector>
int countPairs(const std::vector<std::string>& nums, const std::string& target) {
int count = 0;
for (size_t i = 0; i < nums.size(); ++i) {
for (size_t j = i + 1; j < nums.size(); ++j) {
if (nums[i] + nums[j] == target || nums[j] + nums[i] == target) {
++count;
}
}
}
return count;
}
The algorithm iterates through all possible pairs of indices i and j where i != j
by using nested loops. For each pair, we check if the concatenation of nums[i] + nums[j]
or nums[j] + nums[i]
equals the target
. If either condition is true, we increment the count. Finally, we return the count of such pairs. This solution has a complexity of O(n^2).