Skip to content

Commit d3ec0b0

Browse files
authored
feat: add rust solution to lc problem: No.3433 (#4898)
1 parent 60c4ca0 commit d3ec0b0

File tree

3 files changed

+178
-0
lines changed

3 files changed

+178
-0
lines changed

solution/3400-3499/3433.Count Mentions Per User/README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,67 @@ function countMentions(numberOfUsers: number, events: string[][]): number[] {
376376
}
377377
```
378378

379+
#### Rust
380+
381+
```rust
382+
impl Solution {
383+
pub fn count_mentions(number_of_users: i32, mut events: Vec<Vec<String>>) -> Vec<i32> {
384+
let n = number_of_users as usize;
385+
386+
events.sort_by(|a, b| {
387+
let x: i32 = a[1].parse().unwrap();
388+
let y: i32 = b[1].parse().unwrap();
389+
if x == y {
390+
a[0].as_bytes()[2].cmp(&b[0].as_bytes()[2])
391+
} else {
392+
x.cmp(&y)
393+
}
394+
});
395+
396+
let mut ans = vec![0_i32; n];
397+
let mut online_t = vec![0_i32; n];
398+
let mut lazy = 0_i32;
399+
400+
for e in events {
401+
let etype = &e[0];
402+
let cur: i32 = e[1].parse().unwrap();
403+
let s = &e[2];
404+
405+
let c0 = etype.as_bytes()[0] as char;
406+
407+
if c0 == 'O' {
408+
let uid: usize = s.parse().unwrap();
409+
online_t[uid] = cur + 60;
410+
411+
} else if s.as_bytes()[0] as char == 'A' {
412+
lazy += 1;
413+
414+
} else if s.as_bytes()[0] as char == 'H' {
415+
for i in 0..n {
416+
if online_t[i] <= cur {
417+
ans[i] += 1;
418+
}
419+
}
420+
421+
} else {
422+
for a in s.split(' ') {
423+
let uid: usize = a[2..].parse().unwrap();
424+
ans[uid] += 1;
425+
}
426+
}
427+
}
428+
429+
if lazy > 0 {
430+
for i in 0..n {
431+
ans[i] += lazy;
432+
}
433+
}
434+
435+
ans
436+
}
437+
}
438+
```
439+
379440
<!-- tabs:end -->
380441

381442
<!-- solution:end -->

solution/3400-3499/3433.Count Mentions Per User/README_EN.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,67 @@ function countMentions(numberOfUsers: number, events: string[][]): number[] {
374374
}
375375
```
376376

377+
#### Rust
378+
379+
```rust
380+
impl Solution {
381+
pub fn count_mentions(number_of_users: i32, mut events: Vec<Vec<String>>) -> Vec<i32> {
382+
let n = number_of_users as usize;
383+
384+
events.sort_by(|a, b| {
385+
let x: i32 = a[1].parse().unwrap();
386+
let y: i32 = b[1].parse().unwrap();
387+
if x == y {
388+
a[0].as_bytes()[2].cmp(&b[0].as_bytes()[2])
389+
} else {
390+
x.cmp(&y)
391+
}
392+
});
393+
394+
let mut ans = vec![0_i32; n];
395+
let mut online_t = vec![0_i32; n];
396+
let mut lazy = 0_i32;
397+
398+
for e in events {
399+
let etype = &e[0];
400+
let cur: i32 = e[1].parse().unwrap();
401+
let s = &e[2];
402+
403+
let c0 = etype.as_bytes()[0] as char;
404+
405+
if c0 == 'O' {
406+
let uid: usize = s.parse().unwrap();
407+
online_t[uid] = cur + 60;
408+
409+
} else if s.as_bytes()[0] as char == 'A' {
410+
lazy += 1;
411+
412+
} else if s.as_bytes()[0] as char == 'H' {
413+
for i in 0..n {
414+
if online_t[i] <= cur {
415+
ans[i] += 1;
416+
}
417+
}
418+
419+
} else {
420+
for a in s.split(' ') {
421+
let uid: usize = a[2..].parse().unwrap();
422+
ans[uid] += 1;
423+
}
424+
}
425+
}
426+
427+
if lazy > 0 {
428+
for i in 0..n {
429+
ans[i] += lazy;
430+
}
431+
}
432+
433+
ans
434+
}
435+
}
436+
```
437+
377438
<!-- tabs:end -->
378439

379440
<!-- solution:end -->
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
impl Solution {
2+
pub fn count_mentions(number_of_users: i32, mut events: Vec<Vec<String>>) -> Vec<i32> {
3+
let n = number_of_users as usize;
4+
5+
events.sort_by(|a, b| {
6+
let x: i32 = a[1].parse().unwrap();
7+
let y: i32 = b[1].parse().unwrap();
8+
if x == y {
9+
a[0].as_bytes()[2].cmp(&b[0].as_bytes()[2])
10+
} else {
11+
x.cmp(&y)
12+
}
13+
});
14+
15+
let mut ans = vec![0_i32; n];
16+
let mut online_t = vec![0_i32; n];
17+
let mut lazy = 0_i32;
18+
19+
for e in events {
20+
let etype = &e[0];
21+
let cur: i32 = e[1].parse().unwrap();
22+
let s = &e[2];
23+
24+
let c0 = etype.as_bytes()[0] as char;
25+
26+
if c0 == 'O' {
27+
let uid: usize = s.parse().unwrap();
28+
online_t[uid] = cur + 60;
29+
30+
} else if s.as_bytes()[0] as char == 'A' {
31+
lazy += 1;
32+
33+
} else if s.as_bytes()[0] as char == 'H' {
34+
for i in 0..n {
35+
if online_t[i] <= cur {
36+
ans[i] += 1;
37+
}
38+
}
39+
40+
} else {
41+
for a in s.split(' ') {
42+
let uid: usize = a[2..].parse().unwrap();
43+
ans[uid] += 1;
44+
}
45+
}
46+
}
47+
48+
if lazy > 0 {
49+
for i in 0..n {
50+
ans[i] += lazy;
51+
}
52+
}
53+
54+
ans
55+
}
56+
}

0 commit comments

Comments
 (0)