当前位置:网站首页>Leetcode 5561. Get the maximum value in the generated array
Leetcode 5561. Get the maximum value in the generated array
2020-11-10 10:41:00 【Oc'u bj12kvua】
List of articles
1. subject
Give you an integer n . According to the following rules to generate a length of n + 1 Array of nums :
nums[0] = 0nums[1] = 1When 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
Tips :
0 <= n <= 100
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/get-maximum-in-generated-array
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
2. Problem solving
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
my CSDN Blog address https://michael.blog.csdn.net/
Long click or sweep code pay attention to my official account (Michael amin ), Come on together 、 Learn together !

版权声明
本文为[Oc'u bj12kvua]所创,转载请带上原文链接,感谢
边栏推荐
猜你喜欢

csdn bug8:待加

吴恩达《Machine Learning》精炼笔记 4:神经网络基础 - 知乎

网络安全工程师演示:原来***是这样控制你的服务器的!(下)

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

监控系统选型,这篇不可不读!

Wu Enda's refining notes on machine learning 4: basis of neural network - Zhihu

Taulia launches international payment terms database

What does the mremote variable in servicemanagerproxy refer to?
![[paper reading notes] rosane, robust and scalable attributed network embedding for sparse networks](/img/32/7f28d78caa3cbc2b60cea26a768797.jpg)
[paper reading notes] rosane, robust and scalable attributed network embedding for sparse networks
![Collection of blockchain theory [31]](/img/3b/00bc81122d330c9d59909994e61027.jpg)
Collection of blockchain theory [31]
随机推荐
Thinking about competitive programming: myths and shocking facts
Multibank group announced record financial results with gross profit of $94 million in the first three quarters of 2020
learning to Estimate 3D Hand Pose from Single RGB Images论文理解
想花钱速学互联网行业,大概花两三个月的时间,出来好找工作吗
Solution of MAC terminal iterm2 supporting RZ and sz
高通骁龙875夺安卓处理器桂冠,但外挂5G基带成为它的弊病
[paper reading notes] large scale heterogeneous feature embedding
Promote China manufacturing upgrade, 3D visualization of production line in automobile assembly workshop
C + + STL container
CSDN bug6: to be added
坚持追查7年,近10亿美元比特币终被美国政府没收充公
用例子理解递归
腾讯云TBase在分布式HTAP领域的探索与实践
刷题到底有什么用?你这么刷题还真没用
SEO界,值得收藏的10条金玉良言有哪些?
网络安全工程师演示:原来***是这样控制你的服务器的!(下)
Leetcode 1-sum of two numbers
小度“破圈”提速,IoT迎来新故事
[paper reading notes] a multilayered informational random walk for attributed social network embedding
CSDN bug4: to be added