- sliding window
- dynamic programming (on number of men needed to conquer a maximum number of islands)
Observe that a valid plan may either consist of neighboring islands from only a single waterway or from neighboring islands of two waterways joined at vertex
To compute the maximum number of islands that can be conquered on a single waterway we can use the standard sliding window technique.
For the joined waterways, we iterate over all waterways. We use a dynamic program that memoizes the maximum number of islands (among all previous waterways) that can be conquered using
The complexity of our algorithm is