"High waterline" algorithm: After the flood, when the last wave recedes, the high waterline will mark the highest water level ever seen.
Let's look at the application of the "high water mark" algorithm in Perl.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
The first line performs a shift operation on the array @_, puts an element 2 into the maximum value $max_so_far variable, and the remaining elements in @_ are (3,8,5,10), and then uses the foreach loop to traverse the array, the new The first element 3 in the array is greater than 2, and it is moved to the $max_so_far variable, and so on, and the last 10 is the largest element in the array.