Skip to content

Instantly share code, notes, and snippets.

@ShahOdin
Last active July 3, 2022 01:10
Show Gist options
  • Select an option

  • Save ShahOdin/d3c7873556b18944eda18be594f8eb49 to your computer and use it in GitHub Desktop.

Select an option

Save ShahOdin/d3c7873556b18944eda18be594f8eb49 to your computer and use it in GitHub Desktop.

Revisions

  1. ShahOdin revised this gist Jul 3, 2022. 1 changed file with 20 additions and 23 deletions.
    43 changes: 20 additions & 23 deletions MaxCounters.scala
    Original 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: mutable.Map[State.ArrayIndex, Int], var max: Int, var min: Int) {

    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): Unit = {
    def incrementByOne(arrayIndex: State.ArrayIndex): State = {
    val newValue = get(arrayIndex) + 1
    if(newValue > max){
    max = newValue
    }
    mapping.update(arrayIndex, newValue)
    val newMax = if(newValue > max){newValue} else max
    copy(mapping = mapping.updated(arrayIndex, newValue), max = newMax)
    }

    def setAllToMax: Unit = {
    min = max
    }
    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(mutable.HashMap.empty, max = 0, min = 0)

    def empty: State = State(HashMap.empty, max = 0, min = 0)
    }

    def solution(n: Int, a: Array[Int]): Array[Int] = {
    val state = State.empty
    a.foreach{ i =>
    val state = a.toList.foldLeft(State.empty){ case (s, i) =>
    Input.fromInt(i, n) match {
    case Input.IncrementCounter(humanIndex) => state.incrementByOne(State.ArrayIndex.fromHumanIndex(humanIndex))
    case Input.MaxCounter => state.setAllToMax
    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(", ", ", ")")
    // )
    }
    println(
    solution(
    5,
    Array(3, 4, 4, 6, 1, 4, 4)
    ).mkString("Array(", ", ", ")")
    )
    }
  2. ShahOdin created this gist Jul 3, 2022.
    62 changes: 62 additions & 0 deletions MaxCounters.scala
    Original 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(", ", ", ")")
    // )
    }