Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 983e2ea

Browse files
solves #2511: Maximum Enemy Forts That Can Be Captured in java
1 parent 5c9661f commit 983e2ea

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,7 @@
789789
| 2496 | [Maximum Value of a String in an Array](https://leetcode.com/problems/maximum-value-of-a-string-in-an-array) | [![Java](assets/java.png)](src/MaximumValueOfAStringInAnArray.java) | |
790790
| 2500 | [Delete Greatest Value in Each Row](https://leetcode.com/problems/delete-greatest-value-in-each-row) | [![Java](assets/java.png)](src/DeleteGreatestValueInEachRow.java) | |
791791
| 2506 | [Count Pairs Of Similar Strings](https://leetcode.com/problems/count-pairs-of-similar-strings) | [![Java](assets/java.png)](src/CountPairsOfSimilarStrings.java) | |
792-
| 2511 | [Maximum Enemy Forts That Can Be Captured](https://leetcode.com/problems/maximum-enemy-forts-that-can-be-captured) | | |
792+
| 2511 | [Maximum Enemy Forts That Can Be Captured](https://leetcode.com/problems/maximum-enemy-forts-that-can-be-captured) | [![Java](assets/java.png)](src/MaximumEnemyFortsThatCanBeCaptured.java) | |
793793
| 2515 | [Shortest Distance to Target String in a Circular Array](https://leetcode.com/problems/shortest-distance-to-target-string-in-a-circular-array) | | |
794794
| 2520 | [Count the Digits That Divide a Number](https://leetcode.com/problems/count-the-digits-that-divide-a-number) | | |
795795
| 2525 | [Categorize Box According to Criteria](https://leetcode.com/problems/categorize-box-according-to-criteria) | | |
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// https://leetcode.com/problems/maximum-enemy-forts-that-can-be-captured
2+
// T: O(N)
3+
// S: O(1)
4+
5+
public class MaximumEnemyFortsThatCanBeCaptured {
6+
public int captureForts(int[] forts) {
7+
int last1 = Integer.MAX_VALUE, last_1 = Integer.MAX_VALUE, maxCapture = 0, previous = 0;
8+
for (int index = 0 ; index < forts.length ; index++) {
9+
if (forts[index] == 1) {
10+
if (last_1 != Integer.MAX_VALUE && previous == -1) {
11+
maxCapture = Math.max(maxCapture, index - last_1 - 1);
12+
}
13+
previous = 1;
14+
last1 = index;
15+
} else if (forts[index] == -1) {
16+
if (last1 != Integer.MAX_VALUE && previous == 1) {
17+
maxCapture = Math.max(maxCapture, index - last1 - 1);
18+
}
19+
previous = -1;
20+
last_1 = index;
21+
}
22+
}
23+
return maxCapture;
24+
}
25+
}

0 commit comments

Comments
 (0)