Find stone pair(s)

//  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;
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值