You are viewing a single comment's thread. Return to all comments →
it works! thanks for your insight :) below a scala implementation. just input toys weight array and done.
def totalCost(toys_list:List[Int]):Int={ def toBuyToys(arr:List[Int],ref:Int,acc:Int):Int= arr match{ case Nil => acc case x :: xs =>{ if(x>=ref && x<=(ref+4)) toBuyToys(xs,ref,acc) else toBuyToys(xs,x,acc+1) } } val toys_sorted = toys_list.sorted toBuyToys(toys_sorted,toys_sorted.head,1) } println(totalCost(toy_list))
Seems like cookies are disabled on this browser, please enable them to open this website
Priyanka and Toys
You are viewing a single comment's thread. Return to all comments →
it works! thanks for your insight :) below a scala implementation. just input toys weight array and done.