gpt4 book ai didi

c++ - 在 1.5 秒内找到超过 2000 万个 3 到 4 个不同整数的中位数

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:06:31 26 4
gpt4 key购买 nike

我正在尝试对仅包含 3 到 4 个不同整数的整数字符串进行排序并找到中位数。

我正在处理的数字数量大约为 20 到 2500 万,我应该对 vector 进行排序并在每次将新整数添加到 vector 中时找到中位数并将中位数添加到单独的“Total”变量,每次生成一个中位数时将所有中位数相加。

1                   Median: 1              Total: 1
1 , 2 Median: (1+2)/2 = 1 Total: 1 + 1 = 2
1 , 2 , 3 Median: 2 Total: 2 + 2 = 4
1 , 1 , 2 , 3 Median: (1+2)/2 = 1 Total: 4 + 1 = 5
1 , 1 , 1 , 2 , 3 Median: 1 Total: 5 + 1 = 6

我正在尝试找到一种方法来进一步优化我的代码,因为它不够高效。 (必须在 2 秒左右的时间内处理)有人知道如何进一步加快我的代码逻辑吗?

我目前在 C++ 中使用 2 个堆或优先级队列。一个用作最大堆,另一个用作最小堆。

灵感来自 Data structure to find median

You can use 2 heaps, that we will call Left and Right.
Left is a Max-Heap.
Right is a Min-Heap.
Insertion is done like this:

If the new element x is smaller than the root of Left then we insert x to
Left.
Else we insert x to Right.
If after insertion Left has count of elements that is greater than 1 from
the count of elements of Right, then we call Extract-Max on Left and insert
it to Right.
Else if after insertion Right has count of elements that is greater than the
count of elements of Left, then we call Extract-Min on Right and insert it
to Left.
The median is always the root of Left.

So insertion is done in O(lg n) time and getting the median is done in O(1)
time.

但是还是不够快...

最佳答案

如果字符串中只有三到四个不同的整数,您可以通过遍历字符串一次来跟踪每个整数出现的次数。在此表示中添加(和删除元素)也可以在常数时间内完成。

class MedianFinder
{
public:
MedianFinder(const std::vector<int>& inputString)
{
for (int element : inputString)
_counts[element]++; // Inserts 0 into map if element is not in there.
}

void addStringEntry(int entry)
{
_counts[entry]++;
}

int getMedian() const
{
size_t numberOfElements = 0;
for (auto kvp : _counts)
numberOfElements += kvp.second;

size_t cumulativeCount = 0;
int lastValueBeforeMedian;
for (auto kvp : _counts)
{
cumulativeCount += kvp.second;
if (cumulativeCount >= numberOfElements/2)
lastValueBeforeMedian = kvp.first;
}

// TODO! Handle the case of the median being in between two buckets.
//return ...
}

private:
std::map<int, size_t> _counts;
};

此处未显示对中位数求和的琐碎任务。

关于c++ - 在 1.5 秒内找到超过 2000 万个 3 到 4 个不同整数的中位数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52062204/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com