Skip to content

Commit ee9380b

Browse files
committed
feat: add solutions for lc No.3907
1 parent e0be37f commit ee9380b

13 files changed

Lines changed: 773 additions & 4 deletions

File tree

solution/2000-2099/2078.Two Furthest Houses With Different Colors/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ function maxDistance(colors: number[]): number {
182182
j--;
183183
}
184184
return Math.max(n - i - 1, j);
185-
};
185+
}
186186
```
187187

188188
#### Rust

solution/2000-2099/2078.Two Furthest Houses With Different Colors/README_EN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ function maxDistance(colors: number[]): number {
176176
j--;
177177
}
178178
return Math.max(n - i - 1, j);
179-
};
179+
}
180180
```
181181

182182
#### Rust

solution/2000-2099/2078.Two Furthest Houses With Different Colors/Solution.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ function maxDistance(colors: number[]): number {
1111
j--;
1212
}
1313
return Math.max(n - i - 1, j);
14-
};
14+
}
Lines changed: 297 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,297 @@
1+
---
2+
comments: true
3+
difficulty: 中等
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3900-3999/3907.Count%20Smaller%20Elements%20With%20Opposite%20Parity/README.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3907. Count Smaller Elements With Opposite Parity 🔒](https://leetcode.cn/problems/count-smaller-elements-with-opposite-parity)
10+
11+
[English Version](/solution/3900-3999/3907.Count%20Smaller%20Elements%20With%20Opposite%20Parity/README_EN.md)
12+
13+
## 题目描述
14+
15+
<!-- description:start -->
16+
17+
<p>You are given an integer array <code>nums</code> of length <code>n</code>.</p>
18+
19+
<p>The <strong>score</strong> of an index <code>i</code> is defined as the number of indices <code>j</code> such that:</p>
20+
21+
<ul>
22+
<li><code>i &lt; j &lt; n</code></li>
23+
<li><code>nums[j] &lt; nums[i]</code></li>
24+
<li><code>nums[i]</code> and <code>nums[j]</code> have different parity (one is even and the other is odd).</li>
25+
</ul>
26+
27+
<p>Return an integer array <code>answer</code> of length <code>n</code>, where <code>answer[i]</code> is the score of index <code>i</code>.</p>
28+
29+
<p>&nbsp;</p>
30+
<p><strong class="example">Example 1:</strong></p>
31+
32+
<div class="example-block">
33+
<p><strong>Input:</strong> <span class="example-io">nums = [5,2,4,1,3]</span></p>
34+
35+
<p><strong>Output:</strong> <span class="example-io">[2,1,2,0,0]</span></p>
36+
37+
<p><strong>Explanation:</strong></p>
38+
39+
<ul>
40+
<li>For <code>i = 0</code>, the elements <code>nums[1] = 2</code> and <code>nums[2] = 4</code> are smaller and have different parity.</li>
41+
<li>For <code>i = 1</code>, the element <code>nums[3] = 1</code> is smaller and has different parity.</li>
42+
<li>For <code>i = 2</code>, the elements <code>nums[3] = 1</code> and <code>nums[4] = 3</code> are smaller and have different parity.</li>
43+
<li>No valid elements exist for the remaining indices.</li>
44+
</ul>
45+
46+
<p>Thus, the <code>answer = [2, 1, 2, 0, 0]</code>.</p>
47+
</div>
48+
49+
<p><strong class="example">Example 2:</strong></p>
50+
51+
<div class="example-block">
52+
<p><strong>Input:</strong> <span class="example-io">nums = [4,4,1]</span></p>
53+
54+
<p><strong>Output:</strong> <span class="example-io">[1,1,0]</span></p>
55+
56+
<p><strong>Explanation:</strong>​​​​​​​</p>
57+
58+
<p>For <code>i = 0</code> and <code>i = 1</code>, the element <code>nums[2] = 1</code> is smaller and has different parity. Thus, the <code>answer = [1, 1, 0]</code>.</p>
59+
</div>
60+
61+
<p><strong class="example">Example 3:</strong></p>
62+
63+
<div class="example-block">
64+
<p><strong>Input:</strong> <span class="example-io">nums = [7]</span></p>
65+
66+
<p><strong>Output:</strong> <span class="example-io">[0]</span></p>
67+
68+
<p><strong>Explanation:</strong></p>
69+
70+
<p>No elements exist to the right of index 0, so its score is 0. Thus, the <code>answer = [0]</code>.</p>
71+
</div>
72+
73+
<p>&nbsp;</p>
74+
<p><strong>Constraints:</strong></p>
75+
76+
<ul>
77+
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
78+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code>​​​​​​​</li>
79+
</ul>
80+
81+
<!-- description:end -->
82+
83+
## 解法
84+
85+
<!-- solution:start -->
86+
87+
### 方法一:有序列表或树状数组
88+
89+
我们可以使用两个有序列表(或树状数组)分别维护偶数和奇数的元素。对于每个元素,我们查询另一个列表中比它小的元素的数量,并将当前元素添加到对应的列表中。
90+
91+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 是数组的长度。
92+
93+
<!-- tabs:start -->
94+
95+
#### Python3
96+
97+
```python
98+
class Solution:
99+
def countSmallerOppositeParity(self, nums: list[int]) -> list[int]:
100+
n = len(nums)
101+
ans = [0] * n
102+
sl = [SortedList(), SortedList()]
103+
for i in range(n - 1, -1, -1):
104+
ans[i] = sl[nums[i] & 1 ^ 1].bisect_left(nums[i])
105+
sl[nums[i] & 1].add(nums[i])
106+
return ans
107+
```
108+
109+
#### Java
110+
111+
```java
112+
class BinaryIndexedTree {
113+
int n;
114+
int[] c;
115+
116+
BinaryIndexedTree(int n) {
117+
this.n = n;
118+
this.c = new int[n + 1];
119+
}
120+
121+
void update(int x, int delta) {
122+
while (x <= n) {
123+
c[x] += delta;
124+
x += x & -x;
125+
}
126+
}
127+
128+
int query(int x) {
129+
int s = 0;
130+
while (x > 0) {
131+
s += c[x];
132+
x -= x & -x;
133+
}
134+
return s;
135+
}
136+
}
137+
138+
class Solution {
139+
public int[] countSmallerOppositeParity(int[] nums) {
140+
int n = nums.length;
141+
int[] sorted = nums.clone();
142+
Arrays.sort(sorted);
143+
int m = 0;
144+
for (int i = 0; i < n; i++) {
145+
if (i == 0 || sorted[i] != sorted[i - 1]) {
146+
sorted[m++] = sorted[i];
147+
}
148+
}
149+
BinaryIndexedTree[] bit = {new BinaryIndexedTree(m), new BinaryIndexedTree(m)};
150+
int[] ans = new int[n];
151+
for (int i = n - 1; i >= 0; --i) {
152+
int x = Arrays.binarySearch(sorted, 0, m, nums[i]) + 1;
153+
ans[i] = bit[nums[i] & 1 ^ 1].query(x - 1);
154+
bit[nums[i] & 1].update(x, 1);
155+
}
156+
return ans;
157+
}
158+
}
159+
```
160+
161+
#### C++
162+
163+
```cpp
164+
struct BIT {
165+
int n;
166+
vector<int> c;
167+
BIT(int n)
168+
: n(n)
169+
, c(n + 1, 0) {}
170+
void update(int x, int delta) {
171+
for (; x <= n; x += x & -x) c[x] += delta;
172+
}
173+
int query(int x) {
174+
int s = 0;
175+
for (; x > 0; x -= x & -x) s += c[x];
176+
return s;
177+
}
178+
};
179+
180+
class Solution {
181+
public:
182+
vector<int> countSmallerOppositeParity(vector<int>& nums) {
183+
int n = nums.size();
184+
vector<int> sorted = nums;
185+
sort(sorted.begin(), sorted.end());
186+
sorted.erase(unique(sorted.begin(), sorted.end()), sorted.end());
187+
188+
int m = sorted.size();
189+
BIT* bits[2] = {new BIT(m), new BIT(m)};
190+
vector<int> ans(n);
191+
192+
for (int i = n - 1; i >= 0; --i) {
193+
int x = lower_bound(sorted.begin(), sorted.end(), nums[i]) - sorted.begin() + 1;
194+
ans[i] = bits[nums[i] & 1 ^ 1]->query(x - 1);
195+
bits[nums[i] & 1]->update(x, 1);
196+
}
197+
return ans;
198+
}
199+
};
200+
```
201+
202+
#### Go
203+
204+
```go
205+
type BIT struct {
206+
n int
207+
c []int
208+
}
209+
210+
func newBIT(n int) *BIT {
211+
return &BIT{n: n, c: make([]int, n+1)}
212+
}
213+
214+
func (b *BIT) update(x, delta int) {
215+
for ; x <= b.n; x += x & -x {
216+
b.c[x] += delta
217+
}
218+
}
219+
220+
func (b *BIT) query(x int) int {
221+
s := 0
222+
for ; x > 0; x -= x & -x {
223+
s += b.c[x]
224+
}
225+
return s
226+
}
227+
228+
func countSmallerOppositeParity(nums []int) []int {
229+
n := len(nums)
230+
sorted := make([]int, n)
231+
copy(sorted, nums)
232+
sort.Ints(sorted)
233+
234+
m := 0
235+
if n > 0 {
236+
m = 1
237+
for i := 1; i < n; i++ {
238+
if sorted[i] != sorted[i-1] {
239+
sorted[m] = sorted[i]
240+
m++
241+
}
242+
}
243+
sorted = sorted[:m]
244+
}
245+
246+
bits := []*BIT{newBIT(m), newBIT(m)}
247+
ans := make([]int, n)
248+
249+
for i := n - 1; i >= 0; i-- {
250+
x := sort.SearchInts(sorted, nums[i]) + 1
251+
ans[i] = bits[nums[i]&1^1].query(x - 1)
252+
bits[nums[i]&1].update(x, 1)
253+
}
254+
return ans
255+
}
256+
```
257+
258+
#### TypeScript
259+
260+
```ts
261+
class BIT {
262+
private c: Int32Array;
263+
constructor(private n: number) {
264+
this.c = new Int32Array(n + 1);
265+
}
266+
update(x: number, delta: number) {
267+
for (; x <= this.n; x += x & -x) this.c[x] += delta;
268+
}
269+
query(x: number): number {
270+
let s = 0;
271+
for (; x > 0; x -= x & -x) s += this.c[x];
272+
return s;
273+
}
274+
}
275+
276+
function countSmallerOppositeParity(nums: number[]): number[] {
277+
const n = nums.length;
278+
const sorted = _.sortedUniq(_.sortBy(nums));
279+
const m = sorted.length;
280+
281+
const bits = [new BIT(m), new BIT(m)];
282+
const ans = new Array(n);
283+
284+
for (let i = n - 1; i >= 0; i--) {
285+
const rank = _.sortedIndex(sorted, nums[i]) + 1;
286+
ans[i] = bits[(nums[i] & 1) ^ 1].query(rank - 1);
287+
bits[nums[i] & 1].update(rank, 1);
288+
}
289+
return ans;
290+
}
291+
```
292+
293+
<!-- tabs:end -->
294+
295+
<!-- solution:end -->
296+
297+
<!-- problem:end -->

0 commit comments

Comments
 (0)