Monotonic-Run Shuffle Score
Problem statement
Given a nonempty array nums of distinct integers, divide it into maximal contiguous strictly monotonic runs. A run is either strictly increasing or strictly decreasing, and a new run begins whenever the sign of the adjacent difference changes.
Return the number of runs divided by nums.length as a simple shuffledness score.
Function
shuffleScore(nums: int[]) → doubleExamples
Example 1
nums = [1,2,3,4]return = 0.25The entire array is one increasing run, so the score is 1 / 4.
Example 2
nums = [1,3,2,4]return = 0.75The direction changes twice, creating three runs, so the score is 3 / 4.
Constraints
1 <= nums.length <= 100000-10^9 <= nums[i] <= 10^9- All values are distinct
- Results use absolute tolerance
1e-12