Skip to content

Commit 12e3c4a

Browse files
committed
feat: add solutions to lc problem: No.2016
No.2016.Maximum Difference Between Increasing Elements
1 parent 03d9954 commit 12e3c4a

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed

solution/2000-2099/2016.Maximum Difference Between Increasing Elements/README.md

+34
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,40 @@
6969

7070
```
7171

72+
### **TypeScript**
73+
74+
```ts
75+
function maximumDifference(nums: number[]): number {
76+
const n = nums.length;
77+
let min = nums[0];
78+
let res = -1;
79+
for (let i = 1; i < n; i++) {
80+
res = Math.max(res, nums[i] - min);
81+
min = Math.min(min, nums[i]);
82+
}
83+
return res === 0 ? -1 : res;
84+
}
85+
```
86+
87+
### **Rust**
88+
89+
```rust
90+
impl Solution {
91+
pub fn maximum_difference(nums: Vec<i32>) -> i32 {
92+
let mut min = nums[0];
93+
let mut res = -1;
94+
for i in 1..nums.len() {
95+
res = res.max(nums[i] - min);
96+
min = min.min(nums[i]);
97+
}
98+
match res {
99+
0 => -1,
100+
_ => res,
101+
}
102+
}
103+
}
104+
```
105+
72106
### **...**
73107

74108
```

solution/2000-2099/2016.Maximum Difference Between Increasing Elements/README_EN.md

+34
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,40 @@ The maximum difference occurs with i = 0 and j = 3, nums[j] - nums[i] = 10 - 1 =
6262

6363
```
6464

65+
### **TypeScript**
66+
67+
```ts
68+
function maximumDifference(nums: number[]): number {
69+
const n = nums.length;
70+
let min = nums[0];
71+
let res = -1;
72+
for (let i = 1; i < n; i++) {
73+
res = Math.max(res, nums[i] - min);
74+
min = Math.min(min, nums[i]);
75+
}
76+
return res === 0 ? -1 : res;
77+
}
78+
```
79+
80+
### **Rust**
81+
82+
```rust
83+
impl Solution {
84+
pub fn maximum_difference(nums: Vec<i32>) -> i32 {
85+
let mut min = nums[0];
86+
let mut res = -1;
87+
for i in 1..nums.len() {
88+
res = res.max(nums[i] - min);
89+
min = min.min(nums[i]);
90+
}
91+
match res {
92+
0 => -1,
93+
_ => res,
94+
}
95+
}
96+
}
97+
```
98+
6599
### **...**
66100

67101
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
impl Solution {
2+
pub fn maximum_difference(nums: Vec<i32>) -> i32 {
3+
let mut min = nums[0];
4+
let mut res = -1;
5+
for i in 1..nums.len() {
6+
res = res.max(nums[i] - min);
7+
min = min.min(nums[i]);
8+
}
9+
match res {
10+
0 => -1,
11+
_ => res,
12+
}
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function maximumDifference(nums: number[]): number {
2+
const n = nums.length;
3+
let min = nums[0];
4+
let res = -1;
5+
for (let i = 1; i < n; i++) {
6+
res = Math.max(res, nums[i] - min);
7+
min = Math.min(min, nums[i]);
8+
}
9+
return res === 0 ? -1 : res;
10+
}

0 commit comments

Comments
 (0)