Hackerland Radio Transmitters

  • + 0 comments
    Single Pass One Line 
    
    def hackerlandRadioTransmitters(x: Array[Int], k: Int): Int = 
        x.foldLeft((-1,-1,-1)) {
          case ((-1,-1,-1),_) => 
            x.sortInPlace
            (1,x(0),x(0)+k)
          case ((i,l,r),house) if house - k <= l => 
              (i,l,house+k)           
          case ((i,l,r),house) if house > r => 
              (i+1,house,house+k)
          case acc => acc._1
          }._1