当前位置:网站首页>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;
}
边栏推荐
- SQL Server 数据库设计--SELECT语句
- 股票开户网上开户安全吗,银河证券怎么样
- Canal realizes MySQL data synchronization
- BGP federal experiment
- NodeJS 基于 Dapr 构建云原生微服务应用,从 0 到 1 快速上手指南
- Number theory -- division and blocking, common classic examples.
- 启牛开户安全性高吗?说万3的佣金靠谱吗?
- Under Arduino frame, esp32c3 +1.8 "TFT LCD is driven and displayed through tft_espi library
- 解析steam与创客教育课堂的统筹规划
- 解析创客教育活动所需的空间实践场
猜你喜欢
![[concurrent programming] Chapter 2: go deep into the reentrantlock lock lock from the core source code](/img/df/f29eed667c2a7dc02d93ac3f424198.png)
[concurrent programming] Chapter 2: go deep into the reentrantlock lock lock from the core source code
银联最新测试工程师笔试题目,你能得多少分?

Talk about HART Protocol
![[try to hack] awvs installation and simple use](/img/58/4259e35a04e9435873ed984f884761.png)
[try to hack] awvs installation and simple use

PMP备考心得 | 好的习惯、好的过程、好的结果

Unity中实现判断Missing还是Null

Huawei applications have called the checkappupdate interface. Why is there no prompt for version update in the application

驱动单片机硬件调试器的一些开源库总结(包含stlink调试器)

基于SSM的博客系统【带后台管理】
![[C language] file operation](/img/d3/5e5ce369dd3315089b529cf69b36c0.png)
[C language] file operation
随机推荐
Internet download manager is simply a killer of downloaders
解析steam与创客教育课堂的统筹规划
Is it safe to open an account online? How about Galaxy Securities
Ascension begins with change
Development of ordering system in epidemic isolation area
Unity3D学习笔记9——加载纹理
[cloud native] in the era of storm and cloud, the sharp edge of DBAs is out of the sheath
Amplitude limiting and deblocking filtering of botu PLC signal processing series
DOM系列之禁止选中文字和禁止右键菜单
UGUI源码解析——StencilMaterial
[数组]NC77 调整数组顺序使奇数位于偶数前面(一)-中等
妙啊!美团 OCTO 分布式服务治理系统,这描述也太清晰了
Mathematical modeling interpolation fitting
带你走进MySQL MVCC的世界
SQL Server 数据库设计--SELECT语句
2000. reverse word prefix
-bash: wget: 未找到命令
FasterRCNN示例代码测试1:令anchor_generator = None
[Huawei online battle service] how can new players make up frames when the client quits reconnection or enters the game halfway?
疫情隔离区订餐系统的开发