Construct four auxiliary vectors of size n and initialize them by - 1
leftMin = Array . from ( { length : n } , ( _ , i ) => - 1 ) ; rightMin = Array . from ( { length : n } , ( _ , i ) => - 1 ) ; leftMax = Array . from ( { length : n } , ( _ , i ) => - 1 ) ; rightMax = Array . from ( { length : n } , ( _ , i ) => - 1 ) ;