Skip to content

Commit b1f33c7

Browse files
committed
Rust
1 parent 8e3f9ef commit b1f33c7

7 files changed

+250
-0
lines changed
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Count Number of Special Subsequences
2+
impl Solution {
3+
pub fn count_special_subsequences(nums: Vec<i32>) -> i32 {
4+
const MOD: i64 = 1000000007;
5+
let mut a = 0;
6+
let mut b = 0;
7+
let mut c = 0;
8+
for x in nums.iter() {
9+
match x {
10+
0 => a = (a*2+1) % MOD,
11+
1 => b = (b*2+a) % MOD,
12+
_ => c = (c*2+b) % MOD
13+
}
14+
}
15+
c as i32
16+
}
17+
}

delete-duplicate-folders-in-system.cc

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Delete Duplicate Folders in System
2+
#define ALL(x) (x).begin(), (x).end()
3+
#define FOR(i, a, b) for (remove_cv<remove_reference<decltype(b)>::type>::type i = (a); i < (b); i++)
4+
#define REP(i, n) FOR(i, 0, n)
5+
6+
class Solution {
7+
public:
8+
static const long P = 1000000007;
9+
long inv(long x) {
10+
long s = 1;
11+
for (; x > 1; x = P%x)
12+
s = s*(P-P/x)%P;
13+
return s;
14+
}
15+
vector<vector<string>> deleteDuplicateFolder(vector<vector<string>>& paths) {
16+
sort(ALL(paths));
17+
unordered_map<string, int> code;
18+
unordered_map<int, string> name;
19+
vector<int> a, cur;
20+
vector<pair<int, int>> ranges;
21+
for (auto &path: paths) {
22+
auto res = code.try_emplace(path.back(), code.size()+1);
23+
int tail = res.first->second;
24+
if (res.second)
25+
name.try_emplace(tail, path.back());
26+
for (; cur.size() && (cur.size() > path.size() || cur.size() == path.size() && a[cur.back()] != tail); cur.pop_back()) {
27+
a.push_back(0);
28+
ranges.emplace_back(cur.back(), a.size());
29+
}
30+
cur.emplace_back(a.size());
31+
a.push_back(tail);
32+
}
33+
for (; cur.size(); cur.pop_back()) {
34+
a.push_back(0);
35+
ranges.emplace_back(cur.back(), a.size());
36+
}
37+
38+
long k = code.size()+1, pow = 1;
39+
vector<long> h(a.size()+1), pw(a.size());
40+
REP(i, a.size()) {
41+
pw[i] = pow;
42+
h[i+1] = (h[i]+pow*a[i]) % P;
43+
pow = pow*k % P;
44+
}
45+
unordered_map<long, pair<int, int>> seen;
46+
cur.assign(a.size()+1, 0);
47+
for (auto [l, r]: ranges) {
48+
if (r-l == 2) continue;
49+
long v = (h[r-1]-h[l+1]+P)*inv(pw[l+1]) % P;
50+
auto res = seen.try_emplace(v, pair<int, int>{l, r});
51+
if (!res.second) {
52+
cur[l]++;
53+
cur[r]--;
54+
cur[res.first->second.first]++;
55+
cur[res.first->second.second]--;
56+
}
57+
}
58+
59+
int acc = 0;
60+
vector<string> path;
61+
vector<vector<string>> ans;
62+
REP(i, a.size()) {
63+
acc += cur[i];
64+
if (a[i]) {
65+
path.push_back(name[a[i]]);
66+
if (!acc)
67+
ans.push_back(path);
68+
} else
69+
path.pop_back();
70+
}
71+
return ans;
72+
}
73+
};

distant-barcodes.rs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Distant Barcodes
2+
impl Solution {
3+
pub fn rearrange_barcodes(barcodes: Vec<i32>) -> Vec<i32> {
4+
let mut freq = vec![0; 10001];
5+
let n = barcodes.len();
6+
for &ch in barcodes.iter() { freq[ch as usize] += 1; }
7+
let mut a = freq.into_iter().enumerate().collect::<Vec<_>>();
8+
a.sort_unstable_by_key(|x| std::cmp::Reverse(x.1));
9+
let mut ans = vec![0; n];
10+
let mut j = 0;
11+
for (i, c) in a.into_iter() {
12+
for _ in 0..c {
13+
ans[j] = i as i32;
14+
j += 2;
15+
if j >= n { j = 1; }
16+
}
17+
}
18+
ans
19+
}
20+
}

