java - 从列表中获取最小和最大数字

标签 java algorithm sorting data-structures

假设我们有一个包含整数的列表,我们需要找到最小值和最大值。最好的方法是什么?我可以想到以下内容:

  1. If number of reads are much higher than writes; keep the list in sorted manner. Whenever a number is added; add it in sorted manner. Now to get min and max we can just get first and last element of this list.

  2. If number of writes are higher than reads; iterate on the list and return result. But this is O(n) which looks to be expensive.

还有其他更好的方法吗?

最佳答案

假设您可以观察到列表的任何变化,您可以存储(缓存)最小值和最大值,并可能在添加/删除数字时更新缓存值。此时,列表的顺序无关紧要。

关于java - 从列表中获取最小和最大数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14726565/

相关文章:

java - Groovy 中的泛型

java - 比较数组中的大整数

java - 我如何按降序完成排序?

python - 在python中按行对二维数组进行排序

python - 根据 Python 中的键获取排序列表的一部分

java - ifPresent Stream 的 Else 方法

java - 在自定义 JPanel 和 JTable 布局之间进行选择

algorithm - 用于内核典型相关分析的 KMBOX

python - k-means 聚类中 holdout 集的目的是什么?

c - 这种字符串匹配的最佳方式是什么?