当前位置:网站首页>LeetCode 5561. 获取生成数组中的最大值
LeetCode 5561. 获取生成数组中的最大值
2020-11-10 10:41:00 【osc_bj12kvua】
1. 题目
给你一个整数 n 。按下述规则生成一个长度为 n + 1 的数组 nums :
nums[0] = 0
nums[1] = 1
当 2 <= 2 * i <= n 时,nums[2 * i] = nums[i]
当 2 <= 2 * i + 1 <= n 时,nums[2 * i + 1] = nums[i] + nums[i + 1]
返回生成数组 nums 中的 最大 值。
示例 1:
输入: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
因此,nums = [0,1,1,2,1,3,2,3],最大值 3
示例 2:
输入:n = 2
输出:1
解释:根据规则,nums[0]、nums[1] 和 nums[2] 之中的最大值是 1
示例 3:
输入:n = 3
输出:2
解释:根据规则,nums[0]、nums[1]、nums[2] 和 nums[3] 之中的最大值是 2
提示:
0 <= n <= 100
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/get-maximum-in-generated-array
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
2. 解题
class Solution {
public:
int getMaximumGenerated(int n) {
if(n <= 1) return n;
vector<int> arr(n+1);
arr[0] = 0;
arr[1] = 1;
int ans = 0;
for(int i = 1; i <= n; i++)
{
if(2*i >= 2 && 2*i <= n)
{
arr[2*i] = arr[i];
ans = max(ans, max(arr[i], arr[2*i]));
}
if(2*i+1 >= 2 && 2*i+1 <= n)
{
arr[2*i+1] = arr[i]+arr[i+1];
ans = max(ans, max(arr[i], arr[2*i+1]));
}
else
break;
}
return ans;
}
};
0 ms 6.7 MB
我的CSDN博客地址 https://michael.blog.csdn.net/
长按或扫码关注我的公众号(Michael阿明),一起加油、一起学习进步!
版权声明
本文为[osc_bj12kvua]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/4265475/blog/4710539
边栏推荐
- The solution of polar experience insensitive verification
- CUDA_ Get the specified device
- Farfetch、阿里巴巴集团和历峰集团结成全球合作伙伴关系,将加速奢侈品行业数字化进程
- 《Python Cookbook 3rd》笔记(2.1):使用多个界定符分割字符串
- Experiment 2
- csdn bug11:待加
- csdn bug9:待加
- 坚持追查7年,近10亿美元比特币终被美国政府没收充公
- 【技术教程】C#控制台调用FFMPEG推MP4视频文件至流媒体开源服务平台EasyDarwin过程
- On fedlearner, the latest open source federated machine learning platform of byte
猜你喜欢
C + + STL container
Commodity management - merge purchase demand into purchase order
Getiservicemanager () source code analysis
商品管统——采购需求合并到采购单
csdn bug1:待加
竞争性编程的思考:那些神话和令人震惊的事实[图]
CCR coin robot: novel coronavirus pneumonia has accelerated the interest of regulators in CBDC.
Learning from scratch YoMo series: Opening
CSDN bug3: to be added
【操作教程 】国标GB28181协议安防视频平台EasyGBS订阅功能介绍及开启步骤
随机推荐
CSDN bug11: to be added
ServiceManagerProxy中mRemote变量指的什么?
Understanding of learning to estimate 3D hand pose from single RGB images
CSDN bug9: to be added
Mongodb index management of distributed document storage database
《Python Cookbook 3rd》笔记(2.1):使用多个界定符分割字符串
Android quick shutdown app
CSDN bug10: to be added
Three ways to solve coursera video unable to watch
[论文阅读笔记] Community-oriented attributed network embedding
《Python Cookbook 3rd》笔记(2.2):字符串开头或结尾匹配
[paper reading notes] rosane, robust and scalable attributed network embedding for sparse networks
Problems and solutions in configuring FTP server with FileZilla server
关于centos启动报错:Failed to start Crash recovery kernel arming的解决方案
csdn bug10:待加
JMeter interface test -- a solution with token
利用尾巴作为时间序列进行处理来识别鲸鱼
mac终端Iterm2支持rz和sz的解决方案
Bartender2021 realizes secure remote label printing, new year-end release
奸商加价销售mate40,小米可望在高端手机市场夺取更多市场