Skip to content

Commit 9c67122

Browse files
committed
feat: add solutions to lc problem: No.1395
No.1395.Count Number of Teams
1 parent fa7b5b4 commit 9c67122

File tree

9 files changed

+1010
-194
lines changed

9 files changed

+1010
-194
lines changed

solution/1300-1399/1395.Count Number of Teams/README.md

Lines changed: 379 additions & 61 deletions
Large diffs are not rendered by default.

solution/1300-1399/1395.Count Number of Teams/README_EN.md

Lines changed: 368 additions & 60 deletions
Large diffs are not rendered by default.

solution/1300-1399/1395.Count Number of Teams/Solution.cpp

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,53 @@
1+
class BinaryIndexedTree {
2+
public:
3+
BinaryIndexedTree(int _n)
4+
: n(_n)
5+
, c(_n + 1) {}
6+
7+
void update(int x, int delta) {
8+
while (x <= n) {
9+
c[x] += delta;
10+
x += x & -x;
11+
}
12+
}
13+
14+
int query(int x) {
15+
int s = 0;
16+
while (x) {
17+
s += c[x];
18+
x -= x & -x;
19+
}
20+
return s;
21+
}
22+
23+
private:
24+
int n;
25+
vector<int> c;
26+
};
27+
128
class Solution {
229
public:
330
int numTeams(vector<int>& rating) {
4-
int n = rating.size(), ans = 0;
5-
for (int j = 1; j < n - 1; ++j) {
6-
int ia = 0, ib = 0, ka = 0, kb = 0;
7-
for (int i = 0; i < j; ++i) {
8-
if (rating[i] < rating[j])
9-
++ia;
10-
else if (rating[i] > rating[j])
11-
++ib;
12-
}
13-
for (int k = j + 1; k < n; ++k) {
14-
if (rating[j] < rating[k])
15-
++ka;
16-
else if (rating[j] > rating[k])
17-
++kb;
18-
}
19-
ans += ia * ka + ib * kb;
31+
vector<int> nums = rating;
32+
sort(nums.begin(), nums.end());
33+
nums.erase(unique(nums.begin(), nums.end()), nums.end());
34+
int m = nums.size();
35+
BinaryIndexedTree tree1(m);
36+
BinaryIndexedTree tree2(m);
37+
for (int& v : rating) {
38+
int x = lower_bound(nums.begin(), nums.end(), v) - nums.begin() + 1;
39+
tree2.update(x, 1);
40+
}
41+
int ans = 0;
42+
int n = rating.size();
43+
for (int i = 0; i < n; ++i) {
44+
int x = lower_bound(nums.begin(), nums.end(), rating[i]) - nums.begin() + 1;
45+
tree1.update(x, 1);
46+
tree2.update(x, -1);
47+
int l = tree1.query(x - 1);
48+
int r = n - i - 1 - tree2.query(x);
49+
ans += l * r;
50+
ans += (i - l) * (n - i - 1 - r);
2051
}
2152
return ans;
2253
}
Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,55 @@
1-
func numTeams(rating []int) int {
2-
n, ans := len(rating), 0
3-
for j := 1; j < n-1; j++ {
4-
ia, ib, ka, kb := 0, 0, 0, 0
5-
for i := 0; i < j; i++ {
6-
if rating[i] < rating[j] {
7-
ia++
8-
} else if rating[i] > rating[j] {
9-
ib++
10-
}
11-
}
12-
for k := j + 1; k < n; k++ {
13-
if rating[j] < rating[k] {
14-
ka++
15-
} else if rating[j] > rating[k] {
16-
kb++
17-
}
1+
type BinaryIndexedTree struct {
2+
n int
3+
c []int
4+
}
5+
6+
func newBinaryIndexedTree(n int) *BinaryIndexedTree {
7+
c := make([]int, n+1)
8+
return &BinaryIndexedTree{n, c}
9+
}
10+
11+
func (this *BinaryIndexedTree) update(x, delta int) {
12+
for x <= this.n {
13+
this.c[x] += delta
14+
x += x & -x
15+
}
16+
}
17+
18+
func (this *BinaryIndexedTree) query(x int) int {
19+
s := 0
20+
for x > 0 {
21+
s += this.c[x]
22+
x -= x & -x
23+
}
24+
return s
25+
}
26+
27+
func numTeams(rating []int) (ans int) {
28+
nums := make([]int, len(rating))
29+
copy(nums, rating)
30+
sort.Ints(nums)
31+
m := 0
32+
for i, x := range nums {
33+
if i == 0 || x != nums[i-1] {
34+
nums[m] = x
35+
m++
1836
}
19-
ans += ia*ka + ib*kb
2037
}
21-
return ans
38+
nums = nums[:m]
39+
tree1 := newBinaryIndexedTree(m)
40+
tree2 := newBinaryIndexedTree(m)
41+
for _, x := range rating {
42+
tree2.update(sort.SearchInts(nums, x)+1, 1)
43+
}
44+
n := len(rating)
45+
for i, v := range rating {
46+
x := sort.SearchInts(nums, v) + 1
47+
tree1.update(x, 1)
48+
tree2.update(x, -1)
49+
l := tree1.query(x - 1)
50+
r := n - i - 1 - tree2.query(x)
51+
ans += l * r
52+
ans += (i - l) * (n - i - 1 - r)
53+
}
54+
return
2255
}
Lines changed: 62 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,70 @@
1+
class BinaryIndexedTree {
2+
private int n;
3+
private int[] c;
4+
5+
public BinaryIndexedTree(int n) {
6+
this.n = n;
7+
this.c = new int[n + 1];
8+
}
9+
10+
public void update(int x, int v) {
11+
while (x <= n) {
12+
c[x] += v;
13+
x += x & -x;
14+
}
15+
}
16+
17+
public int query(int x) {
18+
int s = 0;
19+
while (x > 0) {
20+
s += c[x];
21+
x -= x & -x;
22+
}
23+
return s;
24+
}
25+
}
26+
127
class Solution {
228
public int numTeams(int[] rating) {
329
int n = rating.length;
4-
int ans = 0;
5-
for (int j = 1; j < n - 1; ++j) {
6-
int ia = 0;
7-
int ib = 0;
8-
int ka = 0;
9-
int kb = 0;
10-
for (int i = 0; i < j; ++i) {
11-
if (rating[i] < rating[j]) {
12-
++ia;
13-
} else if (rating[i] > rating[j]) {
14-
++ib;
15-
}
16-
}
17-
for (int k = j + 1; k < n; ++k) {
18-
if (rating[j] < rating[k]) {
19-
++ka;
20-
} else if (rating[j] > rating[k]) {
21-
++kb;
22-
}
30+
int[] nums = rating.clone();
31+
Arrays.sort(nums);
32+
int m = 0;
33+
for (int i = 0; i < n; ++i) {
34+
if (i == 0 || nums[i] != nums[i - 1]) {
35+
nums[m++] = nums[i];
2336
}
24-
ans += ia * ka + ib * kb;
37+
}
38+
BinaryIndexedTree tree1 = new BinaryIndexedTree(m);
39+
BinaryIndexedTree tree2 = new BinaryIndexedTree(m);
40+
for (int v : rating) {
41+
int x = search(nums, v);
42+
tree2.update(x, 1);
43+
}
44+
45+
int ans = 0;
46+
for (int i = 0; i < n; ++i) {
47+
int x = search(nums, rating[i]);
48+
tree1.update(x, 1);
49+
tree2.update(x, -1);
50+
int l = tree1.query(x - 1);
51+
int r = n - i - 1 - tree2.query(x);
52+
ans += l * r;
53+
ans += (i - l) * (n - i - 1 - r);
2554
}
2655
return ans;
2756
}
57+
58+
private int search(int[] nums, int x) {
59+
int l = 0, r = nums.length;
60+
while (l < r) {
61+
int mid = (l + r) >> 1;
62+
if (nums[mid] >= x) {
63+
r = mid;
64+
} else {
65+
l = mid + 1;
66+
}
67+
}
68+
return l + 1;
69+
}
2870
}
Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,38 @@
1+
class BinaryIndexedTree:
2+
def __init__(self, n: int):
3+
self.n = n
4+
self.c = [0] * (n + 1)
5+
6+
def update(self, x: int, v: int):
7+
while x <= self.n:
8+
self.c[x] += v
9+
x += x & -x
10+
11+
def query(self, x: int) -> int:
12+
s = 0
13+
while x:
14+
s += self.c[x]
15+
x -= x & -x
16+
return s
17+
18+
119
class Solution:
220
def numTeams(self, rating: List[int]) -> int:
3-
n, ans = len(rating), 0
4-
for j in range(1, n - 1):
5-
ia = ib = ka = kb = 0
6-
for i in range(j):
7-
if rating[i] < rating[j]:
8-
ia += 1
9-
elif rating[i] > rating[j]:
10-
ib += 1
11-
for k in range(j + 1, n):
12-
if rating[j] < rating[k]:
13-
ka += 1
14-
elif rating[j] > rating[k]:
15-
kb += 1
16-
ans += ia * ka + ib * kb
21+
nums = sorted(set(rating))
22+
m = len(nums)
23+
tree1 = BinaryIndexedTree(m)
24+
tree2 = BinaryIndexedTree(m)
25+
for v in rating:
26+
x = bisect_left(nums, v) + 1
27+
tree2.update(x, 1)
28+
n = len(rating)
29+
ans = 0
30+
for i, v in enumerate(rating):
31+
x = bisect_left(nums, v) + 1
32+
tree1.update(x, 1)
33+
tree2.update(x, -1)
34+
l = tree1.query(x - 1)
35+
r = n - i - 1 - tree2.query(x)
36+
ans += l * r
37+
ans += (i - l) * (n - i - 1 - r)
1738
return ans
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
class BinaryIndexedTree {
2+
private n: number;
3+
private c: number[];
4+
5+
constructor(n: number) {
6+
this.n = n;
7+
this.c = new Array(n + 1).fill(0);
8+
}
9+
10+
public update(x: number, v: number): void {
11+
while (x <= this.n) {
12+
this.c[x] += v;
13+
x += x & -x;
14+
}
15+
}
16+
17+
public query(x: number): number {
18+
let s = 0;
19+
while (x > 0) {
20+
s += this.c[x];
21+
x -= x & -x;
22+
}
23+
return s;
24+
}
25+
}
26+
27+
function numTeams(rating: number[]): number {
28+
let nums = [...rating];
29+
nums.sort((a, b) => a - b);
30+
const n = rating.length;
31+
let m = 0;
32+
for (let i = 0; i < n; ++i) {
33+
if (i === 0 || nums[i] !== nums[i - 1]) {
34+
nums[m++] = nums[i];
35+
}
36+
}
37+
nums = nums.slice(0, m);
38+
const search = (x: number): number => {
39+
let l = 0;
40+
let r = m;
41+
while (l < r) {
42+
const mid = (l + r) >> 1;
43+
if (nums[mid] >= x) {
44+
r = mid;
45+
} else {
46+
l = mid + 1;
47+
}
48+
}
49+
return l + 1;
50+
};
51+
let ans = 0;
52+
const tree1 = new BinaryIndexedTree(m);
53+
const tree2 = new BinaryIndexedTree(m);
54+
for (const x of rating) {
55+
tree2.update(search(x), 1);
56+
}
57+
for (let i = 0; i < n; ++i) {
58+
const x = search(rating[i]);
59+
tree1.update(x, 1);
60+
tree2.update(x, -1);
61+
const l = tree1.query(x - 1);
62+
const r = n - i - 1 - tree2.query(x);
63+
ans += l * r;
64+
ans += (i - l) * (n - i - 1 - r);
65+
}
66+
return ans;
67+
}

solution/1300-1399/1396.Design Underground System/README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
<ul>
1414
<li><code>void checkIn(int id, string stationName, int t)</code>
15-
1615
<ul>
1716
<li>通行卡 ID 等于 <code>id</code> 的乘客,在时间 <code>t</code> ,从 <code>stationName</code> 站进入</li>
1817
<li>乘客一次只能从一个站进入</li>
@@ -31,7 +30,6 @@
3130
<li>在调用 <code>getAverageTime</code> 之前,至少有一名乘客从 <code>startStation</code> 站到达 <code>endStation</code> 站</li>
3231
</ul>
3332
</li>
34-
3533
</ul>
3634

3735
<p>你可以假设对 <code>checkIn</code> 和 <code>checkOut</code> 方法的所有调用都是符合逻辑的。如果一名乘客在时间 <code>t<sub>1</sub></code> 进站、时间 <code>t<sub>2</sub></code> 出站,那么 <code>t<sub>1</sub> &lt; t<sub>2</sub></code> 。所有时间都按时间顺序发生。</p>

solution/1300-1399/1396.Design Underground System/README_EN.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
<ul>
1212
<li><code>void checkIn(int id, string stationName, int t)</code>
13-
1413
<ul>
1514
<li>A customer with a card ID equal to <code>id</code>, checks in at the station <code>stationName</code> at time <code>t</code>.</li>
1615
<li>A customer can only be checked into one place at a time.</li>
@@ -29,7 +28,6 @@
2928
<li>There will be at least one customer that has traveled from <code>startStation</code> to <code>endStation</code> before <code>getAverageTime</code> is called.</li>
3029
</ul>
3130
</li>
32-
3331
</ul>
3432

3533
<p>You may assume all calls to the <code>checkIn</code> and <code>checkOut</code> methods are consistent. If a customer checks in at time <code>t<sub>1</sub></code> then checks out at time <code>t<sub>2</sub></code>, then <code>t<sub>1</sub> &lt; t<sub>2</sub></code>. All events happen in chronological order.</p>

0 commit comments

Comments
 (0)