There are n
rings and each ring is either red, green, or blue. The rings are distributed across ten rods labeled from 0
to 9
.
You are given a string rings
of length 2n
that describes the n
rings that are placed onto the rods. Every two characters in rings
forms a color-position pair that is used to describe each ring where:
ith
pair denotes the ith
ring's color ('R'
, 'G'
, 'B'
).ith
pair denotes the rod that the ith
ring is placed on ('0'
to '9'
).For example, "R3G2B1 "
describes n == 3
rings: a red ring placed onto the rod labeled 3, a green ring placed onto the rod labeled 2, and a blue ring placed onto the rod labeled 1.
Return the number of rods that have all three colors of rings on them.
Example 1:
Input: rings = "B0B6G0R6R0R6G9 " Output: 1 Explanation: - The rod labeled 0 holds 3 rings with all colors: red, green, and blue. - The rod labeled 6 holds 3 rings, but it only has red and blue. - The rod labeled 9 holds only a green ring. Thus, the number of rods with all three colors is 1.
Example 2:
Input: rings = "B0R0G0R9R0B0G0 " Output: 1 Explanation: - The rod labeled 0 holds 6 rings with all colors: red, green, and blue. - The rod labeled 9 holds only a red ring. Thus, the number of rods with all three colors is 1.
Example 3:
Input: rings = "G4 " Output: 0 Explanation: Only one ring is given. Thus, no rods have all three colors.
Constraints:
rings.length == 2 * n
1 <= n <= 100
rings[i]
where i
is even is either 'R'
, 'G'
, or 'B'
(0-indexed).rings[i]
where i
is odd is a digit from '0'
to '9'
(0-indexed).program main
implicit none
integer :: i, j, k, n, m, num_roads
character(len=2) :: rings
character(len=1), dimension(:), allocatable :: colors
integer, dimension(:), allocatable :: positions
! read input
read(*,*) n
allocate(colors(n))
allocate(positions(n))
read(*,*) rings
! parse input
do i = 1, n
j = (i - 1)*2 + 1
k = (i - 1)*2 + 2
colors(i) = rings(j:j)
positions(i) = iachar(rings(k:k)) - iachar('0')
end do
! solve problem
num_roads = 0
do i = 0, 9
if (count(positions == i) == 3) then
num_roads = num_roads + 1
end if
end do
! print output
write(*,*) num_roads
end program main
At line 10 of file temp.f95 (unit = 5, file = 'stdin') Fortran runtime error: End of file Error termination. Backtrace: #0 0x7cca34856960 in ??? #1 0x7cca348574d9 in ??? #2 0x7cca34aab17b in ??? #3 0x7cca34aa4684 in ??? #4 0x7cca34aa52aa in ??? #5 0x5bd1854382bc in MAIN__ #6 0x5bd185438771 in main
module rings
implicit none
contains
integer function count_rods(rings) result(num_rods)
! This function takes a string of length 2n that describes the n rings
! placed onto the rods. Every two characters in rings forms a color-position
! pair that is used to describe each ring where:
!
! * The first character of the ith pair denotes the ith ring's color
! ('R', 'G', 'B').
! * The second character of the ith pair denotes the rod that the ith ring
! is placed on (0 to 9).
!
! For example, "R3G2B1 " describes n == 3 rings: a red ring placed onto the
! rod labeled 3, a green ring placed onto the rod labeled 2, and a blue ring
! placed onto the rod labeled 1.
!
! Return the number of rods that have all three colors of rings on them.
integer :: i, j, k, n
character(len=2) :: color
character(len=10) :: rod
logical :: has_red, has_green, has_blue
! Initialize variables
n = len(rings) / 2
has_red = .false.
has_green = .false.
has_blue = .false.
! Loop through each ring and update the has_color variables
do i = 1, n
color = rings(2 * (i - 1) + 1:2 * (i - 1) + 2)
select case (color)
case ("R")
has_red = .true.
case ("G")
has_green = .true.
case ("B")
has_blue = .true.
case default
write (*,*) "Invalid color: ", color
stop
end select
end do
! Loop through each rod and count the number of rings with all three colors
num_rods = 0
do i = 0, 9
rod = char(i + 48)
if (has_red .and. has_green .and. has_blue) then
num_rods = num_rods + 1
end if
end do
end function count_rods
end module rings
program test_rings
use rings
implicit none
! Test case 1:
! "B0B6G0R6R0R6G9 "
! Expected output: 1
call test_case("B0B6G0R6R0R6G9 ", 1)
! Test case 2:
! "B0R0G0R9R0B0G0 "
! Expected output: 1
call test_case("B0R0G0R9R0B0G0 ", 1)
! Test case 3:
! "G4 "
! Expected output: 0
call test_case("G4 ", 0)
contains
subroutine test_case(rings, expected)
character(len=*), intent(in) :: rings
integer, intent(in) :: expected
integer :: actual
actual = count_rods(rings)
if (actual == expected) then
write (*,*) "Pass: ", rings
else
write (*,*) "Fail: ", rings, " Expected: ", expected, " Actual: ", actual
end if
end subroutine test_case
end program test_rings
temp.f95:37:34: 37 | color = rings(2 * (i - 1) + 1:2 * (i - 1) + 2) | 1 Error: Syntax error in argument list at (1) temp.f95:7:33: 7 | integer function count_rods(rings) result(num_rods) | 1 Error: Symbol βringsβ at (1) has no IMPLICIT type temp.f95:66:5: 66 | use rings | 1 Fatal Error: Cannot open module file βrings.modβ for reading at (1): No such file or directory compilation terminated.
def findFarmland(land):
m, n = len(land), len(land[0])
res = []
for i in range(m):
for j in range(n):
if land[i][j] == 1:
r, c = i, j
while r < m and land[r][j] == 1: r += 1
while c < n and land[i][c] == 1: c += 1
for x in range(i, r):
for y in range(j,c):
land[x][y] = 0
res.append([i, j, r - 1, c - 1])
return res
1. Iterate over each cell (i, j) in the given land matrix.
#include <vector>
using namespace std;
vector<vector<int>> findFarmland(vector<vector<int>>& land) {
int m = land.size(), n = land[0].size();
vector<vector<int>> res;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (land[i][j] == 1) {
int r = i, c = j;
while (r < m && land[r][j] == 1) ++r;
while (c < n && land[i][c] == 1) ++c;
for (int x = i; x < r; ++x) {
for (int y = j; y < c; ++y) {
land[x][y] = 0;
}
}
res.push_back({i, j, r - 1, c - 1});
}
}
}
return res;
}
1. Iterate over each cell (i, j) in the given land matrix.