Skip to content

Commit 361d2a2

Browse files
authored
Added Java Solution
Added Java solution for the problem no 3480. Maximize Subarrays After Removing One Conflicting Pair.
1 parent 7a005e6 commit 361d2a2

File tree

1 file changed

+36
-1
lines changed
  • solution/3400-3499/3480.Maximize Subarrays After Removing One Conflicting Pair

1 file changed

+36
-1
lines changed

solution/3400-3499/3480.Maximize Subarrays After Removing One Conflicting Pair/README.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,42 @@ tags:
9595
#### Java
9696

9797
```java
98-
98+
class Solution {
99+
public long maxSubarrays(int n, int[][] conflictingPairs) {
100+
List<List<Integer>> conflicts = new ArrayList<>();
101+
for(int i=0 ; i<=n ; i++)
102+
conflicts.add(new ArrayList<>());
103+
104+
for(int[] c : conflictingPairs) {
105+
int left = c[0], right = c[1];
106+
if(left>right) {
107+
int temp = left;
108+
left = right;
109+
right = temp;
110+
}
111+
conflicts.get(right).add(left);
112+
}
113+
long[] restrictRemoval = new long[n+1];
114+
int leftMaxRestrict = 0, leftSecondMaxRestrict = 0;
115+
long res = 0L;
116+
for(int i=1 ; i<=n ; i++) {
117+
for(Integer ele : conflicts.get(i)) {
118+
if(ele > leftMaxRestrict) {
119+
leftSecondMaxRestrict = leftMaxRestrict;
120+
leftMaxRestrict = ele;
121+
} else if(ele > leftSecondMaxRestrict)
122+
leftSecondMaxRestrict = ele;
123+
}
124+
res += 0L + i - leftMaxRestrict;
125+
restrictRemoval[leftMaxRestrict] += leftMaxRestrict - leftSecondMaxRestrict;
126+
}
127+
long maxRemovalVal = 0L;
128+
for(int i=1 ; i<=n ; i++)
129+
maxRemovalVal = Math.max(maxRemovalVal, restrictRemoval[i]);
130+
res += maxRemovalVal;
131+
return res;
132+
}
133+
}
99134
```
100135

101136
#### C++

0 commit comments

Comments
 (0)