At Stripe, multiple workers acquire locks in sequences called plans. To avoid deadlocks, all workers must follow a single global lock order (a total order of lock IDs). You may fix each plan using adjacent swaps only.
Return the minimum total number of adjacent swaps needed across all plans so that there exists one global order in which every plan becomes increasing (i.e., each plan is a subsequence of that global order).
plans: list[list[int]] where each inner list contains distinct integers (lock IDs).int = minimum total adjacent swaps across all plans.plans = [[3, 1, 2], [1, 2, 3]] → 2
[1, 2, 3]. First plan needs 2 adjacent swaps to become [1,2,3]; second needs 0.plans = [[2, 5, 1, 3, 4]] → 3
[2, 5, 1, 3, 4] itself. Minimum adjacent swaps to match that order equals the inversion count relative to it, which is 3.0 <= len(plans) <= 2 * 10^4sum(len(plans[i])) <= 2 * 10^51 <= plans[i][j] <= 10^9plans = [[3, 1, 2], [1, 2, 3]]Output2WhyGlobal order [1,2,3] works. First plan needs 2 adjacent swaps; second needs 0.plans = [[2, 5, 1, 3, 4]]Output3WhyUsing the implied global order, the plan’s index sequence has 3 inversions, so 3 adjacent swaps are needed.0 <= len(plans) <= 2 * 10^4sum(len(plans[i])) <= 2 * 10^51 <= plans[i][j] <= 10^9All values inside a single plan are distinctdef min_total_swaps_to_enforce_order(plans: list[list[int]]) -> int: