Discretization
Introduction
Discretization is a data processing technique that can be essentially viewed as a form of hashing, ensuring that the data maintains its original total/partial order relationship after hashing.
In simpler terms, when certain data cannot be conveniently used as array indices due to their large size or unsupported type, and only the relative magnitude between elements affects the final result, we can process the original data based on their rankings—this is discretization.
Data that can be discretized includes large integers, floating-point numbers, strings, etc.
Implementation
Discretizing an array and performing queries is a common application scenario.
Method One
Typically, the original array contains duplicate elements, which are usually discretized into the same value.
The method is as follows:
- Create a copy of the original array.
- Sort the copy in ascending order.
- Remove duplicates from the sorted copy.
- Find the position of each element of the original array in the copy; this position is its ranking and serves as the discretized value.
1 2 3 4 5 6 7 8 | |
The STL algorithms used in the reference implementation can be found in STL Algorithms.
Similarly, we can also discretize a std::vector:
1 2 3 4 5 6 | |
Method Two
Depending on the problem requirements, sometimes identical elements need to be discretized into different values based on their input order.
In this case, using the std::lower_bound() function becomes more challenging, and a different approach is needed:
- Create a copy of the original array while recording the position of each element.
- Sort the copy by value in ascending order; when values are equal, sort by occurrence order in ascending order.
- Assign the discretized values back to the original array.
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
Complexity
For Method One, deduplication has a complexity of \(O(n)\), sorting has a complexity of \(O(n \log n)\), and the final \(n\) lookups have a complexity of \(O(n \log n)\).
For Method Two, sorting has a complexity of \(O(n \log n)\).
Thus, the total time complexity for both methods is \(O(n \log n)\).
The space complexity is \(O(n)\).
Exercises
本页面最近更新:,更新历史
发现错误?想一起完善? 在 GitHub 上编辑此页!
本页面贡献者:OI-wiki
本页面的全部内容在 CC BY-SA 4.0 和 SATA 协议之条款下提供,附加条款亦可能应用