2 ms·
Also available in python: import heapq n = 2 heapq.nlargest(n, vals)[n-1] Edit: and out of morbid curiosity, here is the same implemented by a scan. It
by notaddicted 14y ago
Also available in python:
import heapq
n = 2
heapq.nlargest(n, vals)[n-1]
Edit: and out of morbid curiosity, here is the same implemented by a scan. It could be arranged as a single statement.
swapIfGt = lambda xs, q: xs if xs[0] >= q else sorted([q]+xs[1:])
n = 2
reduce(swapIfGt, vals, [None]*n)[0]
Edit2: rewrote swapIfGt