Skip to content

Commit 7a22e31

Browse files
committed
feat: add solutions to lc problem: No.2519
No.2519.Count the Number of K-Big Indices
1 parent be3f4a0 commit 7a22e31

File tree

6 files changed

+491
-8
lines changed

6 files changed

+491
-8
lines changed

solution/2500-2599/2519.Count the Number of K-Big Indices/README.md

+169-4
Original file line numberDiff line numberDiff line change
@@ -48,34 +48,199 @@
4848

4949
<!-- 这里可写通用的实现逻辑 -->
5050

51+
**方法一:树状数组**
52+
53+
维护两个树状数组,一个记录当前位置左边小于当前位置的数的个数,另一个记录当前位置右边小于当前位置的数的个数。
54+
55+
遍历数组,对于当前位置,如果左边小于当前位置的数的个数大于等于 $k$,且右边小于当前位置的数的个数大于等于 $k$,则当前位置是 $k-big$,答案加一。
56+
57+
时间复杂度 $O(n\log n)$,空间复杂度 $O(n)$。其中 $n$ 为数组长度。
58+
5159
<!-- tabs:start -->
5260

5361
### **Python3**
5462

5563
<!-- 这里可写当前语言的特殊实现逻辑 -->
5664

5765
```python
58-
66+
class BinaryIndexedTree:
67+
def __init__(self, n):
68+
self.n = n
69+
self.c = [0] * (n + 1)
70+
71+
def update(self, x, delta):
72+
while x <= self.n:
73+
self.c[x] += delta
74+
x += x & -x
75+
76+
def query(self, x):
77+
s = 0
78+
while x:
79+
s += self.c[x]
80+
x -= x & -x
81+
return s
82+
83+
84+
class Solution:
85+
def kBigIndices(self, nums: List[int], k: int) -> int:
86+
n = len(nums)
87+
tree1 = BinaryIndexedTree(n)
88+
tree2 = BinaryIndexedTree(n)
89+
for v in nums:
90+
tree2.update(v, 1)
91+
ans = 0
92+
for v in nums:
93+
tree2.update(v, -1)
94+
ans += tree1.query(v - 1) >= k and tree2.query(v - 1) >= k
95+
tree1.update(v, 1)
96+
return ans
5997
```
6098

6199
### **Java**
62100

63101
<!-- 这里可写当前语言的特殊实现逻辑 -->
64102

65103
```java
66-
104+
class BinaryIndexedTree {
105+
private int n;
106+
private int[] c;
107+
108+
public BinaryIndexedTree(int n) {
109+
this.n = n;
110+
c = new int[n + 1];
111+
}
112+
113+
public void update(int x, int delta) {
114+
while (x <= n) {
115+
c[x] += delta;
116+
x += x & -x;
117+
}
118+
}
119+
120+
public int query(int x) {
121+
int s = 0;
122+
while (x > 0) {
123+
s += c[x];
124+
x -= x & -x;
125+
}
126+
return s;
127+
}
128+
}
129+
130+
class Solution {
131+
public int kBigIndices(int[] nums, int k) {
132+
int n = nums.length;
133+
BinaryIndexedTree tree1 = new BinaryIndexedTree(n);
134+
BinaryIndexedTree tree2 = new BinaryIndexedTree(n);
135+
for (int v : nums) {
136+
tree2.update(v, 1);
137+
}
138+
int ans = 0;
139+
for (int v : nums) {
140+
tree2.update(v, -1);
141+
if (tree1.query(v - 1) >= k && tree2.query(v - 1) >= k) {
142+
++ans;
143+
}
144+
tree1.update(v, 1);
145+
}
146+
return ans;
147+
}
148+
}
67149
```
68150

69151
### **C++**
70152

71153
```cpp
72-
154+
class BinaryIndexedTree {
155+
public:
156+
BinaryIndexedTree(int _n) : n(_n), c(_n + 1) {}
157+
158+
void update(int x, int delta) {
159+
while (x <= n) {
160+
c[x] += delta;
161+
x += x & -x;
162+
}
163+
}
164+
165+
int query(int x) {
166+
int s = 0;
167+
while (x) {
168+
s += c[x];
169+
x -= x & -x;
170+
}
171+
return s;
172+
}
173+
174+
private:
175+
int n;
176+
vector<int> c;
177+
};
178+
179+
class Solution {
180+
public:
181+
int kBigIndices(vector<int>& nums, int k) {
182+
int n = nums.size();
183+
BinaryIndexedTree* tree1 = new BinaryIndexedTree(n);
184+
BinaryIndexedTree* tree2 = new BinaryIndexedTree(n);
185+
for (int& v : nums) {
186+
tree2->update(v, 1);
187+
}
188+
int ans = 0;
189+
for (int& v : nums) {
190+
tree2->update(v, -1);
191+
ans += tree1->query(v - 1) >= k && tree2->query(v - 1) >= k;
192+
tree1->update(v, 1);
193+
}
194+
return ans;
195+
}
196+
};
73197
```
74198
75199
### **Go**
76200
77201
```go
78-
202+
type BinaryIndexedTree struct {
203+
n int
204+
c []int
205+
}
206+
207+
func newBinaryIndexedTree(n int) *BinaryIndexedTree {
208+
c := make([]int, n+1)
209+
return &BinaryIndexedTree{n, c}
210+
}
211+
212+
func (this *BinaryIndexedTree) update(x, delta int) {
213+
for x <= this.n {
214+
this.c[x] += delta
215+
x += x & -x
216+
}
217+
}
218+
219+
func (this *BinaryIndexedTree) query(x int) int {
220+
s := 0
221+
for x > 0 {
222+
s += this.c[x]
223+
x -= x & -x
224+
}
225+
return s
226+
}
227+
228+
func kBigIndices(nums []int, k int) (ans int) {
229+
n := len(nums)
230+
tree1 := newBinaryIndexedTree(n)
231+
tree2 := newBinaryIndexedTree(n)
232+
for _, v := range nums {
233+
tree2.update(v, 1)
234+
}
235+
for _, v := range nums {
236+
tree2.update(v, -1)
237+
if tree1.query(v-1) >= k && tree2.query(v-1) >= k {
238+
ans++
239+
}
240+
tree1.update(v, 1)
241+
}
242+
return
243+
}
79244
```
80245

81246
### **...**

solution/2500-2599/2519.Count the Number of K-Big Indices/README_EN.md

+161-4
Original file line numberDiff line numberDiff line change
@@ -49,25 +49,182 @@
4949
### **Python3**
5050

5151
```python
52-
52+
class BinaryIndexedTree:
53+
def __init__(self, n):
54+
self.n = n
55+
self.c = [0] * (n + 1)
56+
57+
def update(self, x, delta):
58+
while x <= self.n:
59+
self.c[x] += delta
60+
x += x & -x
61+
62+
def query(self, x):
63+
s = 0
64+
while x:
65+
s += self.c[x]
66+
x -= x & -x
67+
return s
68+
69+
70+
class Solution:
71+
def kBigIndices(self, nums: List[int], k: int) -> int:
72+
n = len(nums)
73+
tree1 = BinaryIndexedTree(n)
74+
tree2 = BinaryIndexedTree(n)
75+
for v in nums:
76+
tree2.update(v, 1)
77+
ans = 0
78+
for v in nums:
79+
tree2.update(v, -1)
80+
ans += tree1.query(v - 1) >= k and tree2.query(v - 1) >= k
81+
tree1.update(v, 1)
82+
return ans
5383
```
5484

5585
### **Java**
5686

