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

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค • ์ฝ”ํ‹€๋ฆฐ] ๋‘ ๊ฐœ ๋ฝ‘์•„์„œ ๋”ํ•˜๊ธฐ #68644

by ํ‚ค์œค 2023. 12. 4.

#68644

๐ŸŽ„ Question ?

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

class Solution {
    fun solution(numbers: IntArray): IntArray {
        var answer: IntArray = intArrayOf()
        return answer
    }
}

๐Ÿงฉ Thought Process

  1. ์ž๋ฆฟ์ˆ˜๋ฅผ ๋”ํ•  ๋•Œ๋งˆ๋‹ค set์— ๊ฐ’์„ ์ถ”๊ฐ€ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ๋‹ค. set์—๋Š” ๊ฐ’์ด ์ค‘๋ณต๋˜์–ด ๋“ค์–ด๊ฐ€์ง€ ์•Š์œผ๋‹ˆ๊นŒ ํšจ์œจ์ ์ผ ๊ฒƒ์ด๋ผ๊ณ  ํŒ๋‹จํ–ˆ๋‹ค.
  2. ์•„๋ž˜ ์ฝ”๋“œ ์ฒ˜๋Ÿผ ์ž‘์„ฑํ•˜๋‹ˆ 44% ์ •ํ™•๋„๋กœ ๋ฌธ์ œ๋ฅผ ํ‹€๋ ธ๋‹ค. ์•„๋งˆ๋„ numbers ๋ฐฐ์—ด์„ ๋ฏธ๋ฆฌ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ๋ฐ”๊พธ์–ด ๋†“๊ณ  ์ฐจ๊ทผ์ฐจ๊ทผ ๊ฐ’์„ ๋”ํ•ด์„œ ์ธ ๊ฒƒ ๊ฐ™๋‹ค. ๊ทธ๋ž˜์„œ sorted์„ ๋‚˜์ค‘์— ํ•ด์ฃผ์—ˆ๋”๋‹ˆ ๋ฌธ์ œ ๋งž์Œ!
class Solution {
    fun solution(numbers: IntArray): IntArray {
        numbers.sort()
        var answer = mutableSetOf<Int>()
        for (i in 0 until numbers.size-1) {
            for (j in i+1 until numbers.size) {
                answer.add(numbers[i]+numbers[j])
            }
        }
        return answer.toIntArray()
    }
}

๐ŸŽ€ Answer

class Solution {
    fun solution(numbers: IntArray): IntArray {
        var answer = mutableSetOf<Int>()
        for (i in 0 until numbers.size-1) {
            for (j in i+1 until numbers.size) {
                answer.add(numbers[i]+numbers[j])
            }
        }
        return answer.sorted().toIntArray()
    }
}

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

class Solution {
    fun solution(numbers: IntArray): IntArray {
        var set = mutableSetOf<Int>()

        (0 until numbers.size - 1).forEach { index1 ->
            (index1 + 1 until numbers.size).forEach { index2 ->
                set.add(numbers[index1] + numbers[index2])
            }
        }

        return set.sorted().toIntArray()
    }
}

์ ‘๊ทผ ๋ฐฉ์‹์€ ๋‚ด๊ฐ€ ํ•œ ๋ฐฉ์‹๊ณผ ๋™์ผํ•œ๋ฐ for๋ฌธ ๋Œ€์‹ ์— forEach ๋ฅผ ์‚ฌ์šฉํ•˜์˜€๋‹ค. [forEach() : https://occhiolism.tistory.com/48]

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

class Solution {
    var answer = ArrayList<Int>()
    fun solution(numbers: IntArray): IntArray {
        check(numbers, Array<Boolean>(numbers.size){false}, 0, 0)
        return answer.distinct().sorted().toIntArray()
    }
    
    fun check(array : IntArray, visit : Array<Boolean>, start : Int, count : Int){
        var sum = 0
        if(count == 2){
            for(i in 0..visit.size-1){ // 5๋ฒˆ์งธ ์ค„
                if(visit[i]){
                    sum +=array[i]
                }
            }				//11๋ฒˆ์งธ ์ค„
            answer.add(sum)
        }
        else{
            for( i in start..array.size-1){
                visit[i] = true
                check(array, visit, i+1,  count+1)
                visit[i] = false
            }
        }
    }
}
์ถœ์ฒ˜: https://kolinuno.tistory.com/18?category=1045563 [์ฝ”๋ฆฐ์ด์œ ๋…ธ:ํ‹ฐ์Šคํ† ๋ฆฌ]

์ ‘๊ทผ ๋ฐฉ์‹: ์žฌ๊ท€ํ•จ์ˆ˜()๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ์™„์ „ ํƒ์ƒ‰์„ ํ•ด์ฃผ์—ˆ๋‹ค.

๐ŸŽ Result

๐Ÿ† Comment

sort() ํ•จ์ˆ˜๋กœ ํ–ˆ์„ ๋•Œ๋Š” ์˜ค๋ฅ˜๊ฐ€ ๋‚˜์„œ ์ฝ”๋“œ๊ฐ€ ์ž‘๋™์ด ์•ˆ๋๋‹ค.. sorted๋งŒ ๋˜๋Š”๊ฑธ๋กœ?