当前位置:网站首页>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)
边栏推荐
- Queue and stack
- [51nod p3058] Xiao ming'ai set [set]
- [51nod 2493] sum of binary distances [bit operation]
- LeetCode 6097. 替换字符后匹配(字典)
- 阿里高级专家剖析 | Protocol的标准设计
- MOOC week 8 programming exercise 1
- [51nod p2653] interval XOR [bit operation]
- Jenkins接入Openldap用戶認證
- VGA common resolution and calculation method
- LeetCode 6098. 统计得分小于 K 的子数组数目(前缀和+二分查找)
猜你喜欢

Tree and binary tree: basic operation and implementation of binary tree

Consolas-with-Yahei
![[51nod p3058] Xiao ming'ai set [set]](/img/a8/dbe6d4ed5881a757780700143e0526.jpg)
[51nod p3058] Xiao ming'ai set [set]
![[51nod p3111] xiaoming'ai intercepts [Las]](/img/39/2d75a289c715fd010bf400d6eace71.jpg)
[51nod p3111] xiaoming'ai intercepts [Las]

(dfs+ pruning + checkerboard problem +dood) acwing 843 N-queen problem

Exercise 8-3 rotate the array to the right (20 points)

(dp+ memory) acwing 901 skiing

【pytorch环境安装搭建】
![[51nod p2527] or and sum [bit operation]](/img/50/75ee9ee40ef0ca9b99e6900018b61a.jpg)
[51nod p2527] or and sum [bit operation]
![1-2 24:00 (20 points) [CSP certification true question]](/img/3b/fe2c0e46dca604e5906d9c5ceabbe3.jpg)
1-2 24:00 (20 points) [CSP certification true question]
随机推荐
acwing 790. The third root of a number (dichotomy)
(state compression dp+ binary) acwing 91 Shortest Hamilton path
1-4 message passing interface [CSP authentication]
LeetCode 6095. Strong password checker II
1-2 24:00 (20 points) [CSP certification true question]
【20220526】UE5.0.2 release d11782b
阿里高级专家剖析 | Protocol的标准设计
Exercise 8-3 rotate the array to the right (20 points)
[51nod p2106] an odd number of times [bit operation]
Standard template library (STL)
(tree DP) acwing 285 A dance without a boss
[ssl1271] sort I [heap]
LeetCode 202. Happy number
[51nod p3111] xiaoming'ai intercepts [Las]
Interrupt handling mechanism
[51nod p3216] Award [bit operation]
Musk's "meta universe" dream
(bfs) acwing 847. Hierarchy of points in the graph
Exploitation of competitive loopholes in attacking and defending world PWN play conditions
C # introductory series (XIII) -- getting to know the structure for the first time