当前位置:网站首页>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
边栏推荐
- [paper reading notes] large scale heterogeneous feature embedding
- Oschina: my green plants are potatoes, ginger and garlic
- Promote China manufacturing upgrade, 3D visualization of production line in automobile assembly workshop
- CSDN bug9: to be added
- 【操作教程 】国标GB28181协议安防视频平台EasyGBS订阅功能介绍及开启步骤
- Coding style: SSM environment in MVC mode, code hierarchical management
- An unsafe class named unsafe
- C++ STL容器篇
- mac终端Iterm2支持rz和sz的解决方案
- 消防知识线上答题活动小程序复盘
猜你喜欢

CSDN bug3: to be added

寻找性能更优秀的不可变小字典

解决Coursera视频无法观看的三种方法(亲测有效)
![[论文阅读笔记] Network Embedding with Attribute Refinement](/img/71/760f29f8a13b366cf3bad4fb48bd63.jpg)
[论文阅读笔记] Network Embedding with Attribute Refinement

js解决浏览器打印自动分页的问题
![[paper reading notes] a multilayered informational random walk for attributed social network embedding](/img/3d/657a60600219ce6cfc514ad1b1bb49.jpg)
[paper reading notes] a multilayered informational random walk for attributed social network embedding

Day85: Luffy: shopping cart switching price according to different validity period & shopping cart deletion operation & price settlement & foreplay of order page
![[paper reading notes] network embedding with attribute refinement](/img/71/760f29f8a13b366cf3bad4fb48bd63.jpg)
[paper reading notes] network embedding with attribute refinement

《Python Cookbook 3rd》笔记(2.2):字符串开头或结尾匹配
![[paper reading notes] large scale heterogeneous feature embedding](/img/00/df94bfe594e17ab120c30fd6b31931.jpg)
[paper reading notes] large scale heterogeneous feature embedding
随机推荐
After seven years of pursuing, nearly one billion US dollars of bitcoin was eventually confiscated and confiscated by the US government
Connection to XXX could not be established. Broker may not be available
[paper reading notes] rosane, robust and scalable attributed network embedding for sparse networks
Oschina: my green plants are potatoes, ginger and garlic
从大专生到蚂蚁金服CTO,他写下“支付宝”第一行代码:逆风的方向,更适合飞翔!...
利用尾巴作为时间序列进行处理来识别鲸鱼
GNU assembly basic mathematical equations multiplication
推动中国制造升级,汽车装配车间生产流水线 3D 可视化
getIServiceManager() 源码分析
史上最全异常检测算法概述
CSDN bug4: to be added
The length of the last word in leetcode
Several solutions to the problem that selenium webdriver always fails to use click
CSDN bug10: to be added
CSDN bug5: to be added
从零开始学习 YoMo 系列教程:开篇
安卓快速关机APP
If you need a million objects
实验2
《Python Cookbook 3rd》笔记(2.1):使用多个界定符分割字符串