当前位置:网站首页>1646. Recursive method of getting the maximum value in the generated array
1646. Recursive method of getting the maximum value in the generated array
2022-07-23 09:12:00 【Mr Gao】
1646. Get the maximum value in the generated array
Give you an integer n . According to the following rules to generate a length of n + 1 Array of nums :
nums[0] = 0
nums[1] = 1
When 2 <= 2 * i <= n when ,nums[2 * i] = nums[i]
When 2 <= 2 * i + 1 <= n when ,nums[2 * i + 1] = nums[i] + nums[i + 1]
Returns the generated array nums Medium Maximum value .
Example 1:
Input :n = 7
Output :3
explain : According to rules :
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
therefore ,nums = [0,1,1,2,1,3,2,3], Maximum 3
Example 2:
Input :n = 2
Output :1
explain : According to rules ,nums[0]、nums[1] and nums[2] The maximum of these is 1
Example 3:
Input :n = 3
Output :2
explain : According to rules ,nums[0]、nums[1]、nums[2] and nums[3] The maximum of these is 2
Add a little skill to this question , The solution code is as follows :
int f(n){
if(n==1){
return 1;
}
if(n==0){
return 0;
}
if(n%2==0){
return f(n/2);
}
else{
return f(n/2)+f(n/2+1);
}
}
int getMaximumGenerated(int n){
if(n==1){
return 1;
}
if(n==0){
return 0;
}
if(n==2){
return 1;
}
int max=0;
for(int i=3;i<=n;i=i+2){
// printf("%d %d",i,f(i));
if(f(i)>max){
max=f(i);
}
}
//printf(" %d ",max);
return max;
}
边栏推荐
- FasterRCNN示例代码测试1:令anchor_generator = None
- Internet download manager is simply a killer of downloaders
- It is not safe to open an account in tongdaxin
- 券商真的有保本型理财产品吗?
- rust allow dead_code
- DOM series prohibit selected text and prohibit right-click menu
- 工作中遇到一个bug的解决过程
- UGUI源码解析——IClippable
- 2000. 反转单词前缀
- 50道经典计算机网络面试题,你答得上几个?(二)
猜你喜欢

数学建模——插值拟合

华为应用已经调用了checkAppUpdate接口,为什么应用内不提示版本更新

实行自动化网络性能监控的优势

Advantages of implementing automatic network performance monitoring

UGUI源码解析——IMaskable

IDM下载器免费高质量的Win下载工具无使用限制

关系表达式 大于> 小于< 全等=== Nan isNan() 逻辑运算符 双感叹号!! && || % ++ -- 短路计算 赋值表达式 快捷运算符 顺序 闰年

How many of the 50 classic computer network interview questions can you answer? (top)

发现了一个好用到爆的数据分析利器

Talk about HART Protocol
随机推荐
解读随着教育改革的深入steam教育
Compose与RecyclerView结合效果会是怎样的?
BGP experiment
IDM downloader free high-quality win download tool without restrictions
Internet Download Manager简直就是下载器中的大杀器
基于JSP实现OA办公系统
[array]nc77 adjust the array order so that odd numbers precede even numbers (one) - medium
[advanced mathematics] elementary transformation of matrix and determinant
UGUI源码解析——MaskableGraphic
[C language] file operation
Kali 2022.2 installation
.NET开发云原生应用,你只差给自己加个油
十. 实战——云服务器
基于SSM的博客系统【带后台管理】
7. Image data processing of paddlepaddle
[try to hack] awvs installation and simple use
砥砺前行新征程,城链科技狂欢庆典在厦门隆重举行
[ManageEngine] six essential functions of network configuration management
disruptor框架无锁实现生产者消费者代码实例
关系表达式 大于> 小于< 全等=== Nan isNan() 逻辑运算符 双感叹号!! && || % ++ -- 短路计算 赋值表达式 快捷运算符 顺序 闰年