You are viewing a single comment's thread. Return to all comments →
Bucket. O(n) time and space:
int beautifulTriplets(int d, vector<int> arr) { // bucket int count(0); int n = arr.size(); int b_size = 20001; vector<int> bucket(b_size); for(int i(0);i<n;++i){ ++bucket[arr[i]]; } for(int i(0);i<n;++i){ int num_j = arr[i]+d; int num_k = arr[i] + 2 *d; if(num_j>=b_size || num_k >= b_size) continue; count += bucket[num_j] * bucket[num_k]; } return count; }
Seems like cookies are disabled on this browser, please enable them to open this website
Beautiful Triplets
You are viewing a single comment's thread. Return to all comments →
Bucket. O(n) time and space: