【问题标题】:Scala data structure and complexity O(1)Scala 数据结构和复杂度 O(1)
【发布时间】:2015-07-23 15:39:26
【问题描述】:

今天我接受了关于 Scala 的采访,要求是:

使用以下功能实现具有固定大小 N 的数据结构:

  • 获取(索引)
  • 设置(索引,值)
  • 设置(值)

复杂度应该是 O(1)

example:
   val ds = DS(100)
   ds.set(4,5)
   ds.get(4) //would return  5
   ds.set(1,4)
   ds.setall(10)
   ds.get(4) //would return  10
   ds.get(7) //would return  10
   ds.set(1,7)
   ds.get(1) //would return  7

请在下面找到我发送的代码。
我的问题是这是正确的解决方案吗?是否有更好的方法?

import scala.collection.mutable.HashMap

trait Operations {

  def get(index: Int): Int
  def set(index: Int, value: Int)
  def setall(value: Int)
}

class DS(N: Int) extends Operations {

  var myMutableHashMap = new HashMap[Int, Int]().withDefaultValue(0)

  def get(index: Int) = myMutableHashMap(index)

  def set(index: Int, value: Int) = {
    if (index <= N) myMutableHashMap += (index -> value)
  }

  def setall(value: Int) = {

    myMutableHashMap = new HashMap[Int, Int]().withDefaultValue(value)
  }

}

object Task {

  def main(args: Array[String]): Unit = {

    val ds = new DS(100)

    ds.set(4, 5)
    ds.get(4)

    println(ds.get(4)) // -> 5

    ds.setall(10)
    println(ds.get(4)) //-> 10
    println(ds.get(7)) //-> 10
    ds.set(1, 7)
    println(ds.get(1)) //-> 7

  }

}

【问题讨论】:

    标签: scala data-structures


    【解决方案1】:

    我不确定它是否更好,但我认为 HashMap 可能有点矫枉过正。 以下解决方案可能占用空间更小,代码更少。 虽然,我可能宁愿实现更通用的东西,但它应该满足你提到的要求。

    trait Operations {
      def get(index: Int): Int
      def set(index: Int, value: Int): Unit
      def setall(fill: Int): Unit
    }
    
    class DS(size: Int) extends Operations {
      val underlying: Array[Int] = new Array(size)
    
      def get(index: Int): Int = underlying(index)
    
      def set(index: Int, value: Int): Unit = underlying(index) = value
    
      def setall(fill: Int): Unit = (0 until size).foreach(underlying(_) = fill)
    }
    

    替代方案,这可能会给我们带来更好的“setall”复杂性,但要付出代价......

    trait Operations {
      def get(index: Int): Int
      def set(index: Int, value: Int): Unit
      def setall(fill: Int): Unit
    }
    
    class DS(size: Int) extends Operations {
      var underlying: Array[Integer] = new Array(size)
      var default: Integer = new Integer(0)
    
      def get(index: Int): Int = {
        val result = underlying(index)
        if (result == null) default else result
      }
    
      def set(index: Int, value: Int): Unit = underlying(index) = value
    
      def setall(fill: Int): Unit = {
        default = fill
        underlying = new Array(size)
      }
    }
    

    【讨论】:

    • 不幸的是,您的 setsetAll 不是恒定时间复杂度。它们都是线性的。我不太确定get 方法的想法
    • @eliasah。 set 在我看来完全是 O(1)(数组更新)。但我同意你setAll
    • 确实如此!我对它需要数组tail这一事实感到困惑
    • 要使所有内容都为 O(1),请将 underlying 设置为 Array[Option[Int]],其中 None 表示该索引尚未明确设置。然后让setAll 设置用于None 值的默认值。
    • 想过这个问题,但我不是还得用None 填充初始数组吗?否则,所有元素都以null 开头,这很快就会变得非常难看。但是,我将添加一个替代方案,它使用 `Array[_ <: anyref>null 条目。不过,我不太喜欢那样。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-25
    • 1970-01-01
    • 2012-05-13
    相关资源
    最近更新 更多