-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathResult.java
More file actions
27 lines (24 loc) · 769 Bytes
/
Result.java
File metadata and controls
27 lines (24 loc) · 769 Bytes
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
package hackrank.algorithm.implement.utopian;
/**
* @see <a href="https://www.hackerrank.com/challenges/utopian-tree">Utopian Tree</a>
*/
public class Result {
/**
* @param n Number of growth cycles
* @return The height of the tree after {@code n} cycles
*/
public static int utopianTree(int n) {
return findUtopianTreeHeight(1, n);
}
private static int findUtopianTreeHeight(int height, int numberCycles) {
if (numberCycles == 0) {
return height;
} else if (numberCycles == 1) {
return height * 2;
} else if (numberCycles == 2) {
return height * 2 + 1;
} else {
return findUtopianTreeHeight(height * 2 + 1, numberCycles - 2);
}
}
}