[Kotlin] ์ •์ˆ˜ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ๋ฐฐ์น˜ํ•˜๊ธฐ (ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์•Œ๊ณ ๋ฆฌ์ฆ˜)
๋ฐ˜์‘ํ˜•

 

 

๋ฌธ์ œ

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

 

 


 

์ ‘๊ทผ

 

  • ์ฒ˜์Œ์—” ๊ทธ๋ƒฅ ๋ฐ˜๋Œ€๋กœ ๋‚˜์—ดํ•˜๋Š” ๋ฌธ์ œ ์ธ์ง€ ์•Œ๊ณ  ๋ช‡ ์ดˆ๋งŒ์— ์ž˜๋ชป ํ’€์—ˆ๋‹ค.
  • ์ •์ˆ˜ํ˜•์„ ๋ฐ›์•„ String์œผ๋กœ ๋ฐ”๊พธ๊ณ  reversed ์‹œ์ผœ์ค€ ๋’ค, ๋‹ค์‹œ ์ •์ˆ˜ํ˜•์œผ๋กœ ๋ฐ˜ํ™˜์‹œ์ผฐ๋‹ค.

 

// ์ž˜๋ชป๋œ ๋ฌธ์ œ ์ดํ•ด๋กœ ํ‘ผ ์ฝ”๋“œ

class Solution {
    fun solution(n: Long): Long {
        
        var answer = n.toString().reversed()
        return answer.toLong()
        
    }
}

 

  • ๋‹น์—ฐํžˆ ๊ฒฐ๊ด๊ฐ’์ด ์ •๋‹ต๊ณผ ์ผ์น˜ํ•˜์ง€ ์•Š์•˜๊ณ , ์•Œ๊ณ ๋ณด๋‹ˆ ํฐ ๊ฐ’๋ถ€ํ„ฐ ์ •๋ ฌํ•ด์•ผ ํ•˜๋Š” ๋ฌธ์ œ์˜€๋‹ค.
  • Array๋‚˜ List์— n์„ ์ž˜๋ผ ํ•˜๋‚˜์”ฉ ๋„ฃ์–ด๋‘๊ณ  maxOrNull()์„ ์ด์šฉํ•ด ํฐ ์ˆซ์ž๋ถ€ํ„ฐ ๊บผ๋‚ด๋Š” ๊ฑด ์–ด๋–จ๊นŒ ์ƒ๊ฐํ–ˆ๋‹ค.

 

// ๊ตฌ์ƒ

class Solution {
    fun solution(n: Long): Long {
        
        var answerList = mutableListOf<Long>()
        var numList : LongArray = n.toString() 
            .split("") 
            .filter { it != "" } 
            .map { it.toLong() } 
            .toLongArray()
            
        if(numList.size != 0){
            answerList.add(numList.maxorNull())
        }
        else{
            ์•ž์—์„œ๋ถ€ํ„ฐ ํ•˜๋‚˜์”ฉ ๋นผ๊ธฐ
        }
        
    }
}

 

  • ๊ทธ๋Ÿฐ๋ฐ ๋ณด๋‹ˆ๊นŒ sort ๊ณ„์—ด์˜ ๋ฐฐ์—ด ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜๋ฉด ์ด๋ ‡๊ฒŒ ๋ณต์žกํ•˜๊ฒŒ ์•ˆํ•˜๊ณ  ํŽธํ•˜๊ฒŒ ๋‹ต์„ ๋„์ถœํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•˜๋‹ค.
  • ์•„๋ž˜์™€ ๊ฐ™์ด ์„ฑ๊ณตํ–ˆ๋‹ค.

 


 

์„ฑ๊ณต ์ฝ”๋“œ

 

// ํ†ต๊ณผ, ๋Š๋ฆฐ ์ฝ”๋“œ

class Solution {
    fun solution(n: Long): Long {
        
        var numList : LongArray = n.toString() 
            .split("") 
            .filter { it != "" } 
            .map { it.toLong() } 
            .toLongArray()
       
       numList.sortDescending()
       
        return numList.joinToString("").toLong()
        
    }
}

 


 

์„ค๋ช…

 

  1. numList์— n์„ string์œผ๋กœ ํ˜•๋ณ€ํ™˜ํ•˜๊ณ , ํ•œ๊ธ€์ž์”ฉ ์ชผ๊ฐœ ์ €์žฅํ•˜๊ณ , ๊ฐ๊ฐ์˜ ์š”์†Œ๋ฅผ Long์œผ๋กœ ๋‹ค์‹œ ํ˜•๋ณ€ํ™˜ํ•œ๋’ค LongArray๋กœ ๋งŒ๋“ค์–ด์ฃผ์—ˆ๋‹ค.
  2. ๊ทธ๋ ‡๊ฒŒ ๋งŒ๋“ค์–ด์ง„ numList๋ฅผ sortDescending() ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์ž์ฒด์ ์œผ๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ ๋ฐฐ์—ด ํ•œ๋‹ค.
  3. ์›ํ•˜๋Š” ์ˆœ์„œ๋Œ€๋กœ ๋งŒ๋“ค์–ด์ง„ numList์˜ ์›์†Œ๋“ค์„ ํ•˜๋‚˜์˜ ๊ธ€์ž๋กœ ํ•ฉ์ณ์•ผ ํ•˜๋‹ˆ joinToString("")์„ ์‚ฌ์šฉํ•œ๋‹ค.
  4. ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜๊ฐ’์— ๋งž๊ฒŒ Long์œผ๋กœ ํ˜•๋ณ€ํ™˜ํ•˜์—ฌ ๋ฆฌํ„ดํ•œ๋‹ค.
  5. ํšจ์œจ์ ์ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์•„๋‹ˆ๋ผ ์‹คํ–‰ ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ฆฐ๋‹ค.

 

์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ฆผ

 

 

๐Ÿ’ก์›๋ณธ ๋ฐฐ์—ด ์ •๋ ฌ

  • sort() ์›๋ณธ ๋ฐฐ์—ด์„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ
  • sortDescending() ์›๋ณธ ๋ฐฐ์—ด์„ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ

๐Ÿ’ก ์ •๋ ฌ๋œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด ๋ฐ˜ํ™˜ (์›๋ณธ ๋ฐฐ์—ด์€ ๊ทธ๋Œ€๋กœ)

  • sortedArray() ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋„๋‹ˆ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜
  • sortedArrayDescending() ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜

 


 

๋‹ค๋ฅธ ๋ฐฉ๋ฒ•

 

class Solution {
    fun solution(n: Long): Long {
       var answer = 0L

       answer = n.toString()
               .toCharArray()
               .sorted()
               .reversed()
               .joinToString("")
               .toLong()

       return answer
    }
}
  • ๋”ฐ๋กœ ์ฒ˜์Œ๋ถ€ํ„ฐ Array๋กœ ๋งŒ๋“ค์ง€ ์•Š๊ณ , answer๋ผ๋Š” ๋ณ€์ˆ˜ ์•ˆ์—์„œ Array๋กœ ๋ณ€ํ™˜ํ–ˆ๋‹ค๊ฐ€ ๋‹ค์‹œ Long์œผ๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.
  • ์ด ๊ฒฝ์šฐ CharArray๋ฅผ ๋ฐ”๋กœ ์ •๋ ฌํ–ˆ๊ธฐ์—, sortDescending์€ ์ž‘์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.

 

class Solution {
    fun solution(n: Long): Long = String(n.toString().toCharArray().sortedArrayDescending()).toLong()
}
  • ์ œ์ผ ๊ฐ„๋‹จํ•ด๋ณด์ด๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. joinToString("")์„ ์“ฐ์ง€ ์•Š๊ณ , ๋ฐ”๋กœ String์œผ๋กœ ๋‚ด๋ณด๋ƒˆ๋‹ค๊ฐ€ Long์œผ๋กœ ํ˜•๋ณ€ํ™˜ํ•œ๋‹ค.
  • ํ‰๊ท  ํ…Œ์ŠคํŠธ ์‹คํ–‰์†๋„ 13ms๋กœ ๊ฐ€์žฅ ํšจ์œจ์ ์ด์—ˆ๋‹ค.

 

 

 

 

๋ฐ˜์‘ํ˜•
 ๐Ÿ’ฌ C O M M E N T