当前位置:网站首页>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] = 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
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).
边栏推荐
- Why use pycharm to run the use case successfully but cannot exit?
- flink sql-client 退出,表就会被清空怎么办?
- What should the future of the Internet be like when Silicon Valley employees flee the big factory and rush to Web3| Footprint Analytics
- How to store null value on the disk of yyds dry inventory?
- 油猴插件
- The latest analysis of R1 quick opening pressure vessel operation in 2022 and the examination question bank of R1 quick opening pressure vessel operation
- Rest reference
- js demo 計算本年度還剩下多少天
- 常用sql集合
- Global and Chinese market of AC induction motors 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢
Blue Bridge Cup Guoxin Changtian single chip microcomputer -- led lamp module (V)
2022 G3 boiler water treatment registration examination and G3 boiler water treatment examination papers
Code in keil5 -- use the code formatting tool astyle (plug-in)
仿网易云音乐小程序
Yyds dry inventory hcie security Day12: concept of supplementary package filtering and security policy
Asynchronous artifact: implementation principle and usage scenario of completable future
[dynamic programming] Ji Suan Ke: Suan tou Jun breaks through the barrier (variant of the longest increasing subsequence)
No more! Technical team members resign collectively
JS closure knowledge points essence
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
随机推荐
MySQL - idea connects to MySQL
Go language slice interview real question 7 consecutive questions
内存分析器 (MAT)
90 後,辭職創業,說要卷死雲數據庫
TiDB 之 TiCDC6.0 初体验
Investment analysis and prospect trend prediction report of China's boron nitride industry Ⓨ 2022 ~ 2028
Minio deployment
Tidb's initial experience of ticdc6.0
gslb(global server load balance)技术的一点理解
Global and Chinese market of AC induction motors 2022-2028: Research Report on technology, participants, trends, market size and share
常用sql集合
Electronic tube: Literature Research on basic characteristics of 6j1
油猴插件
Under the double reduction policy, research travel may become a big winner
Oil monkey plug-in
2022-02-15 Daily: 2022 AAAI fellow release
Awk getting started to proficient series - awk quick start
Analysis report on the development trend and Prospect of global and Chinese supercontinuum laser source industry Ⓚ 2022 ~ 2027
Yyds dry inventory Chapter 4 of getting started with MySQL: data types that can be stored in the data table
Blue Bridge Cup Guoxin Changtian single chip microcomputer -- led lamp module (V)