# 在查找集合中的最小值/最大值時使用類型的最小值/最大值

int min = someArray[0][0];
for (int i = 0; i < someArray.length; i++)
for (int j = 0; j < someArray[i].length; j++)
min = Math.min(min, someArray[i][j]);


-要麽-

B）

int min = int.MAX_VALUE;
for (int i = 0; i < someArray.length; i++)
for (int j = 0; j < someArray[i].length; j++)
min = Math.min(min, someArray[i][j]);


As an algorithm, which is better/valid-er.

EDIT2：修正了一些粗心的錯誤。

## 最佳答案

1. max是冪等，因此將其應用於相同的值兩次會返回原始值：max（x，x）= x
2. max是可交換的，因此將它應用於其參數的順序無關緊要：max（x，y）= max（y，x）
3. max是 associative ，因此當獲取最多三個或更多值時，如何對元素進行分組並不重要：max（max（x，y），z）= max（x ，max（y，z））

Element e = data[0];
for (i in data[1 .. n])
e = meet(e, data[i])


min(int.MAX_VALUE, x) = min(x, int.MAX_VALUE) = x


In other words, if you compute the meet of int.MAX_VALUE and any other value, you get the second value back. In mathematical terms, this is because int.MAX_VALUE is the top element of the meet semilattice. More formally, a top element for a meet semilattice is an element (denoted &top;) satisfying

meet(&top;, x) = meet(x, &top;) = x

max(int.MIN_VALUE, x) = max(x, int.MIN_VALUE) = x


Because applying the meet operator to &top; and any other element produces that other element, if you have a meet semilattice with a well-defined top element, you can rewrite the above code to compute the meet of all the elements as

Element e = Element.TOP;
for (i in data[0 .. n])
e = meet(e, data[i])


也就是說，並非所有半格都有頂部元素。例如，考慮將滿足運算符定義為的所有字符串的集合

meet(x, y) = x if x lexicographically precedes y
= y otherwise


However, there is a very cute technique you can use to fake this, which actually does end up getting used a bit in practice. Given a semilattice with no top element, you can create a new semilattice that does have a top element by introducing a new element &top; and arbitrarily defining that meet(&top;, x) = meet(x, &top;) = x. In other words, this element is specially crafted to be a top element and has no significance otherwise.

bool found = false;
Element e;

for (i in data[0 .. n]) {
if (!found) {
found = true;
e = i;
} else {
e = meet(e, i);
}
}