This is the "Hard" version of the distribution problem. In Distribute Elements Into Two Arrays II, the rule for placement changes: you compare the number of elements already in the array that are strictly greater than the current element. If arr1 has more such elements, the new element goes to arr1. If arr2 has more, it goes to arr2. If they are equal, it goes to the array with fewer elements. If still equal, it goes to arr1.
Autodesk uses this to test your ability to optimize "Greater Than" queries in a data stream. A naive search for every new element would result in overall, which is too slow for large inputs (). It evaluation your knowledge of Binary Indexed Trees (BIT) or Segment Trees to perform point updates and range queries in time.
This problem uses Coordinate Compression and a Binary Indexed Tree (BIT).
nums to the range .nums = [2, 1, 3, 3], arr1=[2], arr2=[1]. Next is 3.
arr1 has 0 elements .arr2 has 0 elements .arr1 has 1 element, arr2 has 1 element.arr1. arr1 = [2, 3].
Next is 3.arr1 has 0 elements (it has 2, 3... neither is ).count function, which is .When you see "count elements greater than X" in a dynamic array, think Binary Indexed Tree or Segment Tree. These are the standard tools for order statistics. Practice coordinate compression as it is a prerequisite for using these structures with large input values.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Maximum Profitable Triplets With Increasing Prices II | Hard | Solve | |
| Peaks in Array | Hard | Solve | |
| Maximum Profitable Triplets With Increasing Prices I | Medium | Solve | |
| Queries on a Permutation With Key | Medium | Solve | |
| Block Placement Queries | Hard | Solve |