Skip to content

Commit 48ce020

Browse files
committed
misc
1 parent fdad259 commit 48ce020

10 files changed

+264
-46
lines changed

concatenated-words.rs

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
///
2+
#[derive(Default)]
3+
struct Trie { c: [Option<Box<Self>>; 26], is_word: bool }
4+
5+
impl Solution {
6+
pub fn find_all_concatenated_words_in_a_dict(mut words: Vec<String>) -> Vec<String> {
7+
words.sort_by_key(|w| w.len());
8+
let mut trie: Trie = Default::default();
9+
let mut vis: Vec<isize> = vec![0; words.last().unwrap().len()+1];
10+
let mut q: Vec<usize> = vec![];
11+
let mut tick = 0;
12+
let mut ans: Vec<String> = vec![];
13+
for ww in words.into_iter() {
14+
if ww.is_empty() { continue; }
15+
let w = ww.as_bytes();
16+
q.clear();
17+
q.push(0);
18+
tick += 1;
19+
while let Some(bgn) = q.pop() {
20+
let mut t: &Trie = &mut trie;
21+
for i in bgn..w.len() {
22+
if let Some(t1) = &t.c[(w[i]-b'a') as usize] {
23+
t = t1;
24+
if t.is_word && vis[i+1] != tick {
25+
vis[i+1] = tick;
26+
q.push(i+1);
27+
}
28+
} else {
29+
break;
30+
}
31+
}
32+
if vis[w.len()] == tick {
33+
ans.push(ww.clone());
34+
break;
35+
}
36+
}
37+
38+
let mut t = &mut trie;
39+
for c in w.iter().map(|ch| (ch-b'a') as usize) {
40+
t = t.c[c].get_or_insert_with(|| Default::default());
41+
}
42+
t.is_word = true;
43+
}
44+
ans
45+
}
46+
}
47+
48+
/// memoization
49+
50+
#[derive(Default)]
51+
struct Trie { c: [Option<Box<Self>>; 26], is_word: bool }
52+
53+
impl Solution {
54+
pub fn find_all_concatenated_words_in_a_dict(mut words: Vec<String>) -> Vec<String> {
55+
struct St<'a> { root: &'a Trie, memo: &'a mut [(isize,bool)], tick: isize, w: &'a [u8] };
56+
fn dfs(st: &mut St, mut t: &Trie, bgn: usize) -> bool {
57+
if st.memo[bgn].0 == st.tick { return st.memo[bgn].1 }
58+
let w = st.w;
59+
if w.len() == bgn { return true; }
60+
let mut ans = false;
61+
for i in bgn..w.len() {
62+
if let Some(t1) = &t.c[(w[i]-b'a') as usize] {
63+
t = t1;
64+
if t1.is_word && dfs(st, st.root, i+1) {
65+
ans = true;
66+
break;
67+
}
68+
} else {
69+
break;
70+
}
71+
}
72+
st.memo[bgn] = (st.tick, ans);
73+
ans
74+
}
75+
76+
words.sort_by_key(|w| w.len());
77+
let mut trie: Trie = Default::default();
78+
let mut memo: Vec<(isize,bool)> = vec![(0, false); words.last().unwrap().len()+1];
79+
let mut tick = 0;
80+
let mut ans: Vec<String> = vec![];
81+
for w in words.into_iter() {
82+
tick += 1;
83+
let mut st = St { root: &trie, memo: &mut memo, tick, w: w.as_bytes() };
84+
if w.len() > 0 && dfs(&mut st, &trie, 0) {
85+
ans.push(w.to_owned());
86+
}
87+
let mut t = &mut trie;
88+
for c in w.as_bytes().iter().map(|ch| (ch-b'a') as usize) {
89+
t = t.c[c].get_or_insert_with(|| Default::default());
90+
}
91+
t.is_word = true;
92+
}
93+
ans
94+
}
95+
}

frequency-of-the-most-frequent-element.cc

+4-6
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@
55
class Solution {
66
public:
77
int maxFrequency(vector<int>& a, int k) {
8-
long n = a.size(), j = 0, ans = 0;
8+
long n = a.size(), j = 0, ans = 0, s = k;
99
sort(a.begin(), a.end());
10-
vector<long> b(n+1);
11-
REP(i, n)
12-
b[i+1] = b[i]+a[i];
1310
REP(i, n) {
14-
while ((i-j)*a[i]-(b[i]-b[j]) > k)
15-
j++;
11+
while ((i-j)*a[i] > s)
12+
s -= a[j++];
1613
ans = max(ans, i-j+1);
14+
s += a[i];
1715
}
1816
return ans;
1917
}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
impl Solution {
2+
pub fn max_frequency(mut nums: Vec<i32>, k: i32) -> i32 {
3+
let mut k = k as i64;
4+
let mut j = 0;
5+
let mut ans = 0;
6+
nums.sort();
7+
for (i, x) in nums.iter().enumerate() {
8+
while (i-j) as i64 * *x as i64 > k {
9+
k -= nums[j] as i64;
10+
j += 1;
11+
}
12+
ans = std::cmp::max(ans, i-j+1);
13+
k += *x as i64;
14+
}
15+
ans as i32
16+
}
17+
}

longest-substring-of-all-vowels-in-order.cc

+18
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,22 @@
11
// Longest Substring Of All Vowels in Order
2+
class Solution {
3+
public:
4+
int longestBeautifulSubstring(string a) {
5+
int ans = 0, l = 0, n = 1;
6+
for (int i = 1; a[i]; i++) {
7+
if (a[i-1] > a[i])
8+
l = i, n = 1;
9+
else if (a[i-1] < a[i])
10+
n++;
11+
if (n == 5)
12+
ans = max(ans, i-l+1);
13+
}
14+
return ans;
15+
}
16+
};
17+
18+
///
19+
220
class Solution {
321
public:
422
int longestBeautifulSubstring(string a) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
impl Solution {
2+
pub fn longest_beautiful_substring(word: String) -> i32 {
3+
let mut ans = 0;
4+
let mut c0 = 0;
5+
let mut l = 0;
6+
let mut n = 0;
7+
for (i, c) in word.as_bytes().iter().enumerate() {
8+
if c0 > *c { l = i; n = 1; }
9+
else if c0 < *c { n += 1; }
10+
c0 = *c;
11+
if n == 5 { ans = std::cmp::max(ans, i-l+1); }
12+
}
13+
ans as i32
14+
}
15+
}

maximum-building-height.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ class Solution {
44
int maxBuilding(int n, vector<vector<int>>& res) {
55
vector<pair<int, int>> a;
66
a.emplace_back(1, 0);
7+
a.emplace_back(n, n-1);
78
for (auto &x: res)
89
a.emplace_back(x[0], x[1]);
9-
a.emplace_back(n, n-a.back().first+a.back().second);
1010
sort(a.begin(), a.end());
1111
int m = a.size(), ans = 0;
1212
for (int i = 0; i < m-1; i++) {

maximum-building-height.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
impl Solution {
2+
pub fn max_building(n: i32, restrictions: Vec<Vec<i32>>) -> i32 {
3+
use std::cmp;
4+
let mut a: Vec<(i32, i32)> = restrictions.into_iter().map(|x| (x[0], x[1])).collect();
5+
a.push((1, 0));
6+
a.push((n, n-1));
7+
a.sort_unstable();
8+
let mut ans = 0;
9+
for i in 0..a.len()-1 {
10+
a[i+1].1 = a[i+1].1.min(a[i+1].0-a[i].0+a[i].1);
11+
}
12+
for i in (1..a.len()).rev() {
13+
let d = a[i].0-a[i-1].0;
14+
a[i-1].1 = a[i-1].1.min(d+a[i].1);
15+
ans = ans.max(a[i-1].1+a[i].1+d >> 1);
16+
}
17+
ans
18+
}
19+
}

word-break-ii.cc

+24-39
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,32 @@
11
// Word Break II
2-
#define REP(i, n) for (int i = 0; i < (n); i++)
3-
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
4-
#define ROF(i, a, b) for (int i = (b); --i >= (a); )
5-
62
class Solution {
7-
private:
8-
vector<vector<bool>> f;
9-
vector<bool> g;
10-
vector<int> r;
11-
vector<string> rr;
12-
void h(int i, int n, const string &s) {
13-
if (i == n) {
14-
string ss = s;
15-
REP(i, r.size()-1)
16-
ss.insert(r[i]+i, " ");
17-
rr.push_back(ss);
3+
string s;
4+
vector<string> dict, ans;
5+
vector<int> parts;
6+
void dfs(int n) {
7+
if (n == 0) {
8+
string ss;
9+
for (int j = 0, i = parts.size(); i--; ) {
10+
if (j) ss += ' ';
11+
ss.insert(ss.size(), &s[j], parts[i]);
12+
j += parts[i];
13+
}
14+
ans.push_back(ss);
1815
} else
19-
FOR(j, i, n)
20-
if (f[i][j] && g[j+1]) {
21-
r.push_back(j+1);
22-
h(j+1, n, s);
23-
r.pop_back();
16+
for (auto &w: dict)
17+
if (w.size() <= n && s.compare(n-w.size(), w.size(), w) == 0) {
18+
parts.push_back(w.size());
19+
dfs(n-w.size());
20+
parts.pop_back();
2421
}
2522
}
2623
public:
27-
vector<string> wordBreak(string s, unordered_set<string> &dict) {
28-
int n = s.size();
29-
f.assign(n, vector<bool>(n, false));
30-
REP(i, n)
31-
FOR(j, i, n)
32-
if (dict.count(s.substr(i, j-i+1)))
33-
f[i][j] = true;
34-
g.assign(n+1, false);
35-
g[n] = true;
36-
ROF(i, 0, n)
37-
FOR(j, i, n)
38-
if (f[i][j] && g[j+1]) {
39-
g[i] = true;
40-
break;
41-
}
42-
r.clear();
43-
rr.clear();
44-
h(0, n, s);
45-
return rr;
24+
vector<string> wordBreak(string s, vector<string>& wordDict) {
25+
this->s = move(s);
26+
dict = move(wordDict);
27+
ans.clear();
28+
parts.clear();
29+
dfs(this->s.size());
30+
return ans;
4631
}
4732
};

word-break-ii.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
impl Solution {
2+
pub fn word_break(s: String, word_dict: Vec<String>) -> Vec<String> {
3+
struct Sol<'a> { dict: &'a [String], ans: Vec<String>, parts: Vec<&'a str> }
4+
impl<'a> Sol<'a> {
5+
fn dfs(self: &mut Self, s: &str) {
6+
if s.is_empty() {
7+
self.ans.push(self.parts.join(" "));
8+
return;
9+
}
10+
for w in self.dict.iter() {
11+
if s.starts_with(w) {
12+
self.parts.push(w);
13+
self.dfs(&s[w.len()..]);
14+
self.parts.pop();
15+
}
16+
}
17+
}
18+
}
19+
let mut sol = Sol { dict: &word_dict, ans: vec![], parts: vec![] };
20+
sol.dfs(&s);
21+
sol.ans
22+
}
23+
}

word-search-ii.rs

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#[derive(Default)]
2+
struct Trie { c: [Option<Box<Self>>; 26], num: usize, w: Option<String> }
3+
4+
impl Solution {
5+
pub fn find_words(mut board: Vec<Vec<char>>, words: Vec<String>) -> Vec<String> {
6+
let mut trie: Trie = Default::default();
7+
for w in words.into_iter() {
8+
let mut x = &mut trie;
9+
for c in w.as_bytes().iter().map(|ch| (ch-b'a') as usize) {
10+
x = x.c[c].get_or_insert_with(|| Default::default());
11+
x.num += 1;
12+
}
13+
x.w = Some(w)
14+
}
15+
16+
fn dfs(b: &mut Vec<Vec<char>>, ans: &mut Vec<String>, mut t: &mut Trie,
17+
x: usize, y: usize) -> usize {
18+
let ch = b[x][y];
19+
if ch == '.' { return 0; }
20+
if let Some(t1) = t.c[(ch as u8-b'a') as usize] {
21+
t = &mut *t1;
22+
if t.num == 0 { return 0; }
23+
let mut num = 0;
24+
if let Some(w) = t.w.take() {
25+
ans.push(w);
26+
num += 1;
27+
}
28+
b[x][y] = '.';
29+
if x > 0 { num += dfs(b, ans, t, x-1, y); }
30+
if x+1 < b.len() { num += dfs(b, ans, t, x+1, y); }
31+
if y > 0 { num += dfs(b, ans, t, x, y-1); }
32+
if y+1 < b[0].len() { num += dfs(b, ans, t, x, y+1); }
33+
b[x][y] = ch;
34+
t.num -= num;
35+
return num;
36+
}
37+
0
38+
}
39+
40+
let m = board.len();
41+
let n = board[0].len();
42+
let mut ans = vec![];
43+
for x in 0..m {
44+
for y in 0..n {
45+
dfs(&mut board, &mut ans, &mut trie, x, y); } }
46+
ans
47+
}
48+
}

0 commit comments

Comments
 (0)