#136798
๐ Question ?
https://school.programmers.co.kr/learn/courses/30/lessons/136798#
๐งฉ Thought Process
- ์์์ ๊ฐ์๋ฅผ ์ ํ ์ ์๋ ํจ์๋ฅผ ๋ฐ๋ก ๋ง๋ค๊ณ
- ์์์ ๊ฐ์๊ฐ ์ ํ ์ ๋ณด๋ค ํฌ๋ฉด ๊ณต๊ฒฉ ์๋ฅผ ๋ฌด๊ฒ๋ก ๋ํด์ค๋ค.
- ์์์ ๊ฐ์๋ฅผ ์ฐพ์ ๋ ์๊ฐ ๋ณต์ก๋๋ฅผ ๊ณ ๋ คํด ์ฃผ์ด์ผ ํ๋ค.
class Solution {
fun solution(number: Int, limit: Int, power: Int): Int {
var answer: Int = 0
for (i in 1..number) {
var nLCD = countLCD(i)
when {
(nLCD > limit) -> answer += power
else -> answer += nLCD
}
}
return answer
}
fun countLCD(number: Int): Int {
var count = 0
for (i in 1..number) {
when {
(number % i == 0) -> count += 1
}
}
println(count)
return count
}
}
๐ Answer
import kotlin.math.sqrt
class Solution {
fun solution(number: Int, limit: Int, power: Int): Int {
var answer: Int = 0
for (i in 1..number) {
var nLCD = countLCD(i)
when {
(nLCD > limit) -> answer += power
else -> answer += nLCD
}
}
return answer
}
fun countLCD(number: Int): Int {
var count = 0
if (number == 1) {
count += 1
} else {
count += 2
for (i in 2 .. sqrt(number.toDouble()).toInt()) {
if (number%i == 0) {
when {
(i*i == number) -> count += 1
else -> count += 2
}
}
}
}
return count
}
}
๐ Result