Skip to content

Commit 8ee8642

Browse files
authored
Create 1646-get-maximum-in-generated-array.js
1 parent 6f29516 commit 8ee8642

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const arr = [0, 1, 1]
2+
/**
3+
* @param {number} n
4+
* @return {number}
5+
*/
6+
const getMaximumGenerated = function(n) {
7+
if(arr[n] != null) return Math.max(...arr.slice(0, n + 1))
8+
const oddOrEven = num => num % 2 === 0 ? 'even' : 'odd'
9+
const hi = arr.length - 1
10+
for(let i = hi + 1; i <= n; i++) {
11+
let tmp, chk = oddOrEven(i)
12+
if(chk === 'odd') tmp = arr[Math.floor(i / 2)] + arr[Math.floor(i / 2) + 1]
13+
else tmp = arr[Math.floor(i / 2)]
14+
arr[i] = tmp
15+
}
16+
return Math.max(...arr.slice(0, n + 1))
17+
};

0 commit comments

Comments
 (0)