티스토리 뷰

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

 

'Algorithms' 카테고리의 다른 글

Beautiful Days at the Movies  (0) 2020.10.18
Migratory Birds  (0) 2020.10.01
Electronics Shop  (0) 2020.09.25
The Hurdle Race  (0) 2020.09.25
Cats and a Mouse  (0) 2020.09.17