Skip to content

Commit fdad259

Browse files
committed
Weekly Contest 238
1 parent f583cb6 commit fdad259

6 files changed

+136
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
impl Solution {
2+
pub fn get_xor_sum(arr1: Vec<i32>, arr2: Vec<i32>) -> i32 {
3+
use std::ops::BitXor;
4+
arr1.iter().fold(0, BitXor::bitxor) & arr2.iter().fold(0, BitXor::bitxor)
5+
}
6+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Frequency of the Most Frequent Element
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+
int maxFrequency(vector<int>& a, int k) {
8+
long n = a.size(), j = 0, ans = 0;
9+
sort(a.begin(), a.end());
10+
vector<long> b(n+1);
11+
REP(i, n)
12+
b[i+1] = b[i]+a[i];
13+
REP(i, n) {
14+
while ((i-j)*a[i]-(b[i]-b[j]) > k)
15+
j++;
16+
ans = max(ans, i-j+1);
17+
}
18+
return ans;
19+
}
20+
};
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Longest Substring Of All Vowels in Order
2+
class Solution {
3+
public:
4+
int longestBeautifulSubstring(string a) {
5+
int n = a.size(), ans = 0;
6+
for (int i = 0; i < n; )
7+
if (a[i] != 'a')
8+
i++;
9+
else {
10+
int j = i; while (++i < n && a[i] == 'a');
11+
if (a[i] != 'e') continue; while (++i < n && a[i] == 'e');
12+
if (a[i] != 'i') continue; while (++i < n && a[i] == 'i');
13+
if (a[i] != 'o') continue; while (++i < n && a[i] == 'o');
14+
if (a[i] != 'u') continue; while (++i < n && a[i] == 'u');
15+
ans = max(ans, i-j);
16+
}
17+
return ans;
18+
}
19+
};
20+
21+
///
22+
23+
#define FOR(i, a, b) for (remove_cv<remove_reference<decltype(b)>::type>::type i = (a); i < (b); i++)
24+
#define REP(i, n) FOR(i, 0, n)
25+
26+
class Solution {
27+
public:
28+
int longestBeautifulSubstring(string word) {
29+
int n = word.size(), y = -1;
30+
vector<int> b(n);
31+
REP(i, n) {
32+
if (i && word[i] == word[i-1])
33+
b[i] = y;
34+
else
35+
b[i] = y = i-1;
36+
}
37+
int ans = 0;
38+
REP(i, n)
39+
if (word[i] == 'u') {
40+
int j = b[i]; if (j < 0 || word[j] != 'o') continue;
41+
j = b[j]; if (j < 0 || word[j] != 'i') continue;
42+
j = b[j]; if (j < 0 || word[j] != 'e') continue;
43+
j = b[j]; if (j < 0 || word[j] != 'a') continue;
44+
ans = max(ans, i-b[j]);
45+
}
46+
return ans;
47+
}
48+
};
49+

maximum-building-height.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Maximum Building Height
2+
class Solution {
3+
public:
4+
int maxBuilding(int n, vector<vector<int>>& res) {
5+
vector<pair<int, int>> a;
6+
a.emplace_back(1, 0);
7+
for (auto &x: res)
8+
a.emplace_back(x[0], x[1]);
9+
a.emplace_back(n, n-a.back().first+a.back().second);
10+
sort(a.begin(), a.end());
11+
int m = a.size(), ans = 0;
12+
for (int i = 0; i < m-1; i++) {
13+
int d = a[i+1].first-a[i].first;
14+
a[i+1].second = min(a[i+1].second, a[i].second+d);
15+
}
16+
for (int i = m; --i; ) {
17+
int d = a[i].first-a[i-1].first;
18+
a[i-1].second = min(a[i-1].second, a[i].second+d);
19+
ans = max(ans, a[i-1].second+a[i].second+d >> 1);
20+
}
21+
return ans;
22+
}
23+
};

single-threaded-cpu.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 get_order(tasks: Vec<Vec<i32>>) -> Vec<i32> {
3+
use std::collections::BinaryHeap;
4+
use std::cmp::Reverse;
5+
let n = tasks.len();
6+
let mut tasks: Vec<(i32, i32, usize)> = tasks
7+
.into_iter().enumerate().map(|(i,a)| (a[0], a[1], i)).collect();
8+
tasks.sort_by_key(|v| v.0);
9+
10+
let mut q: BinaryHeap<Reverse<(i32, usize)>> = BinaryHeap::new();
11+
let mut ans = Vec::new();
12+
let mut tick = 0;
13+
let mut i = 0;
14+
while ans.len() != n {
15+
if i < n && tasks[i].0 <= tick {
16+
q.push(Reverse((tasks[i].1, tasks[i].2)));
17+
i += 1;
18+
} else if !q.is_empty() {
19+
let x = q.pop().unwrap().0;
20+
ans.push(x.1 as i32);
21+
tick += x.0;
22+
} else {
23+
tick = tasks[i].0;
24+
}
25+
}
26+
ans
27+
}
28+
}

sum-of-digits-in-base-k.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Sum of Digits in Base K
2+
class Solution {
3+
public:
4+
int sumBase(int n, int k) {
5+
int s = 0;
6+
do s += n%k;
7+
while (n /= k);
8+
return s;
9+
}
10+
};

0 commit comments

Comments
 (0)