// Find stone pair(s)
// There are N number of stones, labeled as [0, N-1]. We know the weight of each of those stones. We want to find ONE stone pair, i.e. 2 stones,
// whose weight difference is D.
// Question A
// Formally describe the problem to clearly define the problem without ambiguity. Something like:
// write a function F that …
// the inputs of the function are …
// the output of the function are ….
// This question might be harder than you thought. Please think carefully.
// Question B
// Write the function you described in your answer to question A.
// What is the space complexity of your algorithm? What is the time complexity?
// Can you achieve O(N) for both time complexity and space complexity? If yes, please implement it.
// Write a test program to test your function.
// Please list all corner cases you want to test
// How to verify the function can process these corner cases correctly? Can you do it in a more systematic way?
// Question C (optional)
// Same as the original question B, but this time we want to find ALL stone pairs whose weight difference is D. Please note that pair (1, 4) and pair
// (4, 1) are considered as the same pair, so only need to return one. Your algorithm’s time complexity should be O(max(R, N)), R is the number of
// result pairs, N is the number of stones.
#include<cassert>
#include<utility>
#include<unordered_map>
#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;
// Question A
// Function
// findStonePairs : 通过哈希表实现
// Inputs
// stoneName : 石头的名字
// stoneWeights : 指向石头重量的指针
// N : 石头的数量
// weightDiff : 重量差
// stonePairs : 查找到的结果
// Outputs
// 查找到的结果对数
// Question B
int findStonePairs(const char * stoneName, double* const stoneWeights, int N, double weightDiff, vector<pair<int, int>>& stonePairs) {
assert(N > 0);
assert(weightDiff >= 0.0);
for(int i = 0; i < N; i++) {
assert(stoneWeights[i] > 0.0);
}
if(weightDiff == 0.0){
cout << stoneName << ": " << "This situation does not exist!" << endl;
return stonePairs.size();
}
unordered_map<double, vector<int>> stoneWeightsAddDiff(N);
for(int i = 0; i < N; i++) {
auto iter = stoneWeightsAddDiff.find(stoneWeights[i] + weightDiff);
if(iter != stoneWeightsAddDiff.end()){
iter->second.push_back(i);
} else{
stoneWeightsAddDiff[stoneWeights[i] + weightDiff] = {i};
}
// cout << stoneWeightsAddDiff.size() << " " <<
// (stoneWeightsAddDiff[stoneWeights[i] + weightDiff]).size() << endl;
}
for(int i = 0; i < N; i++) {
auto iter = stoneWeightsAddDiff.find(stoneWeights[i]);
if(iter != stoneWeightsAddDiff.end()) {
for(int j = 0; j < iter->second.size(); j++){
stonePairs.push_back(minmax(iter->second[j], i));
}
}
}
if(stonePairs.size() > 0) {
cout << stoneName << ": ";
for(auto pair : stonePairs) {
cout << pair.first << " " << pair.second << "; ";
}
cout << endl;
} else {
cout << stoneName << ": " << "Zero!" << endl;
}
return stonePairs.size();
}
// 奇数个石头,只有一对符合结果
void testOddStoneWeights1() {
constexpr int N = 7;
constexpr double diffWeight = 0.39;
double stoneWeights[N] = {1.23, 5.39, 5.0, 14560.465, 45613.78, 963.25, 8742.5};
vector<pair<int, int>> stonePairs;
int result = findStonePairs(__FUNCTION__, stoneWeights, N, diffWeight, stonePairs);
assert(result == 1);
}
// 奇数个石头,无符合结果的石头对
void testOddStoneWeights2() {
constexpr int N = 7;
constexpr double diffWeight = 1.0;
double stoneWeights[N] = {1.23, 5.39, 456.369, 14560.465, 45613.78, 963.25, 8742.5};
vector<pair<int, int>> stonePairs;
int result = findStonePairs(__FUNCTION__, stoneWeights, N, diffWeight, stonePairs);
assert(!result);
}
// 偶数个石头,只有一对符合结果
void testEvenStoneWeights1() {
constexpr int N = 6;
constexpr double diffWeight = 1.0;
double stoneWeights[N] = {1.23, 5.39, 456.369, 14560.465, 45613.78, 0.23};
vector<pair<int, int>> stonePairs;
int result = findStonePairs(__FUNCTION__, stoneWeights, N, diffWeight, stonePairs);
assert(result == 1);
}
// 偶数个石头,无符合结果的石头对
void testEvenStoneWeights2() {
constexpr int N = 6;
constexpr double diffWeight = 9.9;
double stoneWeights[N] = {1.23, 5.39, 456.369, 14560.465, 45613.78, 0.23};
vector<pair<int, int>> stonePairs;
int result = findStonePairs(__FUNCTION__, stoneWeights, N, diffWeight, stonePairs);
assert(!result);
}
// 石头对质量差为零
void testWeightDiffZero() {
constexpr int N = 7;
constexpr double diffWeight = 0.0;
double stoneWeights[N] = {1.23, 5.39, 81742.5, 14560.465, 45613.78, 0.23, 8742.5};
vector<pair<int, int>> stonePairs;
int result = findStonePairs(__FUNCTION__, stoneWeights, N, diffWeight, stonePairs);
assert(!result);
}
// 一个石头对应两个质量不同的结果
void testTwoPairsFound1() {
constexpr int N = 6;
constexpr double diffWeight = 1.0;
double stoneWeights[N] = {1.23, 5.39, 456.369, 2.23, 45613.78, 0.23};
vector<pair<int, int>> stonePairs;
int result = findStonePairs(__FUNCTION__, stoneWeights, N, diffWeight, stonePairs);
assert(result == 2);
}
// 一个石头对应两个质量相同的结果
void testTwoPairsFound2() {
constexpr int N = 6;
constexpr double diffWeight = 1.0;
double stoneWeights[N] = {1.23, 5.39, 456.369, 45614.78, 45613.78, 0.23};
vector<pair<int, int>> stonePairs;
int result = findStonePairs(__FUNCTION__, stoneWeights, N, diffWeight, stonePairs);
assert(result == 2);
}
// 一个石头对应多个质量相同的结果
void testMultiplePairsFound3() {
constexpr int N = 6;
constexpr double diffWeight = 1.0;
double stoneWeights[N] = {1.23, 0.23, 0.23, 45614.78, 45613.78, 0.23};
vector<pair<int, int>> stonePairs;
int result = findStonePairs(__FUNCTION__, stoneWeights, N, diffWeight, stonePairs);
assert(result == 4);
}
int main() {
testOddStoneWeights1();
testOddStoneWeights2();
testEvenStoneWeights1();
testEvenStoneWeights2();
testWeightDiffZero();
testTwoPairsFound1();
testTwoPairsFound2();
testMultiplePairsFound3();
return 0;
}
03-13
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交