find-duplicate-subtrees.cc

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Find Duplicate Subtrees
2+
#define FOR(i, a, b) for (remove_cv<remove_reference<decltype(b)>::type>::type i = (a); i < (b); i++)
3+
#define REP(i, n) FOR(i, 0, n)
4+
5+
class Solution {
6+
public:
7+
static const int K = 501, K2 = K/2;
8+
static const long P = 1000000007;
9+
vector<int> a;
10+
vector<long> h, pw;
11+
vector<tuple<TreeNode *, int, int>> ranges;
12+
long inv(long x) {
13+
long s = 1;
14+
for (; x > 1; x = P%x)
15+
s = s*(P-P/x)%P;
16+
return s;
17+
}
18+
void dfs(TreeNode *x) {
19+
int l = a.size();
20+
a.push_back(x->val+K2);
21+
if (x->left) dfs(x->left);
22+
a.push_back(K-1);
23+
if (x->right) dfs(x->right);
24+
a.push_back(K-1);
25+
ranges.emplace_back(x, l, a.size());
26+
}
27+
vector<TreeNode *> findDuplicateSubtrees(TreeNode *root) {
28+
*this = {};
29+
dfs(root);
30+
h.resize(a.size()+1);
31+
pw.resize(a.size());
32+
long pow = 1;
33+
REP(i, a.size()) {
34+
pw[i] = pow;
35+
h[i+1] = (h[i]+pow*a[i]) % P;
36+
pow = pow*K % P;
37+
}
38+
unordered_map<long, TreeNode *> mp;
39+
vector<TreeNode *> ans;
40+
for (auto &range: ranges) {
41+
int l, r;
42+
TreeNode *x;
43+
tie(x, l, r) = range;
44+
long v = (h[r]-h[l]+P)*inv(pw[l]) % P;
45+
auto res = mp.try_emplace(v, x);
46+
if (!res.second && res.first->second) {
47+
ans.push_back(x);
48+
res.first->second = nullptr;
49+
}
50+
}
51+
return ans;
52+
}
53+
};

maximum-compatibility-score-sum.rs

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Maximum Compatibility Score Sum
2+
impl Solution {
3+
pub fn max_compatibility_sum(students: Vec<Vec<i32>>, mentors: Vec<Vec<i32>>) -> i32 {
4+
use std::{i32,usize};
5+
fn augment(n:usize, u:usize, mate:&mut[usize], slack:&mut[i32], lx:&mut[i32], ly:&mut[i32],
6+
fx:&mut[bool], fy:&mut[bool], a:&[Vec<i32>]) -> bool {
7+
fx[u] = true;
8+
for i in 0..n {
9+
if fy[i] { continue; }
10+
let t = lx[u]+ly[i]-a[u][i];
11+
if t != 0 {
12+
slack[i] = slack[i].min(t);
13+
} else {
14+
fy[i] = true;
15+
if mate[i] == usize::MAX || augment(n, mate[i], mate, slack, lx, ly, fx, fy, a) {
16+
mate[i] = u;
17+
return true;
18+
}
19+
}
20+
}
21+
false
22+
}
23+
24+
let n = students.len();
25+
let m = students[0].len();
26+
let mut a = vec![vec![0; n]; n];
27+
for i in 0..n {
28+
for j in 0..n {
29+
for k in 0..m {
30+
if students[i][k] == mentors[j][k] {
31+
a[i][j] += 1; }}}}
32+
33+
let mut mate = vec![usize::MAX; n];
34+
let mut lx = (0..n).map(|i| *a[i].iter().max().unwrap()).collect::<Vec<i32>>();
35+
let mut ly = vec![0; n];
36+
let mut slack = vec![0; n];
37+
let mut fx = vec![false; n];
38+
let mut fy = vec![false; n];
39+
for i in 0..n {
40+
// fill requires 1.50
41+
//slack.fill(i32::MAX);
42+
for j in 0..n { slack[j] = i32::MAX; }
43+
loop {
44+
for j in 0..n { fx[j] = false; fy[j] = false; }
45+
if augment(n, i, &mut mate, &mut slack, &mut lx, &mut ly, &mut fx, &mut fy,&a) { break; }
46+
let mut d = i32::MAX;
47+
for j in 0..n {
48+
if !fy[j] && slack[j] < d {
49+
d = slack[j]; }}
50+
for j in 0..n {
51+
if fx[j] { lx[j] -= d; }
52+
if fy[j] { ly[j] += d; }
53+
else { slack[j] -= d; }
54+
}
55+
}
56+
}
57+
lx.iter().sum::<i32>()+ly.iter().sum::<i32>()
58+
}
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Maximum Number of Weeks for Which You Can Work
2+
impl Solution {
3+
pub fn number_of_weeks(a: Vec<i32>) -> i64 {
4+
let (s,m) = a.into_iter().map(|x| x as i64).fold((0,0), |(s,m),x| (s+x, m.max(x)));
5+
if s < m*2 { (s-m)*2+1 } else { s }
6+
}
7+
}

reorganize-string.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Reorganize String
2+
impl Solution {
3+
pub fn reorganize_string(s: String) -> String {
4+
let mut freq = vec![0; 26];
5+
let n = s.len();
6+
for &ch in s.as_bytes().iter() { freq[(ch-b'a') as usize] += 1; }
7+
let mut a = freq.into_iter().enumerate().collect::<Vec<_>>();
8+
a.sort_unstable_by_key(|x| std::cmp::Reverse(x.1));
9+
if a[0].1 > n/2+n%2 { return String::new(); }
10+
let mut ans = vec![b'a'; n];
11+
let mut j = 0;
12+
for (i, c) in a.into_iter() {
13+
for _ in 0..c {
14+
ans[j] = b'a'+i as u8;
15+
j += 2;
16+
if j >= n { j = 1; }
17+
}
18+
}
19+
String::from_utf8_lossy(&ans[..]).into_owned()
20+
}
21+
}

0 commit comments

Comments
 (0)