/**
 * Sliding window is an algorithm for solving extreme values in a fixed width
 * interval.
 */ export interface SlidingWindow { param WINDOW_SIZE init ( WINDOW_SIZE ? ) : void ; param idx push ( idx ) : void ; /**
     * Get the index of the maximum value in the sliding window.
     */ max ( ) : number ; } param WINDOW_SIZE param cmp export function createSlidingWindow ( _WINDOW_SIZE , cmp ) { let front = 0 ; let end = - 1 ; let WINDOW_SIZE = _WINDOW_SIZE ; const window = [ ] ; return { init , push , max } ; function init ( _WINDOW_SIZE ? ) { if ( _WINDOW_SIZE != null ) WINDOW_SIZE = _WINDOW_SIZE ; window . length = 0 ; front = 0 ; end = - 1 ; } function push ( idx ) { if ( front <= end && idx - window [ front ] >= WINDOW_SIZE ) front += 1 ; for ( ; front <= end ; end -= 1 ) { const delta = cmp ( window [ end ] , idx ) ; if ( delta > 0 ) break ; } window [ ++ end ] = idx ; } function max ( ) { return window [ front ] ; } }