Maximum Enemy Forts That Can Be Captured

🏠 ⬅️ ➡️

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
  • The army travels over enemy forts only. Formally, for all 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

Note: This problem is from LeetCode.
Compiled
Executed
Correct
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
Compiled
Executed
Correct
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
🌐 Data from online sources
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.
  1. Iterate through each element in the array.
  2. If the current fort is our fort (1), we iterate through the rest of the elements.
  3. If the next element j is not an empty position (-1), we count the number of enemy forts we can capture.
  4. If forts[j] is equal to 1, it means it's our fort, and we update the maximum number of captures.
  5. After iterating through all elements, return the maximum number of captures.
🌐 Data from online sources
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.
  1. Iterate through each element in the array.
  2. If the current fort is our fort (1), we iterate through the rest of the elements.
  3. If the next element j is not an empty position (-1), we count the number of enemy forts we can capture.
  4. If forts[j] is equal to 1, it means it's our fort, and we update the maximum number of captures.
  5. After iterating through all elements, return the maximum number of captures.