Loading...
You are given an integer n (labels 0 to n - 1) and an array pairs, where each pairs[i] = [a, b] (a != b) is a forbidden pair.
An interval [L, R] (with 0 <= L <= R <= n - 1) contains a forbidden pair [a, b] if both a and b lie inside it, meaning L <= min(a, b) and max(a, b) <= R.
Return the number of intervals [L, R] that contain no forbidden pair. The same pair may appear multiple times in pairs; duplicates do not change the answer.
Input: n = 4, pairs = [[1,3]]
Output: 8
Explanation: Of the 10 intervals over labels 0..3, only [0,3] and [1,3] contain both 1 and 3. The other 8 are valid.
Input: n = 3, pairs = [[0,1],[1,2]]
Output: 3
Explanation: Only the single-label intervals [0,0], [1,1], [2,2] avoid both pairs.
Input: n = 5, pairs = []
Output: 15
Explanation: With no forbidden pairs, all n * (n + 1) / 2 = 15 intervals are valid.
pairs.length ≤2⋅105pairs[i] =[a,b] with 0≤a,b<n and a=bClick "Run" to test with sample cases or "Submit" to run all tests.