티스토리 뷰
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 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- code refactoring
- hackerrank solution
- Object type casting
- easy javascript algorithm
- HashMap
- equals()
- repeat()
- hackerrank javascript solution
- compareTo()
- string class in java
- spring boot application
- substring()
- Javascript Algorithm
- C++
- easy algorithm
- Collection Framework
- javascript
- hackerrank
- 프로그래머스
- rest parameter
- algorithm
- hackerrank javascript
- 프로그래머스 알고리즘문제
- ... in Javascript
- 프로그래머스 알고리즘
- java
- math.abs
- math.max
- HackerRank Algorithm
- 알고리즘
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | |||||
| 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| 10 | 11 | 12 | 13 | 14 | 15 | 16 |
| 17 | 18 | 19 | 20 | 21 | 22 | 23 |
| 24 | 25 | 26 | 27 | 28 | 29 | 30 |
| 31 |
글 보관함