Skip to content

Commit f603b42

Browse files
authored
feat: add typescript solution to lc problem: No.1906.Minimum Absolute Difference Queries (#471)
1 parent aa781f3 commit f603b42

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

solution/1900-1999/1906.Minimum Absolute Difference Queries/README.md

+33
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,39 @@ class Solution {
141141
}
142142
```
143143

144+
### **TypeScript**
145+
146+
```ts
147+
function minDifference(nums: number[], queries: number[][]): number[] {
148+
let m = nums.length, n = queries.length;
149+
let max = 100;
150+
// let max = Math.max(...nums);
151+
let pre: number[][] = [];
152+
pre.push(new Array(max + 1).fill(0));
153+
for (let i = 0; i < m; ++i) {
154+
let num = nums[i];
155+
pre.push(pre[i].slice());
156+
pre[i + 1][num] += 1;
157+
}
158+
159+
let ans = [];
160+
for (let [left, right] of queries) {
161+
let last = -1;
162+
let min = Infinity;
163+
for (let j = 1; j < max + 1; ++j) {
164+
if (pre[left][j] < pre[right + 1][j]) {
165+
if (last != -1) {
166+
min = Math.min(min, j - last);
167+
}
168+
last = j;
169+
}
170+
}
171+
ans.push(min == Infinity ? -1 : min);
172+
}
173+
return ans;
174+
};
175+
```
176+
144177
### **C++**
145178

146179
```cpp

solution/1900-1999/1906.Minimum Absolute Difference Queries/README_EN.md

+33
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,39 @@ class Solution {
129129
}
130130
```
131131

132+
### **TypeScript**
133+
134+
```ts
135+
function minDifference(nums: number[], queries: number[][]): number[] {
136+
let m = nums.length, n = queries.length;
137+
let max = 100;
138+
// let max = Math.max(...nums);
139+
let pre: number[][] = [];
140+
pre.push(new Array(max + 1).fill(0));
141+
for (let i = 0; i < m; ++i) {
142+
let num = nums[i];
143+
pre.push(pre[i].slice());
144+
pre[i + 1][num] += 1;
145+
}
146+
147+
let ans = [];
148+
for (let [left, right] of queries) {
149+
let last = -1;
150+
let min = Infinity;
151+
for (let j = 1; j < max + 1; ++j) {
152+
if (pre[left][j] < pre[right + 1][j]) {
153+
if (last != -1) {
154+
min = Math.min(min, j - last);
155+
}
156+
last = j;
157+
}
158+
}
159+
ans.push(min == Infinity ? -1 : min);
160+
}
161+
return ans;
162+
};
163+
```
164+
132165
### **C++**
133166

134167
```cpp
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
function minDifference(nums: number[], queries: number[][]): number[] {
2+
let m = nums.length, n = queries.length;
3+
let max = 100;
4+
// let max = Math.max(...nums);
5+
let pre: number[][] = [];
6+
pre.push(new Array(max + 1).fill(0));
7+
for (let i = 0; i < m; ++i) {
8+
let num = nums[i];
9+
pre.push(pre[i].slice());
10+
pre[i + 1][num] += 1;
11+
}
12+
13+
let ans = [];
14+
for (let [left, right] of queries) {
15+
let last = -1;
16+
let min = Infinity;
17+
for (let j = 1; j < max + 1; ++j) {
18+
if (pre[left][j] < pre[right + 1][j]) {
19+
if (last != -1) {
20+
min = Math.min(min, j - last);
21+
}
22+
last = j;
23+
}
24+
}
25+
ans.push(min == Infinity ? -1 : min);
26+
}
27+
return ans;
28+
};

0 commit comments

Comments
 (0)