当前位置:网站首页>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).
边栏推荐
- Preliminary understanding of C program design
- The latest analysis of crane driver (limited to bridge crane) in 2022 and the test questions and analysis of crane driver (limited to bridge crane)
- The 14th five year plan and investment feasibility study report of China's industry university research cooperation Ⓧ 2022 ~ 2028
- [SRS] build a specified version of SRS
- Minio deployment
- Is it safe and reliable to open an account and register for stock speculation? Is there any risk?
- Correlation
- Luogu deep foundation part 1 Introduction to language Chapter 6 string and file operation
- JS Demo calcule combien de jours il reste de l'année
- Solve the problem that openocd fails to burn STM32 and cannot connect through SWD
猜你喜欢

仿网易云音乐小程序

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

Common SQL sets

常用sql集合

Redis concludes that the second pipeline publishes / subscribes to bloom filter redis as a database and caches RDB AOF redis configuration files

MySQL——JDBC

Awk getting started to proficient series - awk quick start

Code in keil5 -- use the code formatting tool astyle (plug-in)

Blue Bridge Cup Guoxin Changtian MCU -- program download (III)

Data consistency between redis and database
随机推荐
90 后,辞职创业,说要卷死云数据库
gslb(global server load balance)技术的一点理解
90 後,辭職創業,說要卷死雲數據庫
TiDB 之 TiCDC6.0 初体验
Report on the development strategy of China's engineering bidding agency and suggestions for the 14th five year plan Ⓙ 2022 ~ 2028
A little understanding of GSLB (global server load balance) technology
gslb(global server load balance)技術的一點理解
What is the difference between res.send() and res.end() in the node express framework
Let me ask you a question. Have you ever used the asynchronous io of Flink SQL to associate dimension tables in MySQL? I set various settings according to the official website
Collection | pytoch common loss function disassembly
Teach you how to install aidlux (1 installation)
Under the double reduction policy, research travel may become a big winner
Oil monkey plug-in
Great gods, I want to send two broadcast streams: 1. Load basic data from MySQL and 2. Load changes in basic data from Kafka
JS closure knowledge points essence
Investment planning analysis and prospect prediction report of China's satellite application industry during the 14th five year plan Ⓑ 2022 ~ 2028
Bluebridge cup Guoxin Changtian single chip microcomputer -- detailed explanation of schematic diagram (IV)
Dahua series books
Correlation
Morning flowers and evening flowers