Skip to content

Need to Pull code to update master branch #4606

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 12 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,42 @@ tags:
#### Java

```java

class Solution {
public long maxSubarrays(int n, int[][] conflictingPairs) {
List<List<Integer>> conflicts = new ArrayList<>();
for(int i=0 ; i<=n ; i++)
conflicts.add(new ArrayList<>());

for(int[] c : conflictingPairs) {
int left = c[0], right = c[1];
if(left>right) {
int temp = left;
left = right;
right = temp;
}
conflicts.get(right).add(left);
}
long[] restrictRemoval = new long[n+1];
int leftMaxRestrict = 0, leftSecondMaxRestrict = 0;
long res = 0L;
for(int i=1 ; i<=n ; i++) {
for(Integer ele : conflicts.get(i)) {
if(ele > leftMaxRestrict) {
leftSecondMaxRestrict = leftMaxRestrict;
leftMaxRestrict = ele;
} else if(ele > leftSecondMaxRestrict)
leftSecondMaxRestrict = ele;
}
res += 0L + i - leftMaxRestrict;
restrictRemoval[leftMaxRestrict] += leftMaxRestrict - leftSecondMaxRestrict;
}
long maxRemovalVal = 0L;
for(int i=1 ; i<=n ; i++)
maxRemovalVal = Math.max(maxRemovalVal, restrictRemoval[i]);
res += maxRemovalVal;
return res;
}
}
```

#### C++
Expand Down