TimSort, the sort behind Array.prototype.sort in V8, starts by cutting the list into natural runs. Starting at index start, it looks at the next item. If it is strictly smaller, the run is strictly descending and extends while each item is strictly smaller than the one before; that run is then reversed. Otherwise the run is non-decreasing and extends while each item is at least the one before. The next run starts right after.
Write findRuns(nums) that returns the runs, in order, as arrays (descending runs already reversed into ascending order).
Input: nums = [1,4,7,9,8,6,3,2,5,10] Output: [[1,4,7,9],[2,3,6,8],[5,10]]
1, 4, 7, 9 goes up; 8, 6, 3, 2 goes strictly down and is reversed; 5, 10 goes up.
Input: nums = [5,5,3,3] Output: [[5,5],[3,3]]
5, 5 is non-decreasing. 3, 3 starts a new run that is also non-decreasing, because 3 is not smaller than 3.
0 <= nums.length <= 10000-10^6 <= nums[i] <= 10^6Run 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 #211 Find the Natural Runs. 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.