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
34
35
36
37
 class Solution:
def minDifference(self, nums: List[int], queries: List[List[int]]) > List[int]:
# Find the maximum number in nums
max_n = max(nums)
# Initialize variables to hold the answer and frequency tracking (freq_tracker)
answer = []
freq_tracker = [[0] * (max_n + 1)]
# Build the frequency tracking (freq_tracker) by iterating through nums
for n in nums:
current_freq = freq_tracker[1][:]
current_freq[n] += 1
freq_tracker.append(current_freq)
# Iterate through queries and compute differences
for start, end in queries:
# Find the differences between frequencies at positions [end+1] and [start]
differences = []
for freq_end, freq_start, index in zip(freq_tracker[end + 1], freq_tracker[start], range(max_n + 1)):
if freq_start != freq_end:
differences.append(index)
# Initialize a list to store the difference between adjacent elements
min_diff_list = []
for a, b in zip(differences, differences[1:]):
min_diff_list.append(b  a)
# If min_diff_list is empty, set min_diff to 1, else find the minimum value in min_diff_list
if min_diff_list:
min_diff = min(min_diff_list)
else:
min_diff = 1
answer.append(min_diff)
return answer