5787
```java
58-
88+
class BinaryIndexedTree {
89+
private int n;
90+
private int[] c;
91+
92+
public BinaryIndexedTree(int n) {
93+
this.n = n;
94+
c = new int[n + 1];
95+
}
96+
97+
public void update(int x, int delta) {
98+
while (x <= n) {
99+
c[x] += delta;
100+
x += x & -x;
101+
}
102+
}
103+
104+
public int query(int x) {
105+
int s = 0;
106+
while (x > 0) {
107+
s += c[x];
108+
x -= x & -x;
109+
}
110+
return s;
111+
}
112+
}
113+
114+
class Solution {
115+
public int kBigIndices(int[] nums, int k) {
116+
int n = nums.length;
117+
BinaryIndexedTree tree1 = new BinaryIndexedTree(n);
118+
BinaryIndexedTree tree2 = new BinaryIndexedTree(n);
119+
for (int v : nums) {
120+
tree2.update(v, 1);
121+
}
122+
int ans = 0;
123+
for (int v : nums) {
124+
tree2.update(v, -1);
125+
if (tree1.query(v - 1) >= k && tree2.query(v - 1) >= k) {
126+
++ans;
127+
}
128+
tree1.update(v, 1);
129+
}
130+
return ans;
131+
}
132+
}
59133
```
60134

61135
### **C++**
62136

63137
```cpp
64-
138+
class BinaryIndexedTree {
139+
public:
140+
BinaryIndexedTree(int _n) : n(_n), c(_n + 1) {}
141+
142+
void update(int x, int delta) {
143+
while (x <= n) {
144+
c[x] += delta;
145+
x += x & -x;
146+
}
147+
}
148+
149+
int query(int x) {
150+
int s = 0;
151+
while (x) {
152+
s += c[x];
153+
x -= x & -x;
154+
}
155+
return s;
156+
}
157+
158+
private:
159+
int n;
160+
vector<int> c;
161+
};
162+
163+
class Solution {
164+
public:
165+
int kBigIndices(vector<int>& nums, int k) {
166+
int n = nums.size();
167+
BinaryIndexedTree* tree1 = new BinaryIndexedTree(n);
168+
BinaryIndexedTree* tree2 = new BinaryIndexedTree(n);
169+
for (int& v : nums) {
170+
tree2->update(v, 1);
171+
}
172+
int ans = 0;
173+
for (int& v : nums) {
174+
tree2->update(v, -1);
175+
ans += tree1->query(v - 1) >= k && tree2->query(v - 1) >= k;
176+
tree1->update(v, 1);
177+
}
178+
return ans;
179+
}
180+
};
65181
```
66182
67183
### **Go**
68184
69185
```go
70-
186+
type BinaryIndexedTree struct {
187+
n int
188+
c []int
189+
}
190+
191+
func newBinaryIndexedTree(n int) *BinaryIndexedTree {
192+
c := make([]int, n+1)
193+
return &BinaryIndexedTree{n, c}
194+
}
195+
196+
func (this *BinaryIndexedTree) update(x, delta int) {
197+
for x <= this.n {
198+
this.c[x] += delta
199+
x += x & -x
200+
}
201+
}
202+
203+
func (this *BinaryIndexedTree) query(x int) int {
204+
s := 0
205+
for x > 0 {
206+
s += this.c[x]
207+
x -= x & -x
208+
}
209+
return s
210+
}
211+
212+
func kBigIndices(nums []int, k int) (ans int) {
213+
n := len(nums)
214+
tree1 := newBinaryIndexedTree(n)
215+
tree2 := newBinaryIndexedTree(n)
216+
for _, v := range nums {
217+
tree2.update(v, 1)
218+
}
219+
for _, v := range nums {
220+
tree2.update(v, -1)
221+
if tree1.query(v-1) >= k && tree2.query(v-1) >= k {
222+
ans++
223+
}
224+
tree1.update(v, 1)
225+
}
226+
return
227+
}
71228
```
72229

73230
### **...**

0 commit comments

Comments
 (0)