Last active
March 21, 2021 07:02
-
-
Save chenharryhua/22b893179d7b7d4a5e28f2a15aea0d8b to your computer and use it in GitHub Desktop.
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
// https://www.hackerrank.com/challenges/insertion-sort/problem | |
def insertionSort(arr: Array[Int]): Int = | |
arr | |
.foldLeft((TreeMap.empty[Int, Int], 0L)) { case ((tm, s), (v)) => | |
val d = tm.rangeFrom(v + 1).values.sum | |
val up = tm.updatedWith(v) { | |
case Some(x) => Some(x + 1) | |
case None => Some(1) | |
} | |
(up, s + d) | |
} | |
._2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
merge sort won