检索界元素的最大指数在斯卡拉多维数组 [英] Retrieving largest indices of the bounded element in a multidimensional array in Scala

查看:96
本文介绍了检索界元素的最大指数在斯卡拉多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多维数组:

  VAL M = Array.ofDim [INT](V,N)

目标是要找到其中存在一个有限元素0℃最大V维指数; W0< = W和返回两个指数和元素值

目前我有这个code段这工作,但不知道是否有这样做一个更好的,更有效的方式。

  M.zipWithIndex.reverse.collectFirst({
  情况下(ARR,IND)如果arr.exists(一个=>一种与所述; = W放大器;&放大器;一个大于0)=> {
    arr.zipWithIndex.find(A => a._1< = W放大器;&安培; a._1大于0){匹配
      有些情况下((重量,IND2))=> (IND,IND2,体重)
    }
  }
})


解决方案

您真的更好这里的当务之急或递归解​​决方案。我会写一个递归的一种:

 高清取景器(ARR:数组[数组[INT]] W:诠释,我:= 0,J:= 0):选项[(智力,INT,INT )] = {
  VAL里= arr.length-I-1
  如果(I> = arr.length)无
  否则,如果(ARR(RI)(J)大于0和放大器;&安培; ARR(RI)(J)LT; W)部分(RI,J,编曲(RI)(J))
  否则,如果第(j + 1所述;改编(ⅰ)。长度)取景器(ARR,W,I,J + 1)
  否则取景器(ARR,W,I + 1)
}

然后取景器(M,W)应该做你想要什么。请注意,这也是有效的 - 没有拳击除了返回值


编辑 - 如果你关心的表现,这里有一个100×100阵列有没有解决办法或方式77%的一个解决方案,以结束对现有解决方案的运行时间(即运行时间应约1/4)

 原始而未答:65微秒/迭代
原始与1/4答案:18微秒/迭代

相比于原来的方法

结果表,采取相对时间(较低的也比较快,与-optimise编译但很难有差别):

 无应答1/4回答
原来1.00 1.00
雷克斯0.55 0.72
4E6 1.95 7.00
missingfaktor 2.41 1.91
路易吉4.93 3.92

所以,你原来的方法实际上比大家的建议更快,保存为递归之一。

I have an multidimensional Array:

val M = Array.ofDim[Int](V, N)

Goal is to find largest V dimension index for which there exists a bounded element 0 < w0 <= W and return both indices and element value.

Currently I have this code snippet which works, but wondering if there is a nicer, more efficient way to do this.

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)
    }
  }
})

解决方案

You're really better off with an imperative or recursive solution here. I'll write a recursive one:

def finder(arr: Array[Array[Int]], w: Int, i: Int = 0, j: Int = 0): Option[(Int,Int,Int)] = {
  val ri = arr.length-i-1
  if (i >= arr.length) None
  else if (arr(ri)(j) > 0 && arr(ri)(j) < w) Some(ri,j,arr(ri)(j))
  else if (j+1 < arr(i).length) finder(arr,w,i,j+1)
  else finder(arr,w,i+1)
}

Then finder(M,W) should do what you want. Note that this is also efficient--no boxing aside from the return value.


Edit--if you care about performance, here are the runtimes of the existing solutions on a 100x100 array which has no solutions or one solution at 77% of the way to the end (i.e. runtime should be about 1/4):

Original without answer:     65 μs / iteration
Original with answer at 1/4: 18 μs / iteration

Result table compared to original method, relative time taken (lower is faster, compiled with -optimise but that hardly makes a difference):

                  No Answer    1/4 Answer
Original            1.00          1.00
Rex                 0.55          0.72
4e6                 1.95          7.00
missingfaktor       2.41          1.91
Luigi               4.93          3.92

So your original method was actually faster than all of the suggestions, save for the recursive one.

这篇关于检索界元素的最大指数在斯卡拉多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