SW Test/Programmers

[Programmers] Lv2.ํ”„๋ฆฐํ„ฐ(kotlin)

An effort will never betray ๐Ÿ˜Ž 2022. 9. 30. 18:38
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

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

 

 

์˜ˆ์‹œ

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

 

 

ํ’€์ด

  • ๋ฌธ์„œ๋ฅผ ๋นผ๊ณ  ์ค‘์š”ํ•œ ๋ฌธ์„œ๊ฐ€ ์•„๋‹ ๊ฒฝ์šฐ ๋‹ค์‹œ ๋’ค๋กœ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ์‹์€ ์ „ํ˜•์ ์ธ ํ ์ž๋ฃŒ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค.
  • kotlin์—๋Š” ๋‚ด์žฅ๋œ ํ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ์—†๊ธฐ ๋•Œ๋ฌธ์— java์˜ LinkedList ๋ฅผ ์‚ฌ์šฉํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
import java.util.*

class Solution {
    fun solution(priorities: IntArray, location: Int): Int {
        var answer = 0
        val plist = priorities.sortedByDescending{it}
        var pidx = 0
        val q = LinkedList<Pair<Int, Int>>().apply{
            priorities.forEachIndexed { index, i -> add(Pair(index, i))}
        }
        
        while(q.isNotEmpty()){
            val node = q.pop()
            if(node.second == plist.get(pidx)){
                answer++
                pidx++
                
                if(node.first == location){
                    break;
                }
            }else{
                q.add(node)
            }
        }
        
        return answer
    }
}

 

 

 

์ฐธ๊ณ 

  • ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์ธ kotlin์˜ ํŠน์„ฑ์— ๋งž๊ฒŒ ํ•จ์ˆ˜๋ฅผ ๋งŽ์ด ์ตํžˆ์ž ๐Ÿ˜ฝ
  • Java์˜ LinkedList๋Š” Double Linked List ํ˜•ํƒœ๋กœ ๊ตฌํ˜„๋˜์–ด ์žˆ์œผ๋ฉฐ, Deque ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ๋ฉ”์†Œ๋“œ๋“ค๋„ ๊ตฌํ˜„๋˜์–ด ์žˆ์–ด์„œ ํ์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
๋ฐ˜์‘ํ˜•