Queries with Fixed Length

  • + 0 comments
    def solve(arr: Array[Int], queries: Array[Int]): Array[Int] = {
        
        val dq = scala.collection.mutable.ArrayDeque.empty[Int]
        
        queries.map{ q => {
          var minOfMax = Int.MaxValue 
          dq.clear()
          
          arr.indices.foreach{ i => {
            
            while (dq.nonEmpty && i >= q && dq.head <= i-q ) 
              dq.removeHead()
            
            while (dq.nonEmpty && arr(dq.last) <= arr(i)) 
              dq.removeLast()
            
            dq.append(i)
    
            if (i >= q-1) 
              minOfMax = arr(dq.head).min(minOfMax)
            
          }}
          
          minOfMax
        }}