File tree Expand file tree Collapse file tree 1 file changed +36
-1
lines changed
solution/3400-3499/3480.Maximize Subarrays After Removing One Conflicting Pair Expand file tree Collapse file tree 1 file changed +36
-1
lines changed Original file line number Diff line number Diff line change 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++
You can’t perform that action at this time.
0 commit comments