Input: n = 2
Output: 1
Explanation: According to the given rules, the maximum between nums[0], nums[1], and nums[2] is 1.
Example 3:
Input: n = 3
Output: 2
Explanation: According to the given rules, the maximum between nums[0], nums[1], nums[2], and nums[3] is 2.
Constraints:
0 <= n <= 100
Solution:
class Solution {
public int getMaximumGenerated(int n) {
if (n <= 1) return n;
int[] arr = new int[n + 1];
arr[1] = 1;
int max = 1;
for (int i = 1; 2 * i + 1 <= n; i ++) {
arr[2 * i] = arr[i];
arr[2 * i + 1] = arr[i] + arr[i + 1];
}
// System.out.println(Arrays.toString(arr));
for (int i = 1; i <= n; i ++) {
max = Math.max(arr[i], max);
}
return max;
}
}