2511. Maximum Enemy Forts That Can Be Captured LeetCode Solution

In this guide, you will get 2511. Maximum Enemy Forts That Can Be Captured LeetCode Solution with the best time and space complexity. The solution to Maximum Enemy Forts That Can Be Captured problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Maximum Enemy Forts That Can Be Captured solution in C++
  4. Maximum Enemy Forts That Can Be Captured solution in Java
  5. Maximum Enemy Forts That Can Be Captured solution in Python
  6. Additional Resources
2511. Maximum Enemy Forts That Can Be Captured LeetCode Solution image

Problem Statement of 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

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

2511. Maximum Enemy Forts That Can Be Captured LeetCode Solution in C++

class Solution {
 public:
  int captureForts(vector<int>& forts) {
    int ans = 0;

    for (int i = 0, j = 0; i < forts.size(); ++i)
      if (forts[i] != 0) {  // -1 or 1
        if (forts[i] == -forts[j])
          ans = max(ans, i - j - 1);
        j = i;
      }

    return ans;
  }
};
/* code provided by PROGIEZ */

2511. Maximum Enemy Forts That Can Be Captured LeetCode Solution in Java

class Solution {
  public int captureForts(int[] forts) {
    int ans = 0;

    for (int i = 0, j = 0; i < forts.length; ++i)
      if (forts[i] != 0) { // -1 or 1
        if (forts[i] == -forts[j])
          ans = Math.max(ans, i - j - 1);
        j = i;
      }

    return ans;
  }
}
// code provided by PROGIEZ

2511. Maximum Enemy Forts That Can Be Captured LeetCode Solution in Python

class Solution:
  def captureForts(self, forts: list[int]) -> int:
    ans = 0

    j = 0
    for i, fort in enumerate(forts):
      if fort != 0:  # -1 or 1
        if fort == -forts[j]:
          ans = max(ans, i - j - 1)
        j = i

    return ans
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.