Algorithms
Utopian Tree
seoca
2020. 10. 1. 21:16
Solution in JavaScript
const n = 5;
function utopianTree(n) {
let cycle = 1;
let height = 1;
while (cycle <= n){
if(cycle % 2 !== 0 ){ //홀수 즉, doubles its height in spring
height *= 2;
}else{
height++; //짝수. grows a meter in summer
}
cycle++;
}
console.log(height);
}
utopianTree(n);
찾고자하는 n까지의 cycle을 전부 condition에 맞게 구해야한다.
Spring은 값이 double이 되고 Summer는 값에 +1
Reference
www.hackerrank.com/challenges/utopian-tree/problem
Utopian Tree | HackerRank
Predict the height of the tree after N growth cycles.
www.hackerrank.com