A store's analytics team wants every click path a shopper could take from the landing page to checkout. Pages are numbered 0 to n - 1, where n = pages.length. pages[i] lists the pages that page i links to. Page 0 is the landing page and page n - 1 is checkout. The links never form a loop, and checkout links nowhere.
Write allRoutes(pages) that returns every path from page 0 to page n - 1, each as a list of page numbers. Sort the paths in lexicographic order: compare two paths page by page, and the path with the smaller page at the first difference comes first.
Input: pages = [[1,2],[3],[3],[]] Output: [[0,1,3],[0,2,3]]
Page 0 leads to 1 and 2, and both lead to checkout (page 3).
Input: pages = [[2,1],[2],[]] Output: [[0,1,2],[0,2]]
[0, 1, 2] comes first because at the second position 1 is smaller than 2.
2 <= pages.length <= 15pages[n - 1] is empty.Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #417 Every Route to Checkout. 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.