Discretization
-10000, 0, 100000, 1, 2-10000 -> 0
0 -> 1
10000 -> 2Implementation
// Author: github.com/lzl124631x
// Time: O(NlogN)
// Space: O(N)
vector<int> discretize(vector<int> &input) {
set<int> s(input.begin(), input.end()); // In this way, we dedupe the data and sort them
unordered_map<int, int> m; // mapping from old number to new number
int id = 0;
for (int n : s) m[n] = id++;
vector<int> output(input.size());
for (int i = 0; i < input.size(); ++i) output[i] = m[input[i]];
return output;
}Problem
Last updated