当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
猜你喜欢
MultiBank Group宣布创纪录的财务业绩,2020年前三季度毛利达到9,400万美元
Looking for a small immutable dictionary with better performance
Bartender2021实现安全远程标签打印,年终全新发布
On fedlearner, the latest open source federated machine learning platform of byte
Factory approach model
设计 API 时通过 POST 获取数据需要注意哪些问题
【高级测试工程师】新鲜出炉的三套价值18K的自动化测试面试(网易、字节跳动、美团)
Hystrix 如何解决 ThreadLocal 信息丢失
分布式文档存储数据库之MongoDB索引管理
LeetCode:数组(一)
随机推荐
What's the difference between delete, truncate, and drop, and what to do if you delete data by mistake
Wu Enda's refining notes on machine learning 4: basis of neural network - Zhihu
商品管统——采购需求合并到采购单
[paper reading notes] network embedding with attribute refinement
Factory approach model
港股上市公司移卡收购创信众42.5%股权 谋划加快营销服务布局
CSDN bug7: to be added
csdn bug5:待加
Problems and solutions in configuring FTP server with FileZilla server
jsliang 求职系列 - 09 - 手写浅拷贝和深拷贝
Taulia launches international payment terms database
安卓快速关机APP
Overview of the most complete anomaly detection algorithm in history
为什么要谨慎使用Arrays.asList、ArrayList的subList?
ElasticSearch 集群基本概念及常用操作汇总(建议收藏)
selenium webdriver使用click一直失效问题的几种解决方法
Call the open source video streaming media platform dawinffc
The high pass snapdragon 875 has won the title of Android processor, but the external 5g baseband has become its disadvantage
About CentOS start error: the solution of failed to start crash recovery kernel arming
专业之旅——GitHub 热点速览 Vol.45