Tap here to switch tabs
Problem
Submissions

Problem: Maximum Enemy Forts That Can Be Captured

Try to solve the Maximum Enemy Forts That Can Be Captured problem.

Statement

You are given an integer array forts of length n representing the state of forts along a line:

  • forts[i] = −1-1 indicates that position i is empty.

  • forts[i] = 00 indicates that position i contains an enemy fort.

  • forts[i] = 11 indicates that position i contains a fort under your command.

You want to move your army from a fort under your command at position i to an empty fort at position j such that every position strictly between i and j contains an enemy fort. That is, for every index k where min⁡(i,j)<k<max⁡(i,j)\min(i, j) < k < \max(i, j), forts[k] = 00.

All enemy forts between i and j are captured during this movement.

Return the maximum number of enemy forts that can be captured. If no movement is possible, return 00.

Constraints:

  • 1≤1 \leq forts.length ≤1000\leq 1000

  • −1≤-1 \leq forts[i] ≤1\leq 1

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Maximum Enemy Forts That Can Be Captured

Try to solve the Maximum Enemy Forts That Can Be Captured problem.

Statement

You are given an integer array forts of length n representing the state of forts along a line:

  • forts[i] = −1-1 indicates that position i is empty.

  • forts[i] = 00 indicates that position i contains an enemy fort.

  • forts[i] = 11 indicates that position i contains a fort under your command.

You want to move your army from a fort under your command at position i to an empty fort at position j such that every position strictly between i and j contains an enemy fort. That is, for every index k where min⁡(i,j)<k<max⁡(i,j)\min(i, j) < k < \max(i, j), forts[k] = 00.

All enemy forts between i and j are captured during this movement.

Return the maximum number of enemy forts that can be captured. If no movement is possible, return 00.

Constraints:

  • 1≤1 \leq forts.length ≤1000\leq 1000

  • −1≤-1 \leq forts[i] ≤1\leq 1