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

 

 

 

 

๋ฌธ์ œ

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

 

 

 


 

์ ‘๊ทผ

 

  • ์ฃผ์–ด์ง„ number ๋ฐฐ์—ด์˜ ์›์†Œ ์ค‘ ํ•ฉ์ณค์„ ๋•Œ 0์ด ๋˜๋Š” ์„ธ ์Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ๋ฌธ์ œ๋‹ค.
  • ๋ฐ˜๋ณต๋ฌธ์„ ์—ฌ๋Ÿฌ ๊ฐœ ์จ์„œ ์•ž์˜ ์ˆ˜๋ฅผ ๋”ํ•˜๋Š” ๋ฐฐ์—ด์— ๋„ฃ๊ณ , ์•ž์˜ ์ˆ˜๋ณด๋‹ค ์ ์€ ๋‘๋ฒˆ์งธ ๊ฐ’์„ ์ฐพ์•„ ๋”ํ•˜๊ณ , ๋‚จ์€ ๊ฐ’๊ณผ ๊ฐ™์€ ์„ธ๋ฒˆ์งธ๊ฐ’์„ ๋”ํ•˜๊ณ , ๋ฐฐ์—ด์—์„œ ํ•ด๋‹น 1/2/3๋ฒˆ์งธ ๊ฐ’์„ ์‚ญ์ œ ๋’ค(์ค‘๋ณต ์ œ์™ธ) count++ ๋ฅผ ํ•˜๋Š” ๋ฐฉ์‹ ์ •๋„๋กœ๋ฐ–์— ์ƒ๊ฐ์ด ์•ˆ ๋‚œ๋‹ค.
  • ๋จธ๋ฆฌ๊ฐ€ ์•„์˜ˆ ์•ˆ ๋Œ์•„๊ฐ€๋Š” ๋ฌธ์ œ๋‹ค. ์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•œ ํšจ์œจ์ ์ธ ํ•จ์ˆ˜๊ฐ€ ์žˆ์„๊นŒ?
  • ๋ฌธ์ œ๋ฅผ ์ž˜๋ชป ๋ดค๋‹ค! ์ค‘๋ณต ๊ตฌ์„ฑ์ด ๊ฐ€๋Šฅํ•œ ๋ฌธ์ œ์˜€๋‹ค.

 

 


 

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

 

class Solution {
    fun solution(number: IntArray): Int {
        
    var answer = 0
    for(i in 0 until number.size - 2){
        for (j in i+1 until number.size - 1){
            for (k in j+1 until number.size){
                if(number[i] + number[j] + number[k] == 0) answer ++
            }
        }
    }
    return answer   
    }
}

 


 

์„ค๋ช…

๋”ฑํžˆ ๋‹ค๋ฅธ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ’€์ดํ•˜๋Š” ๋ฌธ์ œ๊ฐ€ ์•„๋‹ˆ์—ˆ๋‹ค. ์‚ผ์ค‘ ์กฐ๊ฑด๋ฌธ์„ ์ด์šฉํ•˜์—ฌ ํ’€ ์ˆ˜ ์žˆ์—ˆ๋‹ค.
๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์˜ ํ’€์ด๋ฅผ ๋ณด๋ฉด์„œ '์ €๋ ‡๊ฒŒ ๊ตฌํ˜„ํ•˜๋ฉด ์„ธ ์Œ์„ ๊ตฌํ˜„ํ•  ๋•Œ ์ค‘๋ณต๋œ ์›์†Œ๊ฐ€ ๋“ค์–ด๊ฐ€๋Š” ๊ฒŒ ์•„๋‹Œ๊ฐ€?' ์˜๋ฌธ์ด ๋“ค์—ˆ๋Š”๋ฐ, ์• ์ดˆ์— ์ค‘๋ณต์ด ๊ฐ€๋Šฅํ•œ ๋ฌธ์ œ์˜€๋‹ค. ๋ฐœ๋ฌธ์„ ์ œ๋Œ€๋กœ ๋ณด์ž.

 

  1. ์ฒซ ๋ฒˆ์งธ ์ˆ˜๋Š” ์ธ๋ฑ์Šค 0๋ถ€ํ„ฐ ๋’ค์—์„œ 2๋ฒˆ์งธ๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ๋‹ค.
  2. ๋‘ ๋ฒˆ์งธ ์ˆ˜๋Š” ์ธ๋ฑ์Šค ์ฒซ ๋ฒˆ์งธ ์ˆ˜+1๋ถ€ํ„ฐ ๋’ค์—์„œ 1๋ฒˆ์งธ๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ๋‹ค.
  3. ์„ธ ๋ฒˆ์งธ ์ˆ˜๋Š” ์ธ๋ฑ์Šค ๋‘ ๋ฒˆ์งธ ์ˆ˜+1๋ถ€ํ„ฐ ๋งจ ๋๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ๋‹ค.
  4. ์ด๋ ‡๊ฒŒ 3์ค‘ for๋ฌธ์„ ๊ตฌ์„ฑํ•˜๋ฉฐ ์„ธ ์ˆ˜๋ฅผ ํ•ฉ์ณ์„œ 0์ด ๋  ๋•Œ๋งˆ๋‹ค answer์˜ ์ˆซ์ž๋ฅผ 1๊ฐœ์”ฉ ์˜ฌ๋ฆฐ๋‹ค.
  5. ๋ชจ๋“  for๋ฌธ์ด ์™„๋ฃŒ๋˜์—ˆ์œผ๋ฉด answer๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.

 

์—ฌ์ „ํžˆ ๋‚จ์€ ๊ถ๊ธˆ์ฆ์ด ์žˆ๋‹ค.

  • (1) ๋งŒ์•ฝ ์ค‘๋ณต๋œ ์›์†Œ๋กœ ์Œ์„ ๋งŒ๋“ค ์ˆ˜ ์—†์„ ๋• ์–ด๋–ป๊ฒŒ ๊ตฌํ˜„ํ•˜๋Š”๊ฐ€?
  • (2) ์ผ๋ฐ˜์ ์œผ๋กœ ์•Œ๋ ค์ง„ ํด๋ฆฐ์ฝ”๋“œ๋ฅผ ์œ„ํ•œ depth๋Š” ์ตœ๋Œ€ 2์ด๋‹ค. ์ด ๋ฌธ์ œ์—์„œ๋Š” depth๋ฅผ ์–ด๋–ป๊ฒŒ ์ค„์—ฌ์•ผ ํ• ๊นŒ?

 

 

(1) ์ค‘๋ณต๋˜์ง€ ์•Š์€ ์›์†Œ๋กœ๋งŒ ์กฐํ•ฉํ•˜๊ธฐ

fun funSolution(number: IntArray): Int {
    var answer: Int = 0
    val sumMap = HashMap<Int, HashSet<Pair<Int, Int>>>()

    for (i in 0 until number.size - 1) {
        for (j in i + 1 until number.size) {
            val sum = number[i] + number[j]
            val pair = Pair(number[i], number[j])
            if (sumMap.containsKey(-sum)) {
                val pairs = sumMap[-sum]!!
                for (existingPair in pairs) {
                    if (existingPair.first != number[i] && existingPair.second != number[i] &&
                        existingPair.first != number[j] && existingPair.second != number[j]
                    ) {
                        answer++
                    }
                }
            }
            sumMap.computeIfAbsent(sum) { HashSet() }.add(pair)
        }
    }

    return answer
}
  • ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์€ ์ด๋ฏธ ๊ณ„์‚ฐํ•œ ์กฐํ•ฉ์„ ์ €์žฅํ•˜๊ณ  ๋‹ค์‹œ ๊ณ„์‚ฐํ•˜์ง€ ์•Š๋„๋ก ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.
    ์ด๋ฅผ ์œ„ํ•ด ์ด๋ฏธ ๊ณ„์‚ฐํ•œ ์กฐํ•ฉ์„ ์ €์žฅํ•˜๋Š” HashSet์ด๋‚˜ HashMap์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์ด ์ฝ”๋“œ๋Š” ๋จผ์ € ๋‘ ์›์†Œ์˜ ํ•ฉ์„ ๊ณ„์‚ฐํ•˜๊ณ , ์ด๋ฅผ Key๋กœ ํ•ด์‹œ๋งต์—์„œ ์ฐพ๋Š”๋‹ค.
    ๋งŒ์•ฝ ํ•ด๋‹น ํ•ฉ์˜ ๋ฐ˜๋Œ€๊ฐ’์ด ์ด๋ฏธ ํ•ด์‹œ๋งต์— ์žˆ๋Š” ๊ฒฝ์šฐ, ๊ทธ ํ•ฉ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ์›์†Œ๋“ค์˜ ์กฐํ•ฉ์„ ์ฐพ๋Š”๋‹ค.
    ๊ทธ๋ฆฌ๊ณ  ์ด๋ฅผ ํ†ตํ•ด ์„ธ ๊ฐœ์˜ ์›์†Œ๋กœ ๊ตฌ์„ฑ๋œ ์กฐํ•ฉ์„ ์ฐพ๊ฒŒ ๋œ๋‹ค.
  • ์•„์ง ์ž์„ธํžˆ๋Š” ๋ชจ๋ฅด๊ฒ ์ง€๋งŒ, ์ด๋Ÿฐ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค ์ •๋„๋กœ ์ฐพ์•„๋ณด์•˜๋‹ค.

 

 

(2) Depth ๋‚ฎ์ถ”๊ธฐ

  • ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด๋„ ๋”ฑํžˆ depth๋ฅผ ๋‚ฎ์ถ˜ ๊ฒŒ ๋ณด์ด์ง€ ์•Š๋Š”๋‹ค.
    ์ด ๋ฌธ์ œ์˜ ์ •๋‹ต์„ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด์„  ํ•„์—ฐ์ ์œผ๋กœ ๋†’์€ depth๊ฐ€ ํ•„์š”ํ•œ ๊ฑธ๊นŒ?

 

 


 

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

 

class Solution {
    fun solution(number: IntArray) = number.indices.sumOf { i1 ->
        (i1 + 1 until number.size).sumOf { i2 ->
            (i2 + 1 until number.size).count { i3 ->
                number[i1] + number[i2] + number[i3] == 0
            }
        }
    }
}
  • number.indices
    - number ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค ๋ฒ”์œ„๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ด๋Š” 0๋ถ€ํ„ฐ number.size - 1๊นŒ์ง€์˜ ๋ฒ”์œ„๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค.
  • sumOf { i1 -> ... }
    - ์ฒซ ๋ฒˆ์งธ ์ธ๋ฑ์Šค i1์— ๋Œ€ํ•œ ์ธ๋ฑ์Šค ๋ฒ”์œ„์˜ ํ•ฉ์„ ๊ณ„์‚ฐํ•œ๋‹ค. ์ด ํ•ฉ์€ ๋‘ ๋ฒˆ์งธ ๊ณ ์ฐจ ํ•จ์ˆ˜ ๋‚ด์—์„œ ์‚ฌ์šฉ๋œ๋‹ค.
  • (i1 + 1 until number.size)
    - ์ฒซ ๋ฒˆ์งธ ์ธ๋ฑ์Šค i1 ๋‹ค์Œ๋ถ€ํ„ฐ number ๋ฐฐ์—ด์˜ ๋๊นŒ์ง€์˜ ์ธ๋ฑ์Šค ๋ฒ”์œ„๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  • sumOf { i2 -> ... }
    - ๋‘ ๋ฒˆ์งธ ์ธ๋ฑ์Šค i2์— ๋Œ€ํ•œ ์ธ๋ฑ์Šค ๋ฒ”์œ„์˜ ํ•ฉ์„ ๊ณ„์‚ฐํ•œ๋‹ค. ์ด ํ•ฉ์€ ์„ธ ๋ฒˆ์งธ ๊ณ ์ฐจ ํ•จ์ˆ˜ ๋‚ด์—์„œ ์‚ฌ์šฉ๋œ๋‹ค.
  • (i2 + 1 until number.size)
    - ๋‘ ๋ฒˆ์งธ ์ธ๋ฑ์Šค i2 ๋‹ค์Œ๋ถ€ํ„ฐ number ๋ฐฐ์—ด์˜ ๋๊นŒ์ง€์˜ ์ธ๋ฑ์Šค ๋ฒ”์œ„๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  • count { i3 -> ... }
    - ์„ธ ๋ฒˆ์งธ ์ธ๋ฑ์Šค i3์— ๋Œ€ํ•œ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์š”์†Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  • number[i1] + number[i2] + number[i3] == 0
    ์„ ํƒํ•œ ์„ธ ๊ฐœ์˜ ์ˆซ์ž์˜ ํ•ฉ์ด 0์ด ๋˜๋Š”์ง€ ํ™•์ธํ•œ๋‹ค. ๋งŒ์•ฝ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋ฉด ํ•ด๋‹น ์š”์†Œ๋Š” count๋œ๋‹ค.

 

 

 

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