当前位置:网站首页>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).
边栏推荐
- WFC900M-Network_ Card/Qualcomm-Atheros-AR9582-2T-2R-MIMO-802.11-N-900M-high-power-Mini-PCIe-Wi-Fi-Mod
- What is the difference between res.send() and res.end() in the node express framework
- C程序设计的初步认识
- The White House held an open source security summit, attended by many technology giants
- Netfilter ARP log
- treevalue——Master Nested Data Like Tensor
- Leetcode problem solving - 235 Nearest common ancestor of binary search tree
- 使用dnSpy對無源碼EXE或DLL進行反編譯並且修改
- JS notes (III)
- gslb(global server load balance)技術的一點理解
猜你喜欢
Farmersworld farmers world, no faith, how to talk about success?
MySQL——JDBC
6.0 kernel driver character driver
IPhone development swift foundation 09 assets
The latest analysis of R1 quick opening pressure vessel operation in 2022 and the examination question bank of R1 quick opening pressure vessel operation
Tidb's initial experience of ticdc6.0
On my first day at work, this API timeout optimization put me down!
[SRS] build a specified version of SRS
Bluebridge cup Guoxin Changtian single chip microcomputer -- hardware environment (I)
Nacos common configuration
随机推荐
Yyds dry inventory Chapter 4 of getting started with MySQL: data types that can be stored in the data table
十大券商开户注册安全靠谱吗?有没有风险的?
Dahua series books
Luogu deep foundation part 1 Introduction to language Chapter 7 functions and structures
Idea shortcut word operation
The latest analysis of R1 quick opening pressure vessel operation in 2022 and the examination question bank of R1 quick opening pressure vessel operation
[SRS] build a specified version of SRS
Pengcheng cup Web_ WP
Awk getting started to proficient series - awk quick start
Is the account opening of Guotai Junan Securities safe and reliable? How to open Guotai Junan Securities Account
How PHP gets all method names of objects
Analysis report on the development trend and Prospect of global and Chinese supercontinuum laser source industry Ⓚ 2022 ~ 2027
WFC900M-Network_ Card/Qualcomm-Atheros-AR9582-2T-2R-MIMO-802.11-N-900M-high-power-Mini-PCIe-Wi-Fi-Mod
What should the future of the Internet be like when Silicon Valley employees flee the big factory and rush to Web3| Footprint Analytics
js demo 计算本年度还剩下多少天
Exclusive interview with the person in charge of openkruise: to what extent has cloud native application automation developed now?
Analysis report on the development prospect and investment strategy of global and Chinese modular automation systems Ⓟ 2022 ~ 2027
Asynchronous artifact: implementation principle and usage scenario of completable future
Data consistency between redis and database
Tkinter Huarong Road 4x4 tutorial III