Skip to content

Commit 6e01a04

Browse files
committed
feat: add solutions to lc problem: No.2104
No.2104.Sum of Subarray Ranges
1 parent 65a0f0c commit 6e01a04

File tree

4 files changed

+98
-2
lines changed

4 files changed

+98
-2
lines changed

solution/2100-2199/2104.Sum of Subarray Ranges/README.md

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,42 @@ func min(a, b int) int {
165165

166166
### **TypeScript**
167167

168-
<!-- 这里可写当前语言的特殊实现逻辑 -->
169-
170168
```ts
169+
function subArrayRanges(nums: number[]): number {
170+
const n = nums.length;
171+
let res = 0;
172+
for (let i = 0; i < n - 1; i++) {
173+
let min = nums[i];
174+
let max = nums[i];
175+
for (let j = i + 1; j < n; j++) {
176+
min = Math.min(min, nums[j]);
177+
max = Math.max(max, nums[j]);
178+
res += max - min;
179+
}
180+
}
181+
return res;
182+
}
183+
```
171184

185+
### **Rust**
186+
187+
```rust
188+
impl Solution {
189+
pub fn sub_array_ranges(nums: Vec<i32>) -> i64 {
190+
let n = nums.len();
191+
let mut res: i64 = 0;
192+
for i in 1..n {
193+
let mut min = nums[i - 1];
194+
let mut max = nums[i - 1];
195+
for j in i..n {
196+
min = min.min(nums[j]);
197+
max = max.max(nums[j]);
198+
res += (max - min) as i64;
199+
}
200+
}
201+
res
202+
}
203+
}
172204
```
173205

174206
### **...**

solution/2100-2199/2104.Sum of Subarray Ranges/README_EN.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,41 @@ func min(a, b int) int {
153153
### **TypeScript**
154154

155155
```ts
156+
function subArrayRanges(nums: number[]): number {
157+
const n = nums.length;
158+
let res = 0;
159+
for (let i = 0; i < n - 1; i++) {
160+
let min = nums[i];
161+
let max = nums[i];
162+
for (let j = i + 1; j < n; j++) {
163+
min = Math.min(min, nums[j]);
164+
max = Math.max(max, nums[j]);
165+
res += max - min;
166+
}
167+
}
168+
return res;
169+
}
170+
```
156171

172+
### **Rust**
173+
174+
```rust
175+
impl Solution {
176+
pub fn sub_array_ranges(nums: Vec<i32>) -> i64 {
177+
let n = nums.len();
178+
let mut res: i64 = 0;
179+
for i in 1..n {
180+
let mut min = nums[i - 1];
181+
let mut max = nums[i - 1];
182+
for j in i..n {
183+
min = min.min(nums[j]);
184+
max = max.max(nums[j]);
185+
res += (max - min) as i64;
186+
}
187+
}
188+
res
189+
}
190+
}
157191
```
158192

159193
### **...**
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
impl Solution {
2+
pub fn sub_array_ranges(nums: Vec<i32>) -> i64 {
3+
let n = nums.len();
4+
let mut res: i64 = 0;
5+
for i in 1..n {
6+
let mut min = nums[i - 1];
7+
let mut max = nums[i - 1];
8+
for j in i..n {
9+
min = min.min(nums[j]);
10+
max = max.max(nums[j]);
11+
res += (max - min) as i64;
12+
}
13+
}
14+
res
15+
}
16+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function subArrayRanges(nums: number[]): number {
2+
const n = nums.length;
3+
let res = 0;
4+
for (let i = 0; i < n - 1; i++) {
5+
let min = nums[i];
6+
let max = nums[i];
7+
for (let j = i + 1; j < n; j++) {
8+
min = Math.min(min, nums[j]);
9+
max = Math.max(max, nums[j]);
10+
res += max - min;
11+
}
12+
}
13+
return res;
14+
}

0 commit comments

Comments
 (0)