ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Programmers] Lv1. ์ตœ์†Œ์ง์‚ฌ๊ฐํ˜•(Kotlin)
    SW Test/Programmers 2021. 11. 12. 02:29
    ๋ฐ˜์‘ํ˜•
    • ์ด ๋ฌธ์ œ๋Š” kotlin์œผ๋กœ ํ’€์ด๋ฅผ ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค.

    ๋ฌธ์ œ

    https://programmers.co.kr/learn/courses/30/lessons/86491

     

    ์˜ˆ์‹œ

    https://programmers.co.kr/learn/courses/30/lessons/86491

     

    ํ’€์ด

    • kotlin ์—์„œ maxํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ kotlin.math.max ๋ฅผ import ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.
    • ํ•ด๋‹น ๋ฌธ์ œ๋Š” ๋ฐฐ์—ด ํ•œ ์ค„์”ฉ ํ™•์ธํ•˜์—ฌ first๋Š” ๊ธฐ์กด์˜ ๊ฐ’ y์™€ ๋ฐฐ์—ด์˜ ์ฒซ ๋ฒˆ์งธ ๊ฐ’ ์ค‘ ํฐ ๊ฐ’๊ณผ ๊ธฐ์กด์˜ x์™€ ๋ฐฐ์—ด์˜ ๋‘ ๋ฒˆ์งธ ๊ฐ’ ์ค‘ ํฐ ๊ฐ’์„ ๊ณฑํ•˜๋Š” ๊ฐ’์ž…๋‹ˆ๋‹ค. second๋Š” ๋ฐ˜๋Œ€๋กœ y์™€ ๋ฐฐ์—ด์˜ ๋‘ ๋ฒˆ์งธ ๊ฐ’ ์ค‘ ํฐ ๊ฐ’๊ณผ x์™€ ์ฒซ ๋ฒˆ์งธ ๊ฐ’ ์ค‘ ํฐ ๊ฐ’์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ์ดํ›„ first์™€ second ์ค‘ ๋” ์ž‘์€ ๊ฐ’์ด answer์ด ๋ฉ๋‹ˆ๋‹ค.
    import kotlin.math.*
    
    class Solution {
        fun solution(sizes: Array<IntArray>): Int {
            var answer: Int = 0
            
            var y = 0
            var x = 0
            for(arr in sizes){
                var first = max(y,arr[0])*max(x,arr[1])
                var second = max(y,arr[1])*max(x,arr[0])
                if(first > second){
                    answer = second
                    y = max(y,arr[1])
                    x = max(x,arr[0])
                }else{
                    answer = first
                    y = max(y,arr[0])
                    x = max(x,arr[1])
                }
            }
            
            return answer
        }
    }

     

    • ๊ตณ์ด ์ €์ฒ˜๋Ÿผ ๊ตฌํ˜„ํ•˜์ง€ ์•Š๊ณ  sizes ๋ฐฐ์—ด์—์„œ ํ•˜๋‚˜์”ฉ ํƒ์ƒ‰ ์‹œ arr๋ฅผ ์ •๋ ฌํ•œ ๋’ค first๋Š” ์ฒซ ๋ฒˆ์งธ ๊ฐ’๋“ค ์ค‘ ์ตœ๋Œ“๊ฐ’, second๋Š” ๋‘ ๋ฒˆ์งธ ๊ฐ’๋“ค ์ค‘ ์ตœ๋Œ“๊ฐ’์„ ๊ตฌํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. first์™€ second๋ฅผ ๊ณฑํ•œ ๊ฐ’์ด ๋‹ต์ด ๋ฉ๋‹ˆ๋‹ค.
    • ๋‹ค๋งŒ, sort ํ•จ์ˆ˜๋ฅผ ์ผ๋‹ค๊ณ  0.34 ms์—์„œ 8.88ms ๋กœ ์†๋„์ž์ฒด๋Š” ๋Š๋ ค์ง€๋Š” ๊ฒƒ์„ ํ™•์ธํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์†๋„๋กœ ๋ณด๋ฉด ์ •๋ง ์ž‘์€ ์ˆซ์ž์ง€๋งŒ, arr์˜ ํฌ๊ธฐ๊ฐ€ ๊ณ ์ •์ ์œผ๋กœ 2์ด๊ณ  sortํ•จ์ˆ˜๋Š” ์ž์ฒด์ ์œผ๋กœ ํ€ต ์†ŒํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์œ„ ์ฝ”๋“œ๋ณด๋‹ค ๋นจ๋ฆฌ ๋™์ž‘ํ•  ๊ฒƒ ๊ฐ™์•˜๋Š”๋ฐ ๋Š๋ฆฝ๋‹ˆ๋‹ค. ํ•จ์ˆ˜ ํ˜ธ์ถœ ์Šคํƒ์œผ๋กœ ์ธํ•ด์„œ ๊ทธ๋Ÿฐ๊ฑด์ง€.. ์ •ํ™•ํžˆ ์ด์œ  ์•„์‹œ๋Š” ๋ถ„์€ ๋Œ“๊ธ€ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.
    import kotlin.math.*
    
    class Solution {
        fun solution(sizes: Array<IntArray>): Int {
            var answer: Int = 0
            
            var y = 0
            var x = 0
            for(arr in sizes){
                arr.sort()
                y = max(y, arr[0])
                x = max(x, arr[1])
                answer = y*x
            }
            
            return answer
        }
    }

     

     

    ์ฐธ๊ณ 

     

    max - Kotlin Programming Language

     

    kotlinlang.org

     

    ๋ฐ˜์‘ํ˜•
Designed by Tistory.