Skip to content

Commit 7efe09c

Browse files
committed
update
1 parent e2dc5cf commit 7efe09c

File tree

3 files changed

+43
-43
lines changed

3 files changed

+43
-43
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ mod prob_10;
2828
mod prob_16;
2929
mod prob_18;
3030
mod prob_21;
31+
mod prob_22;
3132
mod prob_27;
3233
mod prob_29;
3334
mod prob_30;

src/prob_22.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
impl Solution {
2+
pub fn generate_parenthesis(n: i32) -> Vec<String> {
3+
let mut tmp = vec![];
4+
let mut ans = vec![];
5+
Self::dfs(n, 0, &mut tmp, &mut ans);
6+
ans
7+
}
8+
fn dfs(n: i32, open: i32, tmp: &mut Vec<u8>, ans: &mut Vec<String>) {
9+
if n == 0 {
10+
for _ in 0..open {
11+
tmp.push(b')');
12+
}
13+
ans.push(unsafe {std::str::from_utf8_unchecked(&tmp).to_string()});
14+
tmp.truncate(tmp.len()-(open as usize));
15+
return;
16+
}
17+
if open > 0 {
18+
tmp.push(b')');
19+
Self::dfs(n, open-1, tmp, ans);
20+
tmp.pop();
21+
}
22+
tmp.push(b'(');
23+
Self::dfs(n-1, open+1, tmp, ans);
24+
tmp.pop();
25+
}
26+
}
27+
28+
struct Solution;

src/prob_406.rs

Lines changed: 14 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,21 @@
1-
use std::collections::BinaryHeap;
2-
use std::cmp::{Ordering, Reverse};
3-
41
impl Solution {
5-
pub fn reconstruct_queue(people: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
6-
if people.len() <= 1 {
2+
pub fn reconstruct_queue(mut people: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
3+
let n = people.len();
4+
if n <= 1 {
75
return people;
86
}
9-
let mut heap = BinaryHeap::new();
10-
for item in people {
11-
heap.push(Reverse(Pair(item[0], item[1])));
12-
}
13-
let n = heap.len();
14-
let mut ans: Vec<Option<Vec<i32>>> = vec![None; n];
15-
while !heap.is_empty() {
16-
let cur = heap.pop().unwrap().0;
17-
let mut count = 0;
18-
let mut idx = 0;
19-
while cur.1 > count {
20-
if ans[idx].is_none() {
21-
count+=1;
22-
}
23-
idx+=1;
7+
people.sort_by(|a,b| {
8+
if a[0] == b[0] {
9+
a[1].cmp(&b[1])
10+
} else {
11+
b[0].cmp(&a[0])
2412
}
25-
while idx < n && ans[idx].is_some() {idx+=1;}
26-
ans[idx] = Some(vec![cur.0, cur.1]);
27-
}
28-
ans.into_iter().map(|x| x.unwrap()).collect()
29-
}
30-
}
31-
32-
#[derive(Eq, PartialEq, Copy, Clone)]
33-
struct Pair(i32, i32);
34-
35-
impl Ord for Pair {
36-
fn cmp(&self, other: &Self) -> Ordering {
37-
let t = self.0.cmp(&other.0);
38-
match t {
39-
Ordering::Equal => other.1.cmp(&self.1),
40-
_ => t,
13+
});
14+
let mut ans = vec![];
15+
for item in people {
16+
ans.insert(item[1] as usize, item);
4117
}
42-
}
43-
}
44-
45-
impl PartialOrd for Pair {
46-
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
47-
Some(self.cmp(other))
18+
ans
4819
}
4920
}
5021

@@ -60,7 +31,7 @@ mod tests {
6031
(vec![vec![1,1], vec![2,0]], vec![vec![2,0], vec![1,1]]),
6132
];
6233
for (num, expect) in test_cases {
63-
assert_eq!(expect, Solution::reconstruct_queue(num.clone()), "num: {:?}", num);
34+
assert_eq!(Solution::reconstruct_queue(num.clone()), expect, "num: {:?}", num);
6435
}
6536
}
6637
}

0 commit comments

Comments
 (0)