当前位置:网站首页>LeetCode 2016. Maximum difference between incremental elements
LeetCode 2016. Maximum difference between incremental elements
2022-06-13 09:40:00 【Python's path to becoming a God】
2016. The maximum difference between incremental elements Answer key
Title source :2016. The maximum difference between incremental elements
2022.02.26 A daily topic
Daily question column address :LeetCode The solution of one question every day is being updated ️
Today's topic is relatively simple , seek nums[j] - nums[i] The maximum difference between the two , And also need to meet nums[j] > nums[i] And j > i
Law 1 : Violent simulation
The first method is to simulate directly , Find the minimum
class Solution {
public:
int maximumDifference(vector<int>& nums) {
int len = nums.size();
int res = -1;
for (int i = 0; i < len; i++)
for (int j = i + 1; j < len; j++)
if (nums[j] > nums[i]) res = max(res, (nums[j] - nums[i]));
return res;
}
}
class Solution {
public int maximumDifference(int[] nums) {
int len = nums.length;
int res = -1;
for (int i = 0; i < len; i++)
for (int j = i + 1; j < len; j++)
if (nums[j] > nums[i]) res = Math.max(res, (nums[j] - nums[i]));
return res;
}
}
- Time complexity
O
(
n
2
)
O(n^2)
O(n2)
- Spatial complexity
O
(
1
)
O(1)
O(1)
Law two : Optimize
We need to find
n
u
m
[
j
]
−
n
u
m
[
i
]
num[j]-num[i]
num[j]−num[i] The maximum value of the difference , At the same time, we should satisfy
n
u
m
s
[
j
]
>
n
u
m
s
[
i
]
nums[j] > nums[i]
nums[j]>nums[i]
Then we can just traverse
j
j
j, then Mark
[
0
,
j
−
1
]
[0,j-1]
[0,j−1] Minimum of , And
n
u
m
[
j
]
num[j]
num[j] Make mistakes , Get the final result
class Solution {
public:
int maximumDifference(vector<int> &nums) {
// Define the result variable , The initial value is assigned to -1
// If there are no number pairs that meet the conditions, it will directly return -1
int res = -1;
// Make i stay [0,j) Within the scope of
int i = nums[0];
// Make j Traverse nums
for (int j = 1; j < nums.size(); j++)
// If meet nums[j] > nums[i]
// Just do bad and res Compare and take the maximum value
if (nums[j] > i)
res = max(res, (nums[j] - i));
else
// If not satisfied nums[j] > nums[i]
// Just update i Value
i = nums[j];
return res;
}
};
class Solution {
public int maximumDifference(int[] nums) {
// Define the result variable , The initial value is assigned to -1
// If there are no number pairs that meet the conditions, it will directly return -1
int res = -1;
// Make i stay [0,j) Within the scope of
int i = nums[0];
// Make j Traverse nums
for (int j = 1; j < nums.length; j++)
// If meet nums[j] > nums[i]
// Just do bad and res Compare and take the maximum value
if (nums[j] > i) res = Math.max(res, (nums[j] - i));
else
// If not satisfied nums[j] > nums[i]
// Just update i Value
i = nums[j];
return res;
}
}
- Time complexity
O
(
n
)
O(n)
O(n)
- Spatial complexity
O
(
1
)
O(1)
O(1)
边栏推荐
- LeetCode 6097. 替换字符后匹配(字典)
- Class and object -- friend
- [most comprehensive and detailed explanation] detailed explanation of knapsack problem
- GCC compilation process
- 英国出台粮食安全计划抵御粮食供应危机
- MySQL事务隔离级别和MVCC
- Attack and defense world PWN shell
- ROS2之OpenCV人脸识别foxy~galactic~humble
- @Value不生效及extend/implement其他类无法注入bean的手动注入
- 【动态规划】入门篇
猜你喜欢
Alibaba senior experts analyze the standard design of protocol
The turtle library displays the system time
[Luogu p1403] Research on divisor
Tree and binary tree: storage structure of binary tree
C language: file operation
Can the operation of the new BMW I3 meet the expectations of the famous products of the 3 series?
Tree and binary tree: operation and storage structure of tree
多线程 从UE4的无锁队列开始 (线程安全)
【20220526】UE5.0.2 release d11782b
[pytorch environment installation]
随机推荐
关于指令集位数,指令构架位数简述
LeetCode 202. Happy number
【工具链系列】 Notepad++
Simple use of spiel expressions
Attack and defense world PWN shell
虚拟化和云计算文章大合集
[51nod p3395] n-bit gray code [bit operation]
Jenkins accédant à l'authentification de l'utilisateur openldap
(dijkstra+ shortest path + edge traversal 0 (m)) acwing 850 Dijkstra finding the shortest path II
go-zero微服务实战系列(三、API定义和表结构设计)
Calculate the number of days between two times (supports cross month and cross year)
MySQL中redo日志和undo日志简述
(bfs+GOOD) acwing 845. Eight digit
LeetCode 5259. Calculate the total tax payable
(dfs+ pruning + checkerboard problem +dood) acwing 843 N-queen problem
[ssl1280] full arrangement
虚拟机内存结构简述
UNIX Environment advanced programming --8- process control ---8.5 function exit-8.6 function wait and waitpid
Tree and binary tree: operation and storage structure of tree
Zigzag transformation