๐ ๋ฌธ์
๐ก ์๊ฐ์ ํ๋ฆ
(1) ์, ์ ํด๋ฆฌ๋ ํธ์ ๋ฒ GCD (์ต๋๊ณต์ฝ์) ํจ์ ์์ฑํด์ ์ต์๊ณต๋ฐฐ์ ๋ฐ๋ก ๊ตฌํด๋ณด๋ฆฌ๊ธฐ ํ๋ฉด ๋๊ฒ ๊ตฐ.
์ต๋๊ณต์ฝ์ ๊ณต์ ์๊ณ ๋ฆฌ์ฆ์ ์๊ธฐ์
(2) ์ค๋ฆ ์ ๋ ฌํด์ ์ชผ๊ผฌ๋ง ์ ๋ค๋ผ๋ฆฌ ๋ง๋ ์ต์๊ณต๋ฐฐ์๋ก ๋ท๋๋ค ๋ค ๊ฒ์ฌํด๋ณด๋ ์์ผ๋ก ํด์ผ๊ฒ ๊ตฐ
๐ํ์ด (Kotlin)
class Solution {
fun gcd (a: Int, b: Int) : Int {
var bigger = a
var smaller = b
while (smaller != 0) {
val remainder = bigger % smaller
bigger = smaller
smaller = remainder
}
return bigger
}
fun lcm (a: Int, b: Int) : Int {
return a * b / gcd(a, b)
}
fun solution(arr: IntArray): Int {
arr.sort()
var answer = arr[0]
for (index in 0..arr.size - 2) {
if (answer % arr[index+1] != 0) answer = lcm(answer, arr[index+1])
}
return answer
}
}
'Algorithm > Algorithm (๋ฌธ์ ํ์ด)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[GeeksForGeeks] Detect cycle using DFS (0) | 2021.02.02 |
---|---|
[Programmers] ๊ฐ์ฅ ํฐ ์ (0) | 2021.01.11 |
[Programmers] 124 ๋๋ผ์ ์ซ์ (0) | 2021.01.04 |
[Programmers] ์์ฅ (0) | 2021.01.02 |
[Programmers] ์คํ์ฑํ ๋ฐฉ (0) | 2020.12.31 |