当前位置:网站首页>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).
边栏推荐
- Rest reference
- What should the future of the Internet be like when Silicon Valley employees flee the big factory and rush to Web3| Footprint Analytics
- WFC900M-Network_ Card/Qualcomm-Atheros-AR9582-2T-2R-MIMO-802.11-N-900M-high-power-Mini-PCIe-Wi-Fi-Mod
- China's TPMS industry demand forecast and future development trend analysis report Ⓐ 2022 ~ 2028
- Nacos common configuration
- 常用sql集合
- Preliminary analysis of smart microwave radar module
- regular expression
- Decompile and modify the non source exe or DLL with dnspy
- 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)
猜你喜欢

pivot ROP Emporium

No more! Technical team members resign collectively

QGIS grid processing DEM data reclassification

Introduction to kubernetes

How PHP drives mongodb

Data consistency between redis and database

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

Decompile and modify the non source exe or DLL with dnspy

Exclusive interview with the person in charge of openkruise: to what extent has cloud native application automation developed now?

gslb(global server load balance)技术的一点理解
随机推荐
Miscellaneous things that don't miss the right business
Are the top ten securities companies safe to open accounts and register? Is there any risk?
Cesium terrain clipping draw polygon clipping
Plug - in Oil Monkey
Cognitive fallacy: what is Fredkin's paradox
Rest参考
Correlation
Bluebridge cup Guoxin Changtian single chip microcomputer -- detailed explanation of schematic diagram (IV)
Decompile and modify the non source exe or DLL with dnspy
Analysis report on the development prospect and investment strategy of global and Chinese modular automation systems Ⓟ 2022 ~ 2027
[dynamic planning] counting garlic customers: the log of garlic King (the longest increasing public subsequence)
On my first day at work, this API timeout optimization put me down!
QGIS grid processing DEM data reclassification
2022 free examination questions for safety management personnel of hazardous chemical business units and reexamination examination for safety management personnel of hazardous chemical business units
Bluebridge cup Guoxin Changtian single chip microcomputer -- hardware environment (I)
Redis concludes that the second pipeline publishes / subscribes to bloom filter redis as a database and caches RDB AOF redis configuration files
Cognitive fallacy: what is dimensional curse
Capturing and sorting out external articles -- autoresponder, composer, statistics [III]
Memory analyzer (MAT)
Under the double reduction policy, research travel may become a big winner