-
Notifications
You must be signed in to change notification settings - Fork 41
/
solution.ts
33 lines (33 loc) · 1.03 KB
/
solution.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
function minDifference (nums: number[], queries: number[][]): number[] {
const prefixCounts:number[][] = new Array(101);
for (let i = 1; i < 101; i++) {
prefixCounts[i] = new Array(nums.length + 1).fill(0);
}
for (let i = 0; i < nums.length; i++) {
prefixCounts[nums[i]][i + 1] = 1;
}
for (let i = 1; i < 101; i++) {
for (let j = 1; j < nums.length + 1; j++) {
prefixCounts[i][j] += prefixCounts[i][j - 1];
}
}
const result = [];
for (const [l, r, ] of queries) {
let lastNum = 0;
let minDiff = Infinity;
for (let i = 1; i < 101; i++) {
const count = prefixCounts[i][r + 1] - prefixCounts[i][l];
if (count === 0) {
continue;
}
if (lastNum === 0) {
lastNum = i;
continue;
}
minDiff = Math.min(i - lastNum, minDiff);
lastNum = i;
}
result.push(minDiff === Infinity ? -1 : minDiff);
}
return result;
}