簡體   English   中英

計算向量中的求逆數

[英]Counting the number of inversions in a vector

背景資料:

這是Google每天的編碼問題。

我們可以通過計算數組A的反轉次數來確定數組A的“混亂程度”。 如果A [i]> A [j]但i <j,則兩個元素A [i]和A [j]形成求逆。也就是說,較小的元素出現在較大的元素之后。

給定一個數組,計算它具有的反轉次數,比O(N ^ 2)時間要快。

您可以假設數組中的每個元素都是不同的。

例如,一個排序列表的反轉數為零。array [2,4,1,3,5]具有三個反轉數:(2,1),(4,1)和(4,3)。array [5 ,,4,3,2,1]有十個反轉:每個不同的對都形成一個反轉。

蠻力解決方案:

auto num_inversions(std::vector<int>& nums)
{
    int count = 0;
    for (int i = 0; i <= nums.size(); ++i)
    {
        for (int j = i + 1; j < nums.size(); ++j)
        {
            if (nums[i] > nums[j])
                ++count;
        }
    }
    return count;
}

更好的潛在解決方案:

我的想法是使用priority_queue來實現以下目標:

auto num_inversions1(std::vector<int>& nums)
{
    auto compare = [](int lhs, int rhs)
    {
        return lhs > rhs;
    };
    std::priority_queue<int, std::vector<int>, decltype(compare)> q(compare);
    for (auto num : nums)
        q.push(num);
    print_queue(q);
}

現在,如果我知道使用比較lambda的次數,那么我認為這將確定我的反轉次數。 是否可以計算在優先級隊列中使用lambda表達式的次數? 如果是這樣,這種方法行得通嗎?

更新:

按照提供的鏈接的建議,除了一個修改的mergesort之外,沒有過多地看答案,我嘗試了一下,但沒有得到正確的結果。

這是我的代碼:

#include <iostream>
#include <vector>

int merge(std::vector<int>& data, std::vector<int>& temp, int low, int middle, int high) {

    // create a temporary array ... O(N) memory complexity !!!      
    // copy the data to a temporary array 
    for (int i = low; i <= high; i++) {
        temp[i] = data[i];
    }

    int i = low;
    int j = middle + 1;
    int k = low;

    int inv_count = 0;
    // Copy the smallest values from either the left or the right side back
    // to the original array
    while ((i <= middle) && (j <= high)) {
        if (temp[i] <= temp[j]) {
            data[k] = temp[i];
            i++;
        }
        else {
            data[k] = temp[j];
            j++;
            inv_count = inv_count + (middle - i);
        }

        k++;
    }

    // Copy the rest of the left side of the array into the target array
    while (i <= middle) {
        data[k] = temp[i];
        k++;
        i++;
    }

    // Copy the rest of the right side of the array into the target array
    while (j <= high) {
        data[k] = temp[j];
        k++;
        j++;
    }
    return inv_count;
}

int merge_sort(std::vector<int>& data, std::vector<int>& temp, int low, int high)
{
    int mid, inv_count = 0;
    if (high > low)
    {

        mid = (low + high) / 2;
        inv_count = merge_sort(data, temp, low, mid);
        inv_count += merge_sort(temp, temp, mid + 1, high);
        inv_count += merge(data, temp, low, mid, high);
    }

    return inv_count;
}

int sort(std::vector<int>& data, std::vector<int>& temp)
{
    return merge_sort(data, temp, 0, data.size() - 1);
}

int main()
{
    std::vector<int> data = { 2, 4, 1, 3, 5 };
    auto n = data.size();
    std::vector<int> temp(n, 0);
    std::cout << "The number of inversions is " << sort(data, temp);

    std::cin.get();
}

答案應該是3,但我只有1

使用計數器:

auto num_inversions1(std::vector<int>& nums)
{
    int cmpCounter = 0;
    auto compare = [&cmpCounter](int lhs, int rhs)
    {
        cmpCounter++;
        return lhs > rhs;
    };
    std::priority_queue<int, std::vector<int>, decltype(compare)> q(compare);
    for (auto num : nums)
        q.push(num);
    print_queue(q);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM