-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2747-CountZeroRequestServers.go
More file actions
124 lines (112 loc) · 4.48 KB
/
2747-CountZeroRequestServers.go
File metadata and controls
124 lines (112 loc) · 4.48 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package main
// 2747. Count Zero Request Servers
// You are given an integer n denoting the total number of servers and a 2D 0-indexed integer array logs, where logs[i] = [server_id, time] denotes that the server with id server_id received a request at time time.
// You are also given an integer x and a 0-indexed integer array queries.
// Return a 0-indexed integer array arr of length queries.length where arr[i] represents the number of servers that did not receive any requests during the time interval [queries[i] - x, queries[i]].
// Note that the time intervals are inclusive.
// Example 1:
// Input: n = 3, logs = [[1,3],[2,6],[1,5]], x = 5, queries = [10,11]
// Output: [1,2]
// Explanation:
// For queries[0]: The servers with ids 1 and 2 get requests in the duration of [5, 10]. Hence, only server 3 gets zero requests.
// For queries[1]: Only the server with id 2 gets a request in duration of [6,11]. Hence, the servers with ids 1 and 3 are the only servers that do not receive any requests during that time period.
// Example 2:
// Input: n = 3, logs = [[2,4],[2,1],[1,2],[3,1]], x = 2, queries = [3,4]
// Output: [0,1]
// Explanation:
// For queries[0]: All servers get at least one request in the duration of [1, 3].
// For queries[1]: Only server with id 3 gets no request in the duration [2,4].
// Constraints:
// 1 <= n <= 10^5
// 1 <= logs.length <= 10^5
// 1 <= queries.length <= 10^5
// logs[i].length == 2
// 1 <= logs[i][0] <= n
// 1 <= logs[i][1] <= 10^6
// 1 <= x <= 10^5
// x < queries[i] <= 10^6
import "fmt"
import "sort"
func countServers(n int, logs [][]int, x int, queries []int) []int {
sort.Slice(logs, func(i, j int) bool {
return logs[i][1] < logs[j][1]
})
arr := make([][]int,len(queries))
for i ,q := range queries {
arr[i] = []int{ q, i }
}
sort.Slice(arr, func(i, j int) bool {
return arr[i][0] < arr[j][0]
})
res, count := make([]int,len(queries)), make(map[int]int)
i, j := 0, 0
for _, pair := range arr{
q, pos := pair[0], pair[1]
left, right := q - x, q
for j < len(logs) && logs[j][1] <= right{
count[logs[j][0]]++
j++
}
for i < len(logs) && logs[i][1] < left{
count[logs[i][0]]--
if count[logs[i][0]] <= 0 {
delete(count, logs[i][0])
}
i++
}
res[pos] = n - len(count)
}
return res
}
func countServers1(n int, logs [][]int, x int, queries []int) []int {
m := len(queries)
for i := range queries {
queries[i] = queries[i] << 32 | i
}
sort.Ints(queries)
res := make([]int, m)
sort.Slice(logs, func(i, j int) bool {
return logs[i][1] < logs[j][1]
})
l, r := 0, 0
mp, count := make([]int, n + 1), 0
for _, q := range queries {
qv, i := q >> 32, q & (1 << 32 - 1)
// 维护右侧
for ; r < len(logs) && logs[r][1] <= qv; r ++ {
server_id := logs[r][0]
mp[server_id]++
if mp[server_id] == 1 {
count++
}
}
// 维护左侧
for ; l < r && logs[l][1] < qv - x; l ++ {
server_id := logs[l][0]
mp[server_id]--
if mp[server_id] == 0 {
count--
}
}
res[i] = n - count
}
return res
}
func main() {
// Example 1:
// Input: n = 3, logs = [[1,3],[2,6],[1,5]], x = 5, queries = [10,11]
// Output: [1,2]
// Explanation:
// For queries[0]: The servers with ids 1 and 2 get requests in the duration of [5, 10]. Hence, only server 3 gets zero requests.
// For queries[1]: Only the server with id 2 gets a request in duration of [6,11]. Hence, the servers with ids 1 and 3 are the only servers that do not receive any requests during that time period.
fmt.Println(countServers(3, [][]int{{1,3},{2,6},{1,5}}, 5, []int{10,11})) // [1,2]
// Example 2:
// Input: n = 3, logs = [[2,4],[2,1],[1,2],[3,1]], x = 2, queries = [3,4]
// Output: [0,1]
// Explanation:
// For queries[0]: All servers get at least one request in the duration of [1, 3].
// For queries[1]: Only server with id 3 gets no request in the duration [2,4].
fmt.Println(countServers(3, [][]int{{2,4},{2,1},{1,2},{3,1}}, 2, []int{3,4})) // [0,1]
fmt.Println(countServers1(3, [][]int{{1,3},{2,6},{1,5}}, 5, []int{10,11})) // [1,2]
fmt.Println(countServers1(3, [][]int{{2,4},{2,1},{1,2},{3,1}}, 2, []int{3,4})) // [0,1]
}