SW Test/Programmers

[Programmers] Lv2. ์ฃผ์ฐจ ์š”๊ธˆ ๊ณ„์‚ฐ(kotlin)

An effort will never betray ๐Ÿ˜Ž 2022. 10. 22. 17:02
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

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

 

 

 

์˜ˆ์‹œ

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

 

 

 

ํ’€์ด

  • ๋‘ ๊ฐœ์˜ map์„ ํ†ตํ•ด ํ•˜๋‚˜๋Š” ์ž…/์ถœ์ฐจ ๊ธฐ๋ก๊ณผ ํ•˜๋‚˜๋Š” ํ•ด๋‹น ์ฐจ๋Ÿ‰์˜ ์ „์ฒด ์‹œ๊ฐ„์„ ๊ธฐ๋กํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
import kotlin.math.*

class Solution {
    fun solution(fees: IntArray, records: Array<String>): IntArray {
        val score = mutableMapOf<String, Int>()
        var map = mutableMapOf<String, Int>()
        
        records.forEach{ record ->
            val rl = record.split(" ")
            val time = rl.get(0).split(":").let{ it.first().toInt()* 60 + it.last().toInt()}
            val num = rl.get(1)
            
            if(map.contains(num)){
                score.put(num, (score.get(num)?: 0) + time - map.get(num)!!)
                map.remove(num)
            }else{
                map.put(num, time)
            }
        }
        
        for(elem in map){
            score.put(elem.key, (score.get(elem.key)?: 0) + 60*23 + 59 - elem.value)
        }
        
        return score.toSortedMap().map{
            if(it.value <=fees.get(0).toInt()){
                fees.get(1).toInt()
            }else{
                fees.get(1).toInt() + ceil((it.value - fees.get(0).toInt())/fees.get(2).toFloat()).toInt()*fees.get(3).toInt()
            }
        }.toIntArray()
    }
}

 

 

 

์ฐธ๊ณ 

  • ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์ธ kotlin์˜ ํŠน์„ฑ์— ๋งž๊ฒŒ ํ•จ์ˆ˜๋ฅผ ๋งŽ์ด ์ตํžˆ์ž ๐Ÿ˜ฝ
  • mutableMapOf๋Š” Java์˜ LinkedHashMap์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. LinkedHashMap์€ HashMap๊ณผ key์™€ value๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ํ˜•ํƒœ๋Š” ๋™์ผํ•˜๋‚˜ ๋ฐ์ดํ„ฐ์˜ ์ˆœ์„œ๋ฅผ ๋ณด์žฅ๋ฐ›๊ธฐ ์œ„ํ•ด์„œ ๋ณ„๋„์˜ Double Linked List๋ฅผ ๊ด€๋ฆฌํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
  • https://developer.android.com/reference/java/util/LinkedHashMap
 

LinkedHashMap  |  Android Developers

 

developer.android.com

 

๋ฐ˜์‘ํ˜•