Greater Count Distribution
HardArrays · Uber
Distribute the values of nums into two arrays, first and second, one value at a time:
nums[0]goes tofirstandnums[1]goes tosecond.- For each later value
v, letabe the number of elements already infirstthat are strictly greater thanv, andbthe same count forsecond. - If
a > b, appendvtofirst. - If
a < b, appendvtosecond. - If
a == b, appendvto whichever array currently has fewer elements; if they have the same length, append tofirst.
Return first followed by second, concatenated into one list.
Example 1
- Input:
- nums = [4, 9, 3, 7, 7, 2]
- Output:
- [4, 3, 9, 7, 7, 2]
- Explanation
- Start with
first = [4],second = [9]. For 3 both counts are 1 and the lengths tie, so it goes tofirst. Each 7 sees no greater value infirstbut the 9 insecond, so both go tosecond. For 2,firsthas 2 greater values andsecond = [9, 7, 7]has 3, sosecondagain. Result:[4, 3]+[9, 7, 7, 2].
Example 2
- Input:
- nums = [1, 1, 1, 1]
- Output:
- [1, 1, 1, 1]
- Explanation
- Nothing is ever strictly greater, so placement alternates by length:
first, thensecond, giving[1, 1]+[1, 1].
Example 3
- Input:
- nums = [10, 2, 5, 1, 8]
- Output:
- [10, 5, 1, 8, 2]
- Explanation
- 5 sees one greater value in
first(10) and none insecond, so it joinsfirst. 1 sees two greater values infirstvs one insecond, sofirstagain. 8 sees one greater (10) vs none, sofirst, ending with[10, 5, 1, 8]+[2].
Constraints
3 <= nums.length <= 10^51 <= nums[i] <= 10^9