Created
July 16, 2019 15:33
-
-
Save cg4jins/3112d0b7a4626703a5160196a2f81f03 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
class Solution { | |
var printOrders = arrayListOf<Int>() | |
var waitings = arrayListOf<Pair<Int, Int>>() | |
fun solution(priorities: IntArray, location: Int): Int { | |
for ((i, v) in priorities.withIndex()) { | |
waitings.add(Pair(i, v)) | |
} | |
while (waitings.size > 0) { | |
reOrderAndMaxPop() | |
} | |
for ((i, v) in printOrders.withIndex()) { | |
if (v == location) { | |
return i + 1 | |
} | |
} | |
return 0 | |
} | |
private fun reOrderAndMaxPop() { | |
val max = waitings.stream().max { o1, o2 -> o1.second - o2.second }.get().second | |
var tmp = arrayListOf<Pair<Int, Int>>() | |
var ret = arrayListOf<Pair<Int, Int>>() | |
var flag = true | |
for (w in waitings) { | |
if (flag) { | |
if (w.second == max) { | |
flag = false | |
printOrders.add(w.first) | |
} else { | |
tmp.add(w) | |
} | |
} else { | |
ret.add(w) | |
} | |
} | |
ret.addAll(tmp) | |
waitings = ret | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment