SW Test/Programmers
[Programmers] Lv2. νλ ¬ ν λ리 νμ νκΈ°(kotlin)
An effort will never betray π
2022. 9. 9. 11:06
λ°μν
λ¬Έμ
μμ
νμ΄
- νλ ¬ νμ λ‘μ§μ μΈλ±μ€ νλμ© μ΄λνλλ‘ κ΅¬ννλ©΄ λ©λλ€.
- queiesλ 1λΆν° μμνλ -1μ ν΄μ£Όμλ©΄ λ©λλ€.
import kotlin.math.*
class Solution {
fun solution(rows: Int, columns: Int, queries: Array<IntArray>): IntArray {
var answer = mutableListOf<Int>()
val arr = Array(rows){ i ->
IntArray(columns){ j -> i*columns + j +1}
}
queries.forEach{
val fy = it[0] -1
val fx = it[1] -1
val sy = it[2] -1
val sx = it[3] -1
val last = arr[fy][fx]
var num = last
(fy..sy-1).forEach{
arr[it][fx] = arr[it+1][fx]
num = min(arr[it][fx], num)
}
(fx..sx-1).forEach{
arr[sy][it] = arr[sy][it+1]
num = min(arr[sy][it], num)
}
(sy downTo fy+1).forEach{
arr[it][sx] = arr[it-1][sx]
num = min(arr[it][sx], num)
}
(sx downTo fx+1).forEach{
arr[fy][it] = arr[fy][it-1]
num = min(arr[fy][it], num)
}
arr[fy][fx+1] = last
answer.add(num)
}
return answer.toIntArray()
}
}
μ°Έκ³
- ν¨μν νλ‘κ·Έλλ° μΈμ΄μΈ kotlinμ νΉμ±μ λ§κ² ν¨μλ₯Ό λ§μ΄ μ΅νμ π½
λ°μν