Asked in: AMAZON
import bisect
def getMaxMatching(match):
# Write your code here
n = len(match)
// ... rest of solution available after purchase
```
To tackle this problem, begin by carefully interpreting what the intersection rule means in a visual and logical sense. Imagine two columns: one on the left with indices from 0 to n-1 (or 1 to n for 1-based indexing), and a right column where each entry at index i is match[i], which connects left index i to match[i] on the right. Every i maps to one value, meaning you draw a line from i to match[i].
The constraint is that some lines "intersect," and those intersections make certain pairs of matchings invalid when combined. The challenge is to pick the largest possible subset of these matchings that do not intersect with each other.
First, consider what causes an intersection. Two matchings (i, match[i]) and (j, match[j]) intersect if:
- i < j and match[i] > match[j], or
- i > j and match[i] ≤ match[j]
This is essentially saying: if two lines cross each other visually in the matching diagram, they are considered intersecting. If they don’t cross — meaning they maintain consistent increasing order from left to right — then they are non-intersecting.
A good way to represent this is by considering the set of matchings as a series of ordered pairs and trying to find the largest subset that is strictly increasing both in terms of left index and the match value.
Now think in terms of subsequences. If you fix the left indices as given (since they are fixed by position), and consider only the match[i] values, you need to find the longest increasing subsequence (LIS) of match[i]s. Why? Because any two indices i and j such that i < j and match[i] < match[j] will not intersect — their lines move from bottom-left to top-right without crossing. This is the exact condition for a non-intersecting pair.
So, instead of checking all pairs for intersections directly (which would be computationally expensive given n can go up to 10^5), you reframe the problem as finding the longest increasing subsequence in the array match[]. This problem is well-known and has efficient solutions. But remember, you're not being asked to implement it here — the focus is on the approach.
Think recursively or greedily about building such a subsequence. Starting from the first element, you want to extend your sequence as long as possible by only including a next element if it is greater than the last included one. If not, you consider replacing elements in the current sequence to optimize the potential for longer sequences in the future.
It's important to realize that match[i] values do not have to be unique and can repeat. So, when thinking about the increasing subsequence, you have to decide whether you want strictly increasing or non-decreasing sequences. In this problem, since intersections include both greater-than and less-than-or-equal-to conditions, the safe bet is to use strictly increasing sequences for avoiding all types of intersections.
To further solidify this perspective, consider a case where match = [1, 2, 3, 4, 5]. The matchings go straight from left to right with no overlap — a perfect increasing sequence and hence no intersections. In this case, the number of valid matchings equals the length of the entire array.
On the other hand, if match = [5, 4, 3, 2, 1], every line would intersect with others, and only one matching could be taken at a time without causing an intersection. Hence, the maximum number of non-intersecting matches in this case is just 1.
Your job is to generalize this observation. You want to extract the maximum number of elements from match[] such that the order of their values is strictly increasing — because this guarantees no intersection.
So, the overall strategy is:
1. Realize that the order of left indices is fixed — you’re moving from left to right.
2. Focus on selecting match[i] values such that they form a strictly increasing subsequence.
3. Understand that this maps to the well-known problem of finding the length of the longest increasing subsequence in an array.
By reframing the original intersection-based constraint in terms of increasing sequences, you convert a complex geometric problem into a manageable algorithmic one. The real key is in understanding the correspondence between non-intersecting matchings and increasing sequences in the array of match[i] values.
This insight simplifies the challenge significantly, allowing you to efficiently determine the maximum number of non-intersecting matches by focusing solely on the structure of the match array.
```