简体   繁体   English

整数流中的最大值和最小值

[英]Max & Min in stream of integers

Given a stream of integers (I can only go through them once), what is the best solution to find maximum and minimum? 给定整数流(我只能遍历一次),找到最大值和最小值的最佳解决方案是什么? I suppose that in case I have enough time to proccess each number the easiest solution is to simply keep min and max values in a separate variables, but what is the best approach if I'm unable to proccess every single one of them? 我想,如果我有足够的时间处理每个数字,最简单的解决方案是将最小值和最大值保留在单独的变量中,但是如果我无法处理每个数字,那么最好的方法是什么? Is there some better solution than simply keep max and min variables and skip for example every second number? 有没有比仅保留max和min变量并跳过例如第二个数字更好的解决方案?

If you want the true maximum and minimum, just track them with variables. 如果您想要真实的最大值和最小值,只需使用变量跟踪它们即可。

Depending on your input data, a probabilistic min/max may be "accurate enough". 根据您的输入数据,概率的最小值/最大值可能“足够准确”。 So if you only look at every number with 50% chance, then you probably have only a 50% of having the exact min or max. 因此,如果您仅以50%的机会查看每个数字,那么您可能只有50%的确切最小值或最大值。 But maybe already a 75% chance of having at least the second largest/smallest etc. However, computing the random number to do an unbiased sampling is already more expensive than looking at all numbers for min/max. 但是也许至少有第二大/最小的等概率已经有75%的可能性。但是,计算随机数进行无偏采样比查看所有数字的最小值/最大值已经更加昂贵。 Skipping every second is dangerous: there might be an even/odd pattern in the data that screws you badly. 每秒跳过是很危险的:数据中可能存在偶数/奇数模式,可能会严重损坏您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM