将 ICollection 成员与其自身进行比较
有没有最便宜的方法来将 ICollection 与其自身进行比较。
这是我的代码:
public IEnumerable<Pet> speciesChecker()
{
foreach (Pet pet in _pets)
{
bool wantedSpecies = true;
foreach (Pet pet2 in _pets)
{
if (pet2 != pet && pet.Species == pet2.Species)
{
wantedSpecies = false;
break;
}
}
if (wantedSpecies) yield return pet;
}
}
我的代码的时间复杂度是多少,我所知道的是它小于 O(N^2) 并且如果我从内部 foreach 循环中删除“break”,时间复杂度将为 O (N^2)。如果我错了,请纠正我。
Is there any cheapest way to compare an ICollection with itself.
Here is my code:
public IEnumerable<Pet> speciesChecker()
{
foreach (Pet pet in _pets)
{
bool wantedSpecies = true;
foreach (Pet pet2 in _pets)
{
if (pet2 != pet && pet.Species == pet2.Species)
{
wantedSpecies = false;
break;
}
}
if (wantedSpecies) yield return pet;
}
}
What is the time complexity of my code, all I know is this that it is less than O(N^2) and if I'll remove 'break' from inner foreach loop, the time complexity will be O(N^2). Please correct me if I am wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是我的看法:
GroupBy
将在内部使用 HashSet,因此 O(N)ElementAtOrDefault(1)
只需将枚举器移动一步,因此不会为 O(n)Here is my take on it:
GroupBy
will use HashSet internally so O(N)ElementAtOrDefault(1)
will only need to move the enumerator one step so will not be O(n)我认为这段代码做了同样的事情。在这种情况下,这是一个 O(N) 算法。诀窍是将宠物存储在按物种索引的字典中。
I think that this code does the same thing. In that case, this is an O(N) algorithm. The trick is to store the pets in a dictionary indexed by species.
您还可以使用类似以下内容,也应该是 O(N):
额外的
Select (g => new List(g))
may是多余的,但我相信这将有助于避免再次迭代整个分组逻辑,我相信这会导致 O(N^2) 。编辑: Magnus 对 List 构造函数在 O(n) 中运行的良好评论违背了目的...
怎么样:
我认为这是你能得到的最优化的,并且将在 O(n) 内工作。我们也避免使用
IEnumerable.Any ()
或IEnumerable.Count ()
扩展方法。想法?
You can also use something like the following, which should also be O(N):
The extra
Select (g => new List<Pet> (g))
may be superfluous, but I believe that will help avoid iterating the whole grouping logic a second time, which I believe would result in O(N^2) .Edit: Good comment from Magnus about the List constructor operating in O(n) defeating the purpose...
How about:
I think this is as optimized as you can get, and will work in O(n). We avoid using the
IEnumerable<T>.Any ()
orIEnumerable<T>.Count ()
extension method as well.Thoughts?
让
n
是_pets集合
的长度,需要带break的步骤数:
有两个简单的规则如何从wiki计算O:
如果f(x)是几项之和,其中最大的一项
保留增长率,忽略所有其他项。
如果 f(x) 是多个因子的乘积,则任何常数(中的项)
不依赖于 x) 的乘积被忽略。
不间断所需步骤数:
let
n
is the length of_pets collection
number of required steps with break:
There are two simple rules how to calculate O from wiki:
If f(x) is a sum of several terms, the one with the largest
growth rate is kept, and all others omitted.
If f(x) is a product of several factors, any constants (terms in
the product that do not depend on x) are omitted.
number of required steps without break: