forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminimum-absolute-difference-queries.py
56 lines (51 loc) · 1.7 KB
/
minimum-absolute-difference-queries.py
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# Time: O(r * (n + q)), r is the max of nums
# Space: O(r * n)
class Solution(object):
def minDifference(self, nums, queries):
"""
:type nums: List[int]
:type queries: List[List[int]]
:rtype: List[int]
"""
INF = float("inf")
prefix = [[0]*(max(nums)+1)]
for num in nums:
prefix.append(prefix[-1][:])
prefix[-1][num] += 1
result = []
for l, r in queries:
min_diff, prev = INF, -1
for num in xrange(len(prefix[0])):
if not (prefix[l][num] < prefix[r+1][num]):
continue
if prev != -1:
min_diff = min(min_diff, num-prev)
prev = num
result.append(min_diff if min_diff != INF else -1)
return result
# Time: O(r + n + q * r * logn), r is the max of nums
# Space: O(r + n)
import bisect
class Solution2(object):
def minDifference(self, nums, queries):
"""
:type nums: List[int]
:type queries: List[List[int]]
:rtype: List[int]
"""
INF = float("inf")
idxs = [[] for _ in xrange(max(nums)+1)]
for i, num in enumerate(nums):
idxs[num].append(i)
result = []
for l, r in queries:
min_diff, prev = INF, -1
for num in xrange(len(idxs)):
i = bisect.bisect_left(idxs[num], l)
if not (i < len(idxs[num]) and idxs[num][i] <= r):
continue
if prev != -1:
min_diff = min(min_diff, num-prev)
prev = num
result.append(min_diff if min_diff != INF else -1)
return result