当前位置:网站首页>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
边栏推荐
- 解决Coursera视频无法观看的三种方法(亲测有效)
- csdn bug11:待加
- Mongodb index management of distributed document storage database
- csdn bug9:待加
- 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
- [paper reading notes] community oriented attributed network embedding
- Gets the property value of a column in the list collection object
- Farfetch、阿里巴巴集团和历峰集团结成全球合作伙伴关系,将加速奢侈品行业数字化进程
- 对于程序员,那些既陌生又熟悉的计算机硬件
- Taulia推出国际支付条款数据库
猜你喜欢

分布式文档存储数据库之MongoDB索引管理

Taulia推出国际支付条款数据库

csdn bug9:待加

Promote China manufacturing upgrade, 3D visualization of production line in automobile assembly workshop

GNU assembly language uses inline assembly to extend ASM

Python cookbook 3rd note (2.1): using multiple qualifiers to split strings

Looking for a small immutable dictionary with better performance

编码风格:Mvc模式下SSM环境,代码分层管理

For programmers, those unfamiliar and familiar computer hardware

Mongodb index management of distributed document storage database
随机推荐
从大专生到蚂蚁金服CTO,他写下“支付宝”第一行代码:逆风的方向,更适合飞翔!...
OSChina 周二乱弹 —— 我养的绿植分别为土豆,生姜,蒜
JMeter interface test -- a solution with token
An unsafe class named unsafe
利用尾巴作为时间序列进行处理来识别鲸鱼
分布式文档存储数据库之MongoDB索引管理
Day85: Luffy: shopping cart switching price according to different validity period & shopping cart deletion operation & price settlement & foreplay of order page
Oschina: my green plants are potatoes, ginger and garlic
[python学习手册-笔记]001.python前言
CSDN bug3: to be added
[paper reading notes] a multilayered informational random walk for attributed social network embedding
Graph undirected graph
我手撸了一个划线翻译工具!
Three ways to solve coursera video unable to watch
Seam engraving algorithm: a seemingly impossible image size adjustment method
After seven years of pursuing, nearly one billion US dollars of bitcoin was eventually confiscated and confiscated by the US government
Factory approach model
Coding style: SSM environment in MVC mode, code hierarchical management
坚持追查7年,近10亿美元比特币终被美国政府没收充公
leetcode之最后一个单词的长度