Skip to content

Commit 9a94b58

Browse files
committed
misc
1 parent 3b3fb1f commit 9a94b58

21 files changed

+559
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Count Ways to Build Rooms in an Ant Colony
2+
impl Solution {
3+
pub fn ways_to_build_rooms(prev: Vec<i32>) -> i32 {
4+
const MOD: usize = 1000000007;
5+
let n = prev.len();
6+
let mut inv = vec![1; n];
7+
let mut fac = vec![1; n];
8+
let mut invfac = vec![1; n];
9+
fac[0] = 1; invfac[0] = 1;
10+
fac[1] = 1; invfac[1] = 1; inv[1] = 1;
11+
for i in 2..n {
12+
inv[i] = ((MOD-MOD/i)*(inv[MOD%i] as usize)%MOD) as i32;
13+
fac[i] = (fac[i-1] as usize*i%MOD) as i32;
14+
invfac[i] = (invfac[i-1] as usize*inv[i] as usize%MOD) as i32;
15+
}
16+
17+
let mut d = vec![0; n];
18+
let mut top = std::usize::MAX;
19+
for &p in prev.iter() { if p >= 0 { d[p as usize] += 1; }}
20+
for i in 0..n { if d[i] == 0 { d[i] = top; top = i; }}
21+
22+
let mut num = vec![0; n];
23+
let mut cnt = vec![1; n];
24+
let mut ans = 1;
25+
while top != std::usize::MAX {
26+
let u = top;
27+
let p = prev[u] as usize;
28+
top = d[top];
29+
cnt[u] = cnt[u] *fac[num[u]] as usize%MOD;
30+
num[u] += 1;
31+
if p == std::usize::MAX {
32+
ans = cnt[u];
33+
} else {
34+
cnt[p] = cnt[p] *cnt[u]%MOD *invfac[num[u]] as usize%MOD;
35+
num[p] += num[u];
36+
d[p] -= 1;
37+
if d[p] == 0 {
38+
d[p] = top;
39+
top = p;
40+
}
41+
}
42+
}
43+
ans as i32
44+
}
45+
}

course-schedule-iii.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Course Schedule III
2+
impl Solution {
3+
pub fn schedule_course(mut courses: Vec<Vec<i32>>) -> i32 {
4+
use std::collections::BinaryHeap;
5+
courses.sort_unstable_by_key(|x| x[1]);
6+
let mut h = BinaryHeap::new();
7+
let mut s = 0;
8+
for c in courses.into_iter() {
9+
s += c[0];
10+
h.push(c[0]);
11+
if s > c[1] {
12+
s -= h.pop().unwrap();
13+
}
14+
}
15+
h.len() as i32
16+
}
17+
}

cyclically-rotating-a-grid.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Cyclically Rotating a Grid
2+
impl Solution {
3+
pub fn rotate_grid(mut a: Vec<Vec<i32>>, k: i32) -> Vec<Vec<i32>> {
4+
let m = a.len()-1;
5+
let n = a[0].len()-1;
6+
let mut b = vec![];
7+
for q in 0..(m.min(n)+1)/2 {
8+
b.clear();
9+
for i in q..n-q { b.push(a[q][i]); }
10+
for i in q..m-q { b.push(a[i][n-q]); }
11+
for i in (q+1..=n-q).rev() { b.push(a[m-q][i]); }
12+
for i in (q+1..=m-q).rev() { b.push(a[i][q]); }
13+
b.rotate_left(k as usize%(2*(m+n)-q*8));
14+
let mut j = 0;
15+
for i in q..n-q { a[q][i] = b[j]; j += 1; }
16+
for i in q..m-q { a[i][n-q] = b[j]; j += 1; }
17+
for i in (q+1..=n-q).rev() { a[m-q][i] = b[j]; j += 1; }
18+
for i in (q+1..=m-q).rev() { a[i][q] = b[j]; j += 1; }
19+
}
20+
a
21+
}
22+
}

find-longest-awesome-substring.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Find Longest Awesome Substring
2+
impl Solution {
3+
pub fn longest_awesome(s: String) -> i32 {
4+
let mut pos = vec![-1i32; 1<<10];
5+
let mut sum = 0;
6+
let mut ans = 0;
7+
pos[0] = 0;
8+
for (i, c) in s.as_bytes().iter().enumerate() {
9+
let i = i as i32;
10+
sum ^= 1 << c-b'0';
11+
if pos[sum] >= 0 {
12+
ans = ans.max(i+1-pos[sum]);
13+
} else {
14+
pos[sum] = i+1;
15+
}
16+
for j in 0..10 {
17+
let p = pos[sum ^ 1<<j];
18+
if p >= 0 {
19+
ans = ans.max(i+1-p);
20+
}
21+
}
22+
}
23+
ans
24+
}
25+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Find the Kth Smallest Sum of a Matrix With Sorted Rows
2+
impl Solution {
3+
pub fn kth_smallest(mat: Vec<Vec<i32>>, k: i32) -> i32 {
4+
use std::collections::BinaryHeap;
5+
let mut h = BinaryHeap::new();
6+
h.push(0);
7+
for a in mat.into_iter() {
8+
let mut h1 = BinaryHeap::new();
9+
for x in h.iter() {
10+
for y in a.iter() {
11+
h1.push(x+y);
12+
if h1.len() > k as usize { h1.pop(); }
13+
}
14+
}
15+
h = h1;
16+
}
17+
*h.peek().unwrap()
18+
}
19+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Find the Longest Substring Containing Vowels in Even Counts
2+
impl Solution {
3+
pub fn find_the_longest_substring(s: String) -> i32 {
4+
let mut pos = vec![-1; 1<<5];
5+
pos[0] = 0;
6+
let mut sum = 0;
7+
let mut ans = 0;
8+
for (i, c) in s.as_bytes().iter().enumerate() {
9+
let i = i as i32;
10+
match c {
11+
b'a' => sum ^= 1,
12+
b'e' => sum ^= 2,
13+
b'i' => sum ^= 4,
14+
b'o' => sum ^= 8,
15+
b'u' => sum ^= 16,
16+
_ => (),
17+
}
18+
if pos[sum] < 0 {
19+
pos[sum] = i+1;
20+
} else {
21+
ans = ans.max(i+1-pos[sum]);
22+
}
23+
}
24+
ans
25+
}
26+
}

largest-palindrome-product.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Largest Palindrome Product
2+
impl Solution {
3+
pub fn largest_palindrome(n: i32) -> i32 {
4+
let l = 10i64.pow(n as u32-1);
5+
let h = 10i64.pow(n as u32)-1;
6+
for mut i in (l..=h).rev() {
7+
let mut s = i;
8+
while i > 0 {
9+
s = s*10+i%10;
10+
i /= 10;
11+
}
12+
let mut j = (s+h-1)/h;
13+
loop {
14+
if s/j < j { break; }
15+
if s%j == 0 { return (s%1337) as i32; }
16+
j += 1;
17+
}
18+
}
19+
9
20+
}
21+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Maximum Alternating Subsequence Sum
2+
impl Solution {
3+
pub fn max_alternating_sum(nums: Vec<i32>) -> i64 {
4+
let mut s = nums[0] as i64;
5+
for i in 1..nums.len() {
6+
if nums[i] > nums[i-1] {
7+
s += (nums[i]-nums[i-1]) as i64;
8+
}
9+
}
10+
s
11+
}
12+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Maximum Number of Events That Can Be Attended
2+
impl Solution {
3+
pub fn max_events(mut events: Vec<Vec<i32>>) -> i32 {
4+
let n = events.iter().fold(0, |acc,e| acc.max(e[1])) + 2;
5+
events.sort_unstable_by_key(|x| x[1]);
6+
let mut uf: Vec<i32> = (0..n).collect();
7+
let mut ans = 0;
8+
for e in events.into_iter() {
9+
let mut x = e[0];
10+
while uf[x as usize] != x {
11+
uf[x as usize] = uf[uf[x as usize] as usize];
12+
x = uf[x as usize];
13+
}
14+
if x <= e[1] {
15+
uf[x as usize] = x+1;
16+
ans += 1;
17+
}
18+
}
19+
ans
20+
}
21+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Maximum Number of Removable Characters
2+
impl Solution {
3+
pub fn maximum_removals(s: String, p: String, removable: Vec<i32>) -> i32 {
4+
let s = s.as_bytes();
5+
let p = p.as_bytes();
6+
let mut l = 0;
7+
let mut h = removable.len();
8+
let mut del = vec![false; s.len()];
9+
while l < h {
10+
let m = l+h+1 >> 1;
11+
for i in 0..m { del[removable[i] as usize] = true; }
12+
let mut j = 0;
13+
for i in 0..s.len() {
14+
if !del[i] && s[i] == p[j] {
15+
j += 1;
16+
if j == p.len() { break; }
17+
}
18+
}
19+
for i in 0..m { del[removable[i] as usize] = false; }
20+
if j == p.len() {
21+
l = m;
22+
} else {
23+
h = m-1;
24+
}
25+
}
26+
l as i32
27+
}
28+
}

maximum-performance-of-a-team.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Maximum Performance of a Team
2+
impl Solution {
3+
pub fn max_performance(n: i32, speed: Vec<i32>, efficiency: Vec<i32>, k: i32) -> i32 {
4+
use std::{cmp::Reverse,collections::BinaryHeap};
5+
let mut h: BinaryHeap<Reverse<i32>> = BinaryHeap::new();
6+
let mut a = efficiency.into_iter().zip(speed).collect::<Vec<(i32,i32)>>();
7+
a.sort_unstable_by_key(|x| Reverse(x.0));
8+
let mut sum = 0;
9+
let mut ans = 0;
10+
for (e, s) in a.into_iter() {
11+
if h.len() == k as usize { sum -= h.pop().unwrap().0 as i64; }
12+
sum += s as i64;
13+
h.push(Reverse(s));
14+
ans = ans.max(e as i64 * sum)
15+
}
16+
(ans % 1000000007) as i32
17+
}
18+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Minimum Absolute Difference Queries
2+
impl Solution {
3+
pub fn min_difference(a: Vec<i32>, queries: Vec<Vec<i32>>) -> Vec<i32> {
4+
const V: usize = 100;
5+
let n = a.len();
6+
let mut s = vec![[0i32; V]; n+1];
7+
for i in 0..n {
8+
let (before, after) = s.split_at_mut(i+1);
9+
after[0].clone_from_slice(&before[i]);
10+
s[i+1][a[i] as usize-1] += 1;
11+
}
12+
let mut ans = vec![];
13+
for q in queries.into_iter() {
14+
let mut x = std::i32::MAX;
15+
let mut last = -1;
16+
let l = &s[q[0] as usize];
17+
let r = &s[q[1] as usize+1];
18+
for i in 0..100 {
19+
if (*r)[i as usize] != (*l)[i as usize] {
20+
if last >= 0 { x = x.min(i-last); }
21+
last = i;
22+
}
23+
}
24+
ans.push(if x==std::i32::MAX {-1} else {x});
25+
}
26+
ans
27+
}
28+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Minimum Cost to Change the Final Value of Expression
2+
impl Solution {
3+
pub fn min_operations_to_flip(e: String) -> i32 {
4+
let mut isp = vec![0; 127];
5+
isp[b'&' as usize]=3; isp[b'|' as usize]=3; isp[b'(' as usize]=1;
6+
let mut icp = vec![0; 127];
7+
icp[b'&' as usize]=2; icp[b'|' as usize]=2; icp[b'(' as usize]=4; icp[b')' as usize]=1;
8+
let mut op = vec![b'\0'];
9+
let mut st = vec![];
10+
for &c in e.as_bytes().iter().chain(std::iter::once(&b'\0')) {
11+
if c == b'0' || c == b'1' {
12+
let v = (c-b'0') as i32;
13+
st.push((v, 1-v));
14+
continue;
15+
}
16+
while isp[op[op.len()-1] as usize] > icp[c as usize] {
17+
let y = st.pop().unwrap();
18+
let x = st.pop().unwrap();
19+
let c00 = x.0+y.0; let c01 = x.0+y.1;
20+
let c10 = x.1+y.0; let c11 = x.1+y.1;
21+
st.push(if op.pop().unwrap() == b'&' {
22+
(c00.min(c01).min(c10), c11.min(c01+1).min(c10+1))
23+
} else {
24+
(c00.min(c01+1).min(c10+1), c11.min(c01).min(c10))
25+
});
26+
}
27+
if isp[op[op.len()-1] as usize] == icp[c as usize] {
28+
op.pop();
29+
} else {
30+
op.push(c);
31+
}
32+
}
33+
let x = st.pop().unwrap();
34+
x.0.max(x.1)
35+
}
36+
}
37+
38+
///
39+
40+
impl Solution {
41+
pub fn min_operations_to_flip(e: String) -> i32 {
42+
let mut isp = vec![0; 127];
43+
isp[b'&' as usize]=3; isp[b'|' as usize]=3; isp[b'(' as usize]=1;
44+
let mut icp = vec![0; 127];
45+
icp[b'&' as usize]=2; icp[b'|' as usize]=2; icp[b'(' as usize]=4; icp[b')' as usize]=1;
46+
let mut op = vec![b'\0'];
47+
let mut st = vec![];
48+
for &c in e.as_bytes().iter().chain(std::iter::once(&b'\0')) {
49+
if c == b'0' || c == b'1' {
50+
let v = (c-b'0') as i32;
51+
st.push((v, 1-v));
52+
continue;
53+
}
54+
while isp[op[op.len()-1] as usize] > icp[c as usize] {
55+
let y = st.pop().unwrap();
56+
let x = st.pop().unwrap();
57+
let c00 = x.0+y.0; let c01 = x.0+y.1;
58+
let c10 = x.1+y.0; let c11 = x.1+y.1;
59+
st.push(if op.pop().unwrap() == b'&' {
60+
(c00.min(c01).min(c10), c11.min(c01+1).min(c10+1))
61+
} else {
62+
(c00.min(c01+1).min(c10+1), c11.min(c01).min(c10))
63+
});
64+
}
65+
if isp[op[op.len()-1] as usize] == icp[c as usize] {
66+
op.pop();
67+
} else {
68+
op.push(c);
69+
}
70+
}
71+
let x = st.pop().unwrap();
72+
x.0.max(x.1)
73+
}
74+
}

0 commit comments

Comments
 (0)