SW Test/Programmers

[Programmers] Lv2. ν–‰λ ¬ ν…Œλ‘λ¦¬ νšŒμ „ν•˜κΈ°(kotlin)

An effort will never betray 😎 2022. 9. 9. 11:06
λ°˜μ‘ν˜•

문제

https://school.programmers.co.kr/learn/courses/30/lessons/77485

 

 

μ˜ˆμ‹œ

 

 

 

풀이

  • ν–‰λ ¬ νšŒμ „ λ‘œμ§μ€ 인덱슀 ν•˜λ‚˜μ”© μ΄λ™ν•˜λ„λ‘ κ΅¬ν˜„ν•˜λ©΄ λ©λ‹ˆλ‹€.
  • 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의 νŠΉμ„±μ— 맞게 ν•¨μˆ˜λ₯Ό 많이 읡히자 😽
λ°˜μ‘ν˜•