Find the min number in all contiguous subarrays of size L of a array of size n

前端 未结 3 434
栀梦
栀梦 2020-12-22 14:19

The minimum number in a subarray of size L. I have to find it for all the subarrays of the array. Is there any other way than scanning through all the subarrays individually

3条回答
  •  清歌不尽
    2020-12-22 14:28

    Scala version answer

    def movingMin(windowSize: Int, array: Seq[Double]) = { 
        (1 to array.length - (windowSize - 1)).
            map{i => (array.take(i + (windowSize - 1)).takeRight(windowSize)).min}
    }
    

提交回复
热议问题