当前位置:网站首页>LeetCode 5561. 获取生成数组中的最大值
LeetCode 5561. 获取生成数组中的最大值
2020-11-10 10:41:00 【osc_bj12kvua】
1. 题目
给你一个整数 n 。按下述规则生成一个长度为 n + 1 的数组 nums :
nums[0] = 0nums[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
边栏推荐
- 实验2
- Taulia推出国际支付条款数据库
- CSDN BUG1: to be added
- Key layout of the Central Government: in the next five years, self-reliance and self-improvement of science and technology will be the priority, and these industries will be named
- 工厂方法模式
- js解决浏览器打印自动分页的问题
- JMeter interface test -- a solution with token
- csdn bug7:待加
- What does the mremote variable in servicemanagerproxy refer to?
- Ineuos industrial interconnection platform, web configuration (ineuview) increases the function of importing and exporting engineering views, as well as optimization and repair. Release: v3.2.1
猜你喜欢

gnu汇编-基本数学方程-乘法

For programmers, those unfamiliar and familiar computer hardware
![[论文阅读笔记] Community-oriented attributed network embedding](/img/17/1d1989945d943ca3cd2a2b8a5731de.jpg)
[论文阅读笔记] Community-oriented attributed network embedding

Day85: Luffy: shopping cart switching price according to different validity period & shopping cart deletion operation & price settlement & foreplay of order page

关于centos启动报错:Failed to start Crash recovery kernel arming的解决方案

Key layout of the Central Government: in the next five years, self-reliance and self-improvement of science and technology will be the priority, and these industries will be named

CSDN bug4: to be added

区块链论文集【三十一】

selenium webdriver使用click一直失效问题的几种解决方法

CSDN bug9: to be added
随机推荐
[operation tutorial] introduction and opening steps of easygbs subscription function of national standard gb28181 protocol security video platform
The length of the last word in leetcode
走进C# abstract,了解抽象类与接口的异同
What does the mremote variable in servicemanagerproxy refer to?
csdn bug1:待加
Raspberry pie drum set WiFi
中央重点布局:未来 5 年,科技自立自强为先,这些行业被点名
csdn bug11:待加
Bifrost 位点管理 之 异构中间件实现难点(1)
Oschina: my green plants are potatoes, ginger and garlic
CUDA_ Get the specified device
GNU assembly basic mathematical equations multiplication
CSDN bug7: to be added
编码风格:Mvc模式下SSM环境,代码分层管理
仅发送options请求,没有发送post解决方案
上线1周,B.Protocal已有7000ETH资产!
[论文阅读笔记] Network Embedding with Attribute Refinement
Promote China manufacturing upgrade, 3D visualization of production line in automobile assembly workshop
[leetcode] 93 balanced binary tree
奸商加价销售mate40,小米可望在高端手机市场夺取更多市场