我有一个多维数组:
val M = Array.ofDim[Int](V, N)
目标是找到存在有界元素 0 < w0 <= W 的最大 V 维索引,并返回索引和元素值。
目前我有这个有效的代码片段,但想知道是否有更好、更有效的方法来做到这一点。
M.zipWithIndex.reverse.collectFirst({
case (arr, ind) if arr.exists(a => a <= W && a > 0) => {
arr.zipWithIndex.find(a => a._1 <= W && a._1 > 0) match {
case Some((weight, ind2)) => (ind, ind2, weight)
}
}
})