diff --git a/solution/3400-3499/3480.Maximize Subarrays After Removing One Conflicting Pair/README.md b/solution/3400-3499/3480.Maximize Subarrays After Removing One Conflicting Pair/README.md index 0426c3f26aa76..de276445e87d6 100644 --- a/solution/3400-3499/3480.Maximize Subarrays After Removing One Conflicting Pair/README.md +++ b/solution/3400-3499/3480.Maximize Subarrays After Removing One Conflicting Pair/README.md @@ -95,7 +95,42 @@ tags: #### Java ```java - +class Solution { + public long maxSubarrays(int n, int[][] conflictingPairs) { + List> 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++