Skip to content

Instantly share code, notes, and snippets.

@sheki
Created April 8, 2011 15:59
Show Gist options
  • Save sheki/910167 to your computer and use it in GitHub Desktop.
Save sheki/910167 to your computer and use it in GitHub Desktop.
Attempted solution to goo.gl/NyPoz [Google code jam problem] in Scala.
import scala.util.Sorting.quickSort
class StoreItem(val price: Int, val index: Int) extends Ordered[StoreItem]
{
override def compare (that : StoreItem ) : Int = {
if (this.price equals that.price)
return this.index-that.index
else
this.price -that.price
}
override def toString = "val :"+price+" index: "+index
}
object StoreCredit {
def main (args : Array[String] ) = {
val noTestCases= Console.readInt
for ( i <- 1 until noTestCases +1) {
val credit = Console.readInt
val noItems = Console.readInt
val listItems =Console.readLine.split(' ').view.zipWithIndex map {e =>new StoreItem(e._1.toInt ,e._2)}
val sortedItems=quickSort [StoreItem](listItems.toArray)
//Need to iterate sortedItems binarysearch for (c - _) and return the first find with index of _ and the corresponding element,
}
}
def binarySearch[A <% Ordered[A]](a: Array[A], v: A) = {
def recurse(low: Int, high: Int): Option[Int] = (low + high) / 2 match {
case _ if high < low => None
case mid if a(mid) > v => recurse(low, mid - 1)
case mid if a(mid) < v => recurse(mid + 1, high)
case mid => Some(mid)
}
recurse(0, a.size - 1)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment