当前位置:网站首页>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).
边栏推荐
- How does sentinel, a traffic management artifact, make it easy for business parties to access?
- (5) User login - services and processes - History Du touch date stat CP
- treevalue——Master Nested Data Like Tensor
- js demo 計算本年度還剩下多少天
- Investment analysis and prospect trend prediction report of China's boron nitride industry Ⓨ 2022 ~ 2028
- A little understanding of GSLB (global server load balance) technology
- 6.0 kernel driver character driver
- Analysis report on the development trend and Prospect of global and Chinese supercontinuum laser source industry Ⓚ 2022 ~ 2027
- Dahua series books
- The 14th five year plan and investment feasibility study report of China's industry university research cooperation Ⓧ 2022 ~ 2028
猜你喜欢

2022 G3 boiler water treatment registration examination and G3 boiler water treatment examination papers

2022 safety officer-a certificate registration examination and summary of safety officer-a certificate examination

Teach you how to install aidlux (1 installation)

Decompile and modify the non source exe or DLL with dnspy
Implementation principle of inheritance, encapsulation and polymorphism

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

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

Imitation Netease cloud music applet

(5) User login - services and processes - History Du touch date stat CP

Control loop of program (while loop)
随机推荐
regular expression
MySQL - idea connects to MySQL
90 後,辭職創業,說要卷死雲數據庫
English topic assignment (28)
Correlation
UC Berkeley proposes a multitask framework slip
How to install sentinel console
The White House held an open source security summit, attended by many technology giants
On my first day at work, this API timeout optimization put me down!
An expression that regularly matches one of two strings
鹏城杯 WEB_WP
IPhone development swift foundation 09 assets
Base ring tree Cartesian tree
China's TPMS industry demand forecast and future development trend analysis report Ⓐ 2022 ~ 2028
gslb(global server load balance)技术的一点理解
Awk getting started to proficient series - awk quick start
Preliminary analysis of smart microwave radar module
What should the future of the Internet be like when Silicon Valley employees flee the big factory and rush to Web3| Footprint Analytics
Yyds dry inventory hcie security Day12: concept of supplementary package filtering and security policy
js demo 計算本年度還剩下多少天