You are given a 0-indexed integer array forts
of length n
representing the positions of several forts. forts[i]
can be -1
, 0
, or 1
where:
-1
represents there is no fort at the ith
position.0
indicates there is an enemy fort at the ith
position.1
indicates the fort at the ith
the position is under your command.Now you have decided to move your army from one of your forts at position i
to an empty position j
such that:
0 <= i, j <= n - 1
k
where min(i,j) < k < max(i,j)
, forts[k] == 0.
While moving the army, all the enemy forts that come in the way are captured.
Return the maximum number of enemy forts that can be captured. In case it is impossible to move your army, or you do not have any fort under your command, return 0
.
Example 1:
Input: forts = [1,0,0,-1,0,0,0,0,1] Output: 4 Explanation: - Moving the army from position 0 to position 3 captures 2 enemy forts, at 1 and 2. - Moving the army from position 8 to position 3 captures 4 enemy forts. Since 4 is the maximum number of enemy forts that can be captured, we return 4.
Example 2:
Input: forts = [0,0,1,-1] Output: 0 Explanation: Since no enemy fort can be captured, 0 is returned.
Constraints:
1 <= forts.length <= 1000
-1 <= forts[i] <= 1
program main
implicit none
integer, parameter :: n = 9
integer, dimension(n) :: forts
integer :: i, j, k, max_enemies
! Example 1
forts = [-1, 0, 0, -1, 0, 0, 0, 0, 1]
write(*,*) "Example 1"
write(*,*) "Forts:", forts
call solve(forts, i, j, k, max_enemies)
write(*,*) "Max enemies:", max_enemies
! Example 2
forts = [0, 0, 1, -1]
write(*,*) "Example 2"
write(*,*) "Forts:", forts
call solve(forts, i, j, k, max_enemies)
write(*,*) "Max enemies:", max_enemies
contains
subroutine solve(forts, i, j, k, max_enemies)
implicit none
integer, intent(in) :: forts(:)
integer, intent(out) :: i, j, k, max_enemies
logical :: found_empty
! Initialize variables
i = 0
j = 0
k = 0
max_enemies = 0
found_empty = .false.
! Loop through the forts
do while (i /= n)
if (forts(i) == 0) then
! Enemy fort found
max_enemies = max(max_enemies, k - i + 1)
else if (forts(i) == 1) then
! Your fort found
if (.not. found_empty) then
! Found an empty position
j = i
found_empty = .true.
end if
end if
! Move to next fort
i = i + 1
end do
! Check if we found an empty position
if (found_empty) then
! Move the army
do while (i /= j)
if (forts(i) == 0) then
! Enemy fort found
max_enemies = max(max_enemies, k - i + 1)
end if
! Move to next fort
i = i + 1
end do
end if
end subroutine solve
end program main
temp.f95:16:4: 16 | forts = [0, 0, 1, -1] | 1 Error: Different shape for array assignment at (1) on dimension 1 (9 and 4)
program capture_enemy_forts
implicit none
integer, parameter :: n = 1000
integer :: forts(n)
integer :: i, j, k, max_enemy_forts
read(*,*) forts
max_enemy_forts = 0
do i = 0, n - 1
do j = i + 1, n - 1
if (forts(i) == 0 .and. forts(j) == 0) then
do k = min(i, j) + 1, max(i, j) - 1
if (forts(k) == 0) then
max_enemy_forts = max_enemy_forts + 1
end if
end do
end if
end do
end do
write(*,*) max_enemy_forts
end program capture_enemy_forts
At line 8 of file temp.f95 (unit = 5, file = 'stdin') Fortran runtime error: End of file Error termination. Backtrace: #0 0x788f2dceb960 in ??? #1 0x788f2dcec4d9 in ??? #2 0x788f2df4017b in ??? #3 0x788f2df39684 in ??? #4 0x788f2df3a2aa in ??? #5 0x788f2df3fb7a in ??? #6 0x568a42f29282 in MAIN__ #7 0x568a42f29419 in main
def max_captured_forts(forts):
n = len(forts)
max_capture = 0
for i in range(n):
if forts[i] == 1:
for j in range(i+1, n):
if forts[j] != -1:
capture = sum(1 for k in range(i+1, j) if forts[k] == 0)
if forts[j] == 1:
max_capture = max(max_capture, capture)
return max_capture
For each of our fort (1), we try to move our army to another our fort (1) through enemy forts (0). For each pair of our forts, we count how many enemy forts we can capture in between our forts. We are only interested in the pair that captures the maximum number of enemy forts.
j
is not an empty position (-1), we count the number of enemy forts we can capture.forts[j]
is equal to 1, it means it's our fort, and we update the maximum number of captures.int maxCapturedForts(vector<int>& forts) {
int n = forts.size();
int maxCapture = 0;
for(int i = 0; i < n; i++) {
if(forts[i] == 1) {
for(int j = i + 1; j < n; j++) {
if(forts[j] != -1) {
int capture = 0;
for(int k = i + 1; k < j; k++) {
if(forts[k] == 0) capture++;
}
if(forts[j] == 1) maxCapture = max(maxCapture, capture);
}
}
}
}
return maxCapture;
}
For each of our fort (1), we try to move our army to another our fort (1) through enemy forts (0). For each pair of our forts, we count how many enemy forts we can capture in between our forts. We are only interested in the pair that captures the maximum number of enemy forts.
j
is not an empty position (-1), we count the number of enemy forts we can capture.forts[j]
is equal to 1, it means it's our fort, and we update the maximum number of captures.