Last active
July 3, 2022 01:10
-
-
Save ShahOdin/d3c7873556b18944eda18be594f8eb49 to your computer and use it in GitHub Desktop.
Revisions
-
ShahOdin revised this gist
Jul 3, 2022 . 1 changed file with 20 additions and 23 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,5 @@ import scala.collection.immutable.HashMap object Demo extends App { sealed trait Input @@ -11,37 +13,32 @@ object Demo extends App { MaxCounter } } final case class State(mapping: Map[State.ArrayIndex, Int], max: Int, min: Int) { def get(arrayIndex: State.ArrayIndex): Int = mapping.get(arrayIndex).filter(_ > min).getOrElse(min) def incrementByOne(arrayIndex: State.ArrayIndex): State = { val newValue = get(arrayIndex) + 1 val newMax = if(newValue > max){newValue} else max copy(mapping = mapping.updated(arrayIndex, newValue), max = newMax) } def setAllToMax: State = copy(min = max) } object State{ final case class ArrayIndex(value: Int) extends AnyVal object ArrayIndex{ def fromHumanIndex(i: Int): ArrayIndex = ArrayIndex(i - 1) } def empty: State = State(HashMap.empty, max = 0, min = 0) } def solution(n: Int, a: Array[Int]): Array[Int] = { val state = a.toList.foldLeft(State.empty){ case (s, i) => Input.fromInt(i, n) match { case Input.IncrementCounter(humanIndex) => s.incrementByOne(State.ArrayIndex.fromHumanIndex(humanIndex)) case Input.MaxCounter => s.setAllToMax } } @@ -53,10 +50,10 @@ object Demo extends App { response } println( solution( 5, Array(3, 4, 4, 6, 1, 4, 4) ).mkString("Array(", ", ", ")") ) } -
ShahOdin created this gist
Jul 3, 2022 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,62 @@ object Demo extends App { sealed trait Input object Input{ final case class IncrementCounter(humanIndex: Int) extends Input case object MaxCounter extends Input def fromInt(n: Int, arraySize: Int): Input = if (n <= arraySize){ IncrementCounter(n) } else { MaxCounter } } final case class State(mapping: mutable.Map[State.ArrayIndex, Int], var max: Int, var min: Int) { def get(arrayIndex: State.ArrayIndex): Int = mapping.get(arrayIndex).filter(_ > min).getOrElse(min) def incrementByOne(arrayIndex: State.ArrayIndex): Unit = { val newValue = get(arrayIndex) + 1 if(newValue > max){ max = newValue } mapping.update(arrayIndex, newValue) } def setAllToMax: Unit = { min = max } } object State{ final case class ArrayIndex(value: Int) extends AnyVal object ArrayIndex{ def fromHumanIndex(i: Int): ArrayIndex = ArrayIndex(i - 1) } def empty: State = State(mutable.HashMap.empty, max = 0, min = 0) } def solution(n: Int, a: Array[Int]): Array[Int] = { val state = State.empty a.foreach{ i => Input.fromInt(i, n) match { case Input.IncrementCounter(humanIndex) => state.incrementByOne(State.ArrayIndex.fromHumanIndex(humanIndex)) case Input.MaxCounter => state.setAllToMax } } val response = new Array[Int](n) Range(0, n).foreach( i => response(i) = state.get(State.ArrayIndex(i)) ) response } // println( // solution( // 5, // Array(3, 4, 4, 6, 1, 4, 4) // ).mkString("Array(", ", ", ")") // ) }