Last active
August 29, 2015 14:00
-
-
Save kamiyaowl/11199262 to your computer and use it in GitHub Desktop.
scala greedy algorithm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.annotation.tailrec | |
object okane2 { | |
@tailrec | |
def greedy(current:Int, can:Seq[(Int,Int)])(count:Int) : Int = { | |
if(current == 0) count | |
else if(can.isEmpty) -1 | |
else { | |
val target = can.head | |
val canPayCount = current / target._1 | |
val payCount = if (canPayCount > target._2) target._2 else canPayCount | |
val pay = payCount * target._1 | |
greedy(current - pay, can.tail)(count + payCount) | |
} | |
} | |
def main(args:Array[String]) = { | |
val input1 = "0 3 0 8 3 4" | |
val input2 = "167" | |
val canUse = Seq(500,100,50,10,5,1) zip input1.split(' ').map(_.toInt) | |
val goal = input2.toInt | |
println(greedy(goal,canUse)(0)) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.Array.; | |
import scala.Predef.; | |
import scala.ScalaObject; | |
import scala.Serializable; | |
import scala.Tuple2; | |
import scala.collection.IterableLike; | |
import scala.collection.Seq; | |
import scala.collection.Seq.; | |
import scala.collection.TraversableLike; | |
import scala.collection.immutable.StringLike; | |
import scala.reflect.Manifest.; | |
import scala.runtime.AbstractFunction1; | |
import scala.runtime.BoxesRunTime; | |
public final class okane2$ | |
implements ScalaObject | |
{ | |
public static final MODULE$; | |
private okane2$() | |
{ | |
MODULE$ = this; | |
} | |
public int greedy(int current, Seq<Tuple2<Object, Object>> can, int count) | |
{ | |
for (;;) | |
{ | |
if (can.isEmpty()) { | |
return | |
current == 0 ? count : | |
-1; | |
} | |
Tuple2 target = (Tuple2)can.head(); | |
int canPayCount = current / target._1$mcI$sp(); | |
int payCount = canPayCount > target._2$mcI$sp() ? target._2$mcI$sp() : canPayCount; | |
int pay = payCount * target._1$mcI$sp(); | |
count += payCount;can = (Seq)can.tail();current -= pay; | |
} | |
} | |
public void main(String[] args) | |
{ | |
String input1 = "0 3 0 8 3 4"; | |
String input2 = "167"; | |
Seq canUse = (Seq)((IterableLike)Seq..MODULE$.apply(Predef..MODULE$.wrapIntArray(new int[] { 500, 100, 50, 10, 5, 1 }))).zip(Predef..MODULE$.wrapIntArray((int[])Predef..MODULE$.refArrayOps((Object[])Predef..MODULE$.augmentString(input1).split(' ')).map(new AbstractFunction1() | |
{ | |
public static final long serialVersionUID = 0L; | |
public final int apply(String paramAnonymousString) | |
{ | |
return Predef..MODULE$.augmentString(paramAnonymousString).toInt(); | |
} | |
}, Array..MODULE$.canBuildFrom(Manifest..MODULE$.Int()))), Seq..MODULE$.canBuildFrom()); | |
int goal = Predef..MODULE$.augmentString(input2).toInt(); | |
Predef..MODULE$.println(BoxesRunTime.boxToInteger(greedy(goal, canUse, 0))); | |
} | |
static | |
{ | |
new (); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment