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

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค • ์ฝ”ํ‹€๋ฆฐ] Generating Prime Number #12977

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

#12977

๐ŸŽ„ Question ?

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

class Solution {
    fun solution(nums: IntArray): Int {
        var answer = -1
 
        // [์‹คํ–‰] ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ์ถœ๋ ฅ ๊ฐ’์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
        println("Hello Kotlin")

        return answer
    }
}

๐Ÿงฉ Thought Process

  1. nums ๋ฐฐ์—ด์—์„œ 3๊ฐœ์˜ ๊ฐ’์„ ๋”ํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ  for ๋ฌธ์„ ์ด์šฉํ•˜์—ฌ ์ฐพ์•„์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.. 
  2. nums ๋ฐฐ์—ด์—์„œ 3๊ฐœ์˜ ๊ฐ’์„ ๋”ํ–ˆ์„ ๋•Œ ์ƒ๊ธฐ๋Š” ์†Œ์ˆ˜์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ธ๋Š” ๋ฌธ์ œ์ธ์ค„ ์•Œ์•˜๋Š”๋ฐ nums ๋ฐฐ์—ด์—์„œ 3๊ฐœ์˜ ๊ฐ’์„ ๋”ํ–ˆ์„ ๋•Œ ์†Œ์ˆ˜๊ฐ€ ์ƒ๊ธฐ๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋ฌผ์–ด๋ณด๋Š” ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค. ๋ฌธ์ œ๋ฅผ ์ •๋ง ๊ผผ๊ผผํžˆ ์ฝ์–ด์•ผ ํ•  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
  3. ๊ทธ๋ž˜์„œ ์•„๋ž˜ ์ฝ”๋“œ์—๋Š” ์ค‘๋ณต๋˜๋Š” ์†Œ์ˆ˜๋ฅผ ์ œ๊ฑฐํ•˜๊ธฐ ์œ„ํ•ด mutableSet์„ ์‚ฌ์šฉํ–ˆ์—ˆ๋Š”๋ฐ mutableList๋กœ ๋ฐ”๊พธ์–ด ์ค‘๋ณต๋˜๋Š” ๊ฒฝ์šฐ๋„ ์„ธ์ฃผ๋‹ˆ๊นŒ ์ฝ”๋“œ๊ฐ€ ์„ฑ๊ณต์ ์œผ๋กœ ์‹คํ–‰ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
  4. forEach๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ์†Œ์ˆ˜์˜ ๊ฐœ์ˆ˜๋ฅผ ์…ˆํ•ด ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.
  5. isPrimeNumber์ด๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด ์†Œ์ˆ˜๋ฅผ ํŒ๋ณ„ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

 

class Solution {
    fun solution(nums: IntArray): Int {
        var answer = 0
        var set : MutableSet<Int> = mutableSetOf()
 
        // [์‹คํ–‰] ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ์ถœ๋ ฅ ๊ฐ’์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
        println("Hello Kotlin")
        for (i in 0 until nums.size - 2 ) {
            for (j in (i+1) until nums.size - 1) {
                for (k in (j+1) until nums.size) {
                    set.add(nums[i] + nums[j] + nums[k])
                }
            }
        }
        println(set)
        
        set.forEach {
            when (isPrimeNumber(it)) {
                true -> answer += 1
            }
        }
        return answer
    }
    
    fun isPrimeNumber(num: Int): Boolean {
        for (i in 2 until num) {
            when {
                (num%i == 0) -> return false
            }
        }
        return true
    }
}

๐ŸŽ€ Answer

class Solution {
    fun solution(nums: IntArray): Int {
        var answer = 0
        var set : MutableList<Int> = mutableListOf()
 
        // [์‹คํ–‰] ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ์ถœ๋ ฅ ๊ฐ’์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
        println("Hello Kotlin")
        for (i in 0 until nums.size - 2 ) {
            for (j in (i+1) until nums.size - 1) {
                for (k in (j+1) until nums.size) {
                    set.add(nums[i] + nums[j] + nums[k])
                }
            }
        }
        println(set)
        
        set.forEach {
            when (isPrimeNumber(it)) {
                true -> answer += 1
            }
        }
        return answer
    }
    
    fun isPrimeNumber(num: Int): Boolean {
        for (i in 2 until num) {
            when {
                (num%i == 0) -> return false
            }
        }
        return true
    }
}

 

๐ŸŽ Result

๐Ÿ† Comment