当前位置:网站首页>LeetCode 1646. Get the maximum value in the generated array
LeetCode 1646. Get the maximum value in the generated array
2022-07-03 22:07:00 【Daylight629】
1646. Get the maximum value in the generated array
Give you an integer n . According to the following rules to generate a length of n + 1 Array of nums :
nums[0] = 0nums[1] = 1- When
2 <= 2 * i <= nwhen ,nums[2 * i] = nums[i] - When
2 <= 2 * i + 1 <= nwhen ,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
Two 、 Method 1
simulation
class Solution {
public int getMaximumGenerated(int n) {
if (n == 0) {
return 0;
}
int[] res = new int[n + 1];
res[1] = 1;
for (int i = 0; i < n; i++) {
if (2 * i <= n) res[2 * i] = res[i];
if (2 * i + 1 <= n) res[2 * i + 1] = res[i] + res[i + 1];
}
return Arrays.stream(res).max().getAsInt();
}
}
Complexity analysis
Time complexity :O(n).
Spatial complexity :O(n).
边栏推荐
- Kali2021.4a build PWN environment
- Investment analysis and prospect trend prediction report of China's boron nitride industry Ⓨ 2022 ~ 2028
- How PHP adds two numbers
- Sed、Awk
- Getting started with DOM
- JS notes (III)
- Great gods, I want to send two broadcast streams: 1. Load basic data from MySQL and 2. Load changes in basic data from Kafka
- No more! Technical team members resign collectively
- [sg function]split game (2020 Jiangxi university student programming competition)
- 2022 safety officer-a certificate registration examination and summary of safety officer-a certificate examination
猜你喜欢

Nacos common configuration

Why use pycharm to run the use case successfully but cannot exit?

QGIS grid processing DEM data reclassification

What indicators should be paid attention to in current limit monitoring?

6.0 kernel driver character driver

Blue Bridge Cup Guoxin Changtian single chip microcomputer -- software environment (II)

MySQL - idea connects to MySQL

TiDB 之 TiCDC6.0 初体验

Covariance

pivot ROP Emporium
随机推荐
仿网易云音乐小程序
Station B, dark horse programmer, employee management system, access conflict related (there is an unhandled exception at 0x00007ff633a4c54d (in employee management system.Exe): 0xc0000005: read locat
What is the content of the securities practice examination?
Exclusive interview with the person in charge of openkruise: to what extent has cloud native application automation developed now?
On my first day at work, this API timeout optimization put me down!
Data consistency between redis and database
Cognitive fallacy: Wittgenstein's ruler
Decompile and modify the non source exe or DLL with dnspy
Décompiler et modifier un exe ou une DLL non source en utilisant dnspy
Bluebridge cup Guoxin Changtian single chip microcomputer -- hardware environment (I)
What should the future of the Internet be like when Silicon Valley employees flee the big factory and rush to Web3| Footprint Analytics
Exness: the Central Bank of England will raise interest rates again in March, and inflation is coming
[SRS] build a specified version of SRS
Dynamic research and future planning analysis report of China's urban water supply industry Ⓝ 2022 ~ 2028
js demo 计算本年度还剩下多少天
Report on the current situation and development trend of ethoxylated sodium alkyl sulfate industry in the world and China Ⓞ 2022 ~ 2027
抓包整理外篇——————autoResponder、composer 、statistics [ 三]
Sed、Awk
Report on the development status and investment planning trends of China's data center industry Ⓡ 2022 ~ 2028
C程序设计的初步认识