An e-commerce site has n shopper accounts numbered 0 to n - 1. A cleanup job finds pairs of accounts that belong to the same person and reports them one at a time in merges. Merging is transitive: if a matches b and b matches c, all three are one shopper.
Write shoppersAfterMerges(n, merges) that returns an array with one number per merge: how many distinct shoppers there are right after that merge is applied.
Input: n = 4, merges = [[0,1],[2,3],[1,0],[1,2]] Output: [3,2,2,1]
Merging 0 and 1 leaves 3 shoppers, then 2 and 3 leaves 2. Merging 1 and 0 again changes nothing. Merging 1 and 2 joins everyone.
Input: n = 2, merges = [] Output: []
No merges, so no counts to report.
1 <= n <= 1000000 <= merges.length <= 1000000 <= a, b < nRun tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #454 Count Unique Shoppers After Each Merge. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.