当前位置:网站首页>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] = 0
nums[1] = 1
When 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]所创,转载请带上原文链接,感谢
边栏推荐
- iOS14新特性-WidgetKit开发与实践
- 他把闲鱼APP长列表流畅度翻了倍
- mac终端Iterm2支持rz和sz的解决方案
- leetcode1-两数之和
- 腾讯云TBase在分布式HTAP领域的探索与实践
- 2020-11-07
- C语言使用随机数生成矩阵,实现三元组的快速转置。
- 中小企业为什么要用CRM系统
- 吴恩达《Machine Learning》精炼笔记 4:神经网络基础 - 知乎
- The high pass snapdragon 875 has won the title of Android processor, but the external 5g baseband has become its disadvantage
猜你喜欢
随机推荐
CSDN bug9: to be added
工厂方法模式
After seven years of pursuing, nearly one billion US dollars of bitcoin was eventually confiscated and confiscated by the US government
区块链论文集【三十一】
selenium webdriver使用click一直失效问题的几种解决方法
Collection of blockchain theory [31]
2020-11-07
Android quick shutdown app
SEO界,值得收藏的10条金玉良言有哪些?
【iOS】苹果登录Sign in with Apple
奸商加价销售mate40,小米可望在高端手机市场夺取更多市场
Looking for a small immutable dictionary with better performance
js数组常用技巧
Swoole 如何使用 Xdebug 进行单步调试
express -- 学习笔记(慕课)
用python猜测一个数字是集合里面哪些数字相加求和而来的
mac终端Iterm2支持rz和sz的解决方案
I have a crossed translation tool in my hand!
B. protocal has 7000eth assets in one week!
Bartender2021 realizes secure remote label printing, new year-end release