You are viewing a single comment's thread. Return to all comments →
My C++ solution from 6 years ago. Gets 100%:
long getMax(long a, long b) { if (a>b) return a; return b; }
// Complete the countTriplets function below. long countTriplets(vector arr, long r) { typedef std::unordered_map HashNumberCounts;
HashNumberCounts hash_numbers1; HashNumberCounts hash_numbers2; HashNumberCounts hash_numbers3; long counts = 0; int n = (int) arr.size(); for( int i = 0; i < n; ++i ) { long long next_number = (long long)arr[ i ] * (long long)r; // treat as endpoint auto it3 = hash_numbers3.find( arr[ i ] ); if ( it3 != hash_numbers3.end() ) counts += it3->second; // treat as middle auto it2 = hash_numbers2.find( arr[ i ] ); if ( it2 != hash_numbers2.end() && next_number <= INT_MAX ) hash_numbers3[ next_number ] += it2->second; // treat as first hash_numbers1[ arr[ i ] ]++; if ( next_number <= INT_MAX ) hash_numbers2[ next_number ] = hash_numbers1[ arr[ i ] ]; } return counts;
}
Seems like cookies are disabled on this browser, please enable them to open this website
Count Triplets
You are viewing a single comment's thread. Return to all comments →
My C++ solution from 6 years ago. Gets 100%:
long getMax(long a, long b) { if (a>b) return a; return b; }
// Complete the countTriplets function below. long countTriplets(vector arr, long r) { typedef std::unordered_map HashNumberCounts;
}