当前位置:网站首页>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)
边栏推荐
- Classes and objects -- polymorphic
- Britain introduces food security plan to resist food supply crisis
- [51nod p2653] interval XOR [bit operation]
- LeetCode 201. Digit range bitwise AND
- (bfs+GOOD) acwing 845. Eight digit
- C#入门系列(十三) -- 初识结构体
- Learning makefile with me
- Jenkins接入Openldap用户认证
- Queue and stack
- Classes and objects -- Inheritance
猜你喜欢
Acwing 787. Merge sort
Exercise 7-10 finding specified characters (15 points)
MySQL中redo日志和undo日志简述
[51nod p2106] an odd number of times [bit operation]
acwing 789. Range of numbers (dichotomy + suitable for understanding dichotomy boundary)
说说MySQL索引机制
[51nod p3395] n-bit gray code [bit operation]
Consolas-with-Yahei
二叉树简介
1-2 24:00 (20 points) [CSP certification true question]
随机推荐
Acwing785. quick sort (sort+ quick sort + merge sort)
[pytorch environment installation]
【工具链系列】 Notepad++
VGA常用分辨率及计算方法
Class template
Tree and binary tree: storage structure of binary tree
VGA common resolution and calculation method
Amadahl's Law (a little thought)
(bfs+GOOD) acwing 845. Eight digit
[51nod p3058] Xiao ming'ai set [set]
LeetCode 72. Edit distance
[51nod p2653] interval XOR [bit operation]
虚拟机内存结构简述
acwing 786. Number k
Tree and binary tree: operation and storage structure of tree
GCC compilation process
(dp+ memory) acwing 901 skiing
Jenkins access openldap user authentication
Remember! Don't be too confident in writing code! Be sure to write some key log info output, or the problem will not be located.
MySQL中redo日志和undo日志简述