๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm/Kotlin

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค • ์ฝ”ํ‹€๋ฆฐ] ๋ฌธ์ž์—ด ๋‚ด ๋งˆ์Œ๋Œ€๋กœ ์ •๋ ฌํ•˜๊ธฐ #12915

by ํ‚ค์œค 2023. 11. 30.

#12915

๐ŸŽ„ Question ?

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

class Solution {
    fun solution(strings: Array<String>, n: Int): Array<String> {
        var answer = arrayOf<String>()
        return answer
    }
}

๐Ÿงฉ Thought Process

  1. for๋ฌธ ๋Œ๋ ค์„œ n๋ฒˆ์งธ ์ธ๋ฑ์Šค์˜ ์•ŒํŒŒ๋ฒณ ๊ฐ€์ ธ์˜ค๊ธฐ
  2. ์‚ฌ์ „์ˆœ์œผ๋กœ ์•ž์„  ๋ฌธ์ž์—ด์ด ์•ž์ชฝ์— ์œ„์น˜ํ•˜๋„๋ก ํ•˜๋Š” ๋ฉ”์†Œ๋“œ ๋งŒ๋“ค๊ธฐ
    - for๋ฌธ
  3. ์•ŒํŒŒ๋ฒณ์˜ ์•„์Šคํ‚ค ์ฝ”๋“œ๊ฐ’์„ ๋น„๊ตํ•˜์—ฌ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ๋ฐ”๊พธ๊ธฐ
class Solution {
    fun solution(strings: Array<String>, n: Int): Array<String> {
        var answer = arrayOf<String>()
        return answer
    }
    
    fun dictionaryOrder(strings: Array<String>, n:Int): mutableList<String> {        
        for (i in strings.indices) {
            var charList1 = string[i].toCharArray().toMutableListOf<Char>()
            var charList2 = string[i+1].toCharArray().toMutableListOf<Char>()
            
            for (j in strings[i].toCharArray().indices) {
                if (j == n) {
                    continue
                } else if (j == n - 1) {
                    charList2 = string[i+2].toCharArray().toMutableListOf<Char>()
                }
                
                if (charList1[j] == charList2[j]) {
                    continue
                }
                
            }
        }
        
    }
}

๋ญ”๊ฐ€ ๋ง‰ ๋„์ ์ด๋Š”๋ฐ ๋„ˆ๋ฌด ๋์ด ์—†๋Š” ๋Š๋‚Œ์ด๋‹ค. ๋””ํ…Œ์ผ์ด ๋งŽ์€ ๋ฌธ์ œ์ธ๊ฑฐ ๊ฐ™๋‹ค...

๋‹ค๋ฅธ์‚ฌ๋žŒ ํ’€์ด

class Solution {
    fun solution(strings: Array<String>, n: Int): Array<String>
    = strings.sortedBy { it[n] }.toTypedArray()
}

์ถœ์ฒ˜:https://yenne.tistory.com/m/93

์‚ฌ์ „ ์ˆœ ์ •๋ ฌ์ด ์•ˆ๋˜๋Š” ์ฝ”๋“œ์ด๊ธด ํ•˜์ง€๋งŒ sortedBy ํ•จ์ˆ˜๋ฅผ ์ดํ•ดํ•˜๊ธฐ ์ข‹์„๊ฑฐ ๊ฐ™์•„๋ณด์—ฌ์„œ ๋”ฐ๋กœ ๊ธฐ๋กํ•ด ๋†จ๋‹ค.
ํ•œ์ฐธ ์ฐพ์•„๋ณด๊ณ  ์ฝ”๋“œ ์ฝ์–ด๋ณด๊ณ  ๋“œ๋””์–ด sort ๋ฉ”์†Œ๋“œ๋ฅผ ๋Œ€์ถฉ ์ดํ•ดํ•œ ๊ฒƒ ๊ฐ™๋‹ค.

class Solution {
    fun solution(strings: Array<String>, n: Int): Array<String>
    = strings.sorted().sortedBy { it[n] }.toTypedArray()
}

์ด ์ฝ”๋“œ๋Š” ์‚ฌ์ „ ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ ํ›„์— ๋‹ค์‹œ n๋ฒˆ์งธ ์ธ๋ฑ์Šค์˜ ์•ŒํŒŒ๋ฒณ ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ ์ฝ”๋“œ๋‹ค

class Solution {
    fun solution(strings: Array<String>, n: Int): Array<String> {
        return strings.also {
            it.sort()
            it.sortBy { it[n] }
        }
    }
}

์ •๋ง์ด์ง€ it์„ ์ด์šฉํ•œ ์ฝ”๋“œ๊ฐ€ ๋„ˆ๋ฌด ๋‚œํ•ดํ•˜๊ฒŒ ๋ฐ›์•„๋“ค์—ฌ์ง„๋‹ค. ๋ณผ๋•Œ๋งˆ๋‹ค ์ƒˆ๋กœ์›Œ...
์œ„ ์‹์—์„œ๋Š” also (scoping function) ๋ฅผ ์‚ฌ์šฉํ•œ๊ฑฐ ๊ฐ™๋‹ค. ์ฝ”ํ‹€๋ฆฐ์˜ ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ์žˆ๋Š” ํ•จ์ˆ˜๋“ค์ด๋ผ๊ณ  ํ•œ๋‹ค.
๋‚ด์ผ ๊ณต๋ถ€ํ•ด๋ณด๋Š”๊ฑธ๋กœ... https://medium.com/@limgyumin/%EC%BD%94%ED%8B%80%EB%A6%B0-%EC%9D%98-apply-with-let-also-run-%EC%9D%80-%EC%96%B8%EC%A0%9C-%EC%82%AC%EC%9A%A9%ED%95%98%EB%8A%94%EA%B0%80-4a517292df29

ํ’€์ด ํ•œ๊ฐ€์ง€ ๋” ์žˆ์Œ

class Solution {
    fun solution(strings: Array<String>, n: Int): Array<String> {
        var answer = strings

        var list =  answer.sortedWith(compareBy({ it[n] }, { it }))
        return list.toTypedArray()
    }
}

๐ŸŽ€ Answer

class Solution {
    fun solution(strings: Array<String>, n: Int): Array<String> {
        var answer = strings

        var list =  answer.sortedWith(compareBy({ it[n] }, { it }))
        return list.toTypedArray()
    }
}

 

๐ŸŽ Result

๐Ÿ† Comment

sort ๋ถ€๋ถ„ ์ œ๋Œ€๋กœ ๊ณต๋ถ€ํ•œ ๊ฑฐ ๊ฐ™๋‹ค. ๊ทธ๋ž˜๋„ ์•„์ง ์ข€ ๋‚œํ•ดํ•˜๋‹ค