Leetcode Problem 2511. Maximum Enemy Forts That Can Be Captured

2511. Maximum Enemy Forts That Can Be Captured

Leetcode Solutions

Maximum Enemy Forts Captured by Moving Between Your Forts

  1. Initialize max_captured to 0 to keep track of the maximum number of enemy forts captured.
  2. Initialize current_captured to 0 to keep track of the current number of enemy forts captured while traversing a segment.
  3. Iterate through the forts array using an index i.
    • If forts[i] is 1 (your fort), reset current_captured to 0.
    • If forts[i] is 0 (enemy fort), increment current_captured.
    • If forts[i] is -1 (empty position) and current_captured is greater than 0, update max_captured with the maximum of max_captured and current_captured, then reset current_captured to 0.
  4. Return max_captured as the result.
UML Thumbnail

Two-Pointer Approach to Capture Maximum Enemy Forts

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...