Last active
February 23, 2023 06:16
-
-
Save kargozeyan/9e3c1e06b91b14ebb70959b95818cfba to your computer and use it in GitHub Desktop.
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 characters
import kotlinx.coroutines.launch | |
import kotlinx.coroutines.runBlocking | |
import java.util.* | |
fun main() { | |
val a = Stack<Int>().also { | |
it.push(9) | |
it.push(8) | |
it.push(7) | |
it.push(6) | |
it.push(5) | |
it.push(4) | |
it.push(3) | |
it.push(2) | |
it.push(1) | |
} | |
val b = Stack<Int>() | |
val c = Stack<Int>() | |
runBlocking { | |
launch { | |
solve(a, b, c, a.size) | |
println(a) | |
println(b) | |
println(c) | |
} | |
} | |
} | |
fun solve(from: Stack<Int>, into: Stack<Int>, temp: Stack<Int>, n: Int) { | |
if (n == 0) { | |
return | |
} | |
if (n == 1) { | |
into.push(from.pop()) | |
return | |
} | |
solve(from, temp, into, n - 1) // solving for n - 1, but into temporary stack | |
into.push(from.pop()) | |
solve( | |
temp, | |
into, | |
from, | |
n - 1 | |
) // solving for n - 1, but the temporary stack (the which is the solution of line 40) into 'into' as 'into' now is the 'temporary' stack | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment