[LeetCode] Find X-Sum of All K-Long Subarrays I

3318. Find X-Sum of All K-Long Subarrays I

You are given an array nums of n integers and two integers k and x.

The x-sum of an array is calculated by the following procedure:

  • Count the occurrences of all elements in the array.
  • Keep only the occurrences of the top x most frequent elements. If two elements have the same number of occurrences, the element with the bigger value is considered more frequent.
  • Calculate the sum of the resulting array.

Note that if an array has less than x distinct elements, its x-sum is the sum of the array.

Return an integer array answer of length n - k + 1 where answer[i] is the x-sum of the subarray nums[i..i + k - 1].

A subarray is a contiguous non-empty sequence of elements within an array.

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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
struct Node {
Node *l, *r, *p;
pair<long long, long long> key;
long long sum, subset;
Node(pair<long long, long long> key, Node* p) : key(key), sum(key.first * key.second), subset(1), l(nullptr), r(nullptr), p(p) {}
void update() {
subset = 1;
sum = key.first * key.second;
if(l) subset += l->subset, sum += l->sum;
if(r) subset += r->subset, sum += r->sum;
}
};

struct SplayTree {
Node* tree;
SplayTree() : tree(nullptr) {
insert({INT_MIN, 0});
insert({INT_MAX, 0});
}

void rotate(Node *x) {
Node *p = x->p, *b;
if (x == p->l) {
p->l = b = x->r;
x->r = p;
} else {
p->r = b = x->l;
x->l = p;
}
x->p = p->p;
p->p = x;
if (b) b->p = p;
(x->p ? p == x->p->l ? x->p->l : x->p->r : tree) = x;
p->update();
x->update();
}

void splay(Node *x, Node *g = nullptr) {
Node* y;
while(x->p != g){
Node* p = x->p;
if(p->p == g){
rotate(x); break;
}
auto pp = p->p;
if((p->l == x) == (pp->l == p)){
rotate(p); rotate(x);
} else {
rotate(x); rotate(x);
}
}
if(!g) tree = x;
}
void insert(pair<long long, long long> key) {
if(!tree) {
tree = new Node(key, nullptr);
return;
}
Node *p = tree, **pp;
while (1) {
if (key == p->key) return;
if (key < p->key) {
if (!p->l) {
pp = &p->l;
break;
}
p = p->l;
} else {
if (!p->r) {
pp = &p->r;
break;
}
p = p->r;
}
}
*pp = new Node(key, p);
splay(*pp);
}
bool find(pair<long long, long long> key) {
Node *p = tree;
if (!p) return false;
while (p) {
if (key == p->key) break;
if (key < p->key) {
if (!p->l) break;
p = p->l;
} else {
if (!p->r) break;
p = p->r;
}
}
splay(p);
return key == p->key;
}

void remove(pair<long long, long long> key) {
if (!find(key)) return;
Node *p = tree;
if (p->l and p->r) {
tree = p->l, tree->p = nullptr;
Node *x = tree;
while (x->r) x = x->r;
x->r = p->r, p->r->p = x;
splay(x), tree->update();
delete p; return;
}
if(p->l) {
tree = p->l, tree->p = nullptr;
tree->update();
delete p; return;
}
if (p->r) {
tree = p->r, tree->p = nullptr;
tree->update();
delete p; return;
}
delete p;
tree = nullptr;
}

void kth(int k) {
Node* x = tree;
while(1){
while(x->l && x->l->subset > k) x = x->l;
if(x->l) k -= x->l->subset;
if(!k--) break;
x = x->r;
}
splay(x);
}

Node* gather(int l, int r) {
l += 1, r += 1;
kth(r+1);
Node* x = tree;
kth(l-1);
splay(x, tree);
return tree->r->l;
}

long long sum(int l, int r) {
return gather(l,r)->sum;
}
};

class Streamer {
deque<int> streams;
SplayTree* st;
unordered_map<long long, long long> freq;
int limit, x;
public:
Streamer(int k, int x) : limit(k), x(x), st(new SplayTree()) {}

void update(int x, int operation) {
if(freq.count(x)) {
st->remove({freq[x], x});
}
freq[x] += operation;
if(freq[x] == 0) freq.erase(x);
else st->insert({freq[x], x});
}

long long query(int n) {
streams.push_back(n);
update(streams.back(),1);

if(streams.size() > limit) {
update(streams.front(), -1);
streams.pop_front();
}
if(streams.size() != limit) return -1;
if(freq.size() <= x) return st->tree->sum;
return st->sum(freq.size() - x, freq.size() - 1);
}
};
class Solution {
public:
vector<int> findXSum(vector<int>& nums, int k, int x) {
Streamer* s = new Streamer(k,x);
vector<int> res;
for(int i = 0; i < nums.size(); i++) {
long long now = s->query(nums[i]);
if(now != -1) res.push_back(now);
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2024/10/13/PS/LeetCode/find-x-sum-of-all-k-long-subarrays-i/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.