#12943
๐ Question ?
https://school.programmers.co.kr/learn/courses/30/lessons/12943
class Solution {
fun solution(num: Int): Int {
var answer = 0
return answer
}
}
๐งฉ Thought Process
- ์ ๋ ฅ๋ ์๊ฐ ์ง์๋ผ๋ฉด 2๋ก ๋๋๋๋ค.
- ์ ๋ ฅ๋ ์๊ฐ ํ์๋ผ๋ฉด 3์ ๊ณฑํ๊ณ 1์ ๋ํฉ๋๋ค.
- 1๋ฒ and 2๋ฒ ๋ฌดํ๋ฃจํ (์ธ๊ธฐ)
- until ์ ๋ ฅ๋ ์๊ฐ 1
๐ Answer
class Solution {
fun solution(num: Int): Int {
var answer = 0
var n = num
while (n > 1) {
if (n % 2 == 0) {
n /= 2
answer += 1
} else {
n = 3*n + 1
answer +=1
}
}
if (n != 1) {
answer = -1
}
return answer
}
}
๐ Result
๐ Comment
๋ ์ ์ ํ๋ ธ๋ค. ์ด์ฏค๋๋ ์ ์ ํ๋ฆฌ๋๋ฐ์ ์์๋ฅผ ๋๊ธฐ ๋ณด๋ค๋ ๋ค๋ฅธ ํ์ด๋ฅผ ๋ณด๋ฉด์ ๊ณต๋ถํ๋๋ฐ์ ์์๋ฅผ ๋ฌ์ผ ๊ฒ ๋ค. ์ ์ ํ๋ ธ๋ค๋๊ฑด ์๋ฌด๊ฒ๋ ๋ฐฐ์ด๊ฒ ์๋ค๋ ์ ..