# 在生成的阵列Leetcode解决方案中获得最大收益

1. arr [0] = 0，arr [1] = 1
2. arr [2 * i] = arr [i]其中2 <= 2 * i <= n
3. 和arr [2 * i + 1] = arr [i] + arr [i + 1]其中2 <= 2 * i + 1 <= n

`n = 7`
`3`

nums [0] = 0，nums [1] = 1
nums [（1 * 2）= 2] = nums [1] = 1
nums [（1 * 2）+1 = 3] = nums [1] + nums [2] = 1 +1 = 2
nums [（2 * 2）= 4] = nums [2] = 1
nums [（2 * 2）+1 = 5] = nums [2] + nums [3] = 1 +2 = 3
nums [（3 * 2）= 6] = nums [3] = 2
nums [（3 * 2）+1 = 7] = nums [3] + nums [4] = 2 +1 = 3

## 代码

### 用于在生成的阵列Leetcode解决方案中获取最大值的C ++代码

```#include <bits/stdc++.h>
using namespace std;

int getMaximumGenerated(int n) {
if(n == 0)return 0;
if(n == 1)return 1;
vector<int> tmp(n+1);
tmp[0] = 0;
tmp[1] = 1;
for(int i=1;i<=n;i++){
if(2*i<=n)
tmp[2*i] = tmp[i];
if(2*i-1<=n)
tmp[2*i-1] = tmp[i] + tmp[i-1];
}
int ans = 0;
for(int i=0;i<=n;i++)
ans = max(tmp[i], ans);
return ans;
}

int main(){
cout<<getMaximumGenerated(7);
}```
`3`

### 在生成的阵列Leetcode解决方案中获取最大数量的Java代码

```import java.util.*;
import java.io.*;

class Main {

public static int getMaximumGenerated(int n) {
if(n == 0)return 0;
if(n == 1)return 1;
int[] tmp = new int[n+1];
tmp[0] = 0;
tmp[1] = 1;
for(int i=1;i<=n;i++){
if(2*i<=n)
tmp[2*i] = tmp[i];
if(2*i-1<=n)
tmp[2*i-1] = tmp[i] + tmp[i-1];
}
int ans = 0;
for(int i=0;i<=n;i++)
ans = Math.max(tmp[i], ans);
return ans;
}

public static void main(String[] args){
System.out.println(getMaximumGenerated(7));
}
}
```
`3`