Skip to content

Commit f583cb6

Browse files
committed
Weekly Contest 237 and misc
1 parent b038d42 commit f583cb6

10 files changed

+220
-1
lines changed

bricks-falling-when-hit.cc

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Bricks Falling When Hit
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+
static constexpr int dx[] = {-1,0,1,0}, dy[] = {0,1,0,-1};
7+
int m, n, num = 0;
8+
public:
9+
void dfs(vector<vector<int>> &g, int x, int y) {
10+
g[x][y] = 2;
11+
num++;
12+
REP(i, 4) {
13+
unsigned x1 = x+dx[i], y1 = y+dy[i];
14+
if (x1 < m && y1 < n && g[x1][y1] == 1)
15+
dfs(g, x1, y1);
16+
}
17+
}
18+
vector<int> hitBricks(vector<vector<int>> &g, vector<vector<int>> &hits) {
19+
vector<int> ans(hits.size());
20+
m = g.size();
21+
n = g[0].size();
22+
for (auto &h: hits)
23+
g[h[0]][h[1]]--;
24+
REP(i, n)
25+
if (g[0][i] == 1)
26+
dfs(g, 0, i);
27+
for (int i = hits.size(); i--; ) {
28+
int old = num;
29+
int x = hits[i][0], y = hits[i][1];
30+
if (g[x][y]++) continue;
31+
bool f = !x;
32+
REP(d, 4) {
33+
unsigned x1 = x+dx[d], y1 = y+dy[d];
34+
if (x1 < m && y1 < n && g[x1][y1] == 2)
35+
f = true;
36+
}
37+
if (f) {
38+
dfs(g, x, y);
39+
ans[i] = num-old-1;
40+
}
41+
}
42+
return ans;
43+
}
44+
};

check-if-the-sentence-is-pangram.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Check if the Sentence Is Pangram
2+
class Solution {
3+
public:
4+
bool checkIfPangram(string sentence) {
5+
int s[26] = {}, cnt = 0;
6+
for (char c: sentence)
7+
if (!s[c-'a']++)
8+
cnt++;
9+
return cnt == 26;
10+
}
11+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Find XOR Sum of All Pairs Bitwise AND
2+
class Solution {
3+
public:
4+
int getXORSum(vector<int>& a, vector<int>& b) {
5+
return accumulate(a.begin(), a.end(), 0, bit_xor()) &
6+
accumulate(b.begin(), b.end(), 0, bit_xor());
7+
}
8+
};

max-value-of-equation.cc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Max Value of Equation
2+
/// monotonicity
3+
#define ALL(x) (x).begin(), (x).end()
4+
5+
class Solution {
6+
public:
7+
int findMaxValueOfEquation(vector<vector<int>>& points, int k) {
8+
deque<pair<int, int>> q;
9+
int ans = INT_MIN;
10+
for (auto &p: points) {
11+
int x = p[0], y = p[1];
12+
while (q.size() && x-q[0].first > k)
13+
q.pop_front();
14+
if (q.size())
15+
ans = max(ans, x+y+q[0].second);
16+
while (q.size() && q.back().second < y-x)
17+
q.pop_back();
18+
q.emplace_back(x, y-x);
19+
}
20+
return ans;
21+
}
22+
};

maximum-ice-cream-bars.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Maximum Ice Cream Bars
2+
#define ALL(x) (x).begin(), (x).end()
3+
4+
class Solution {
5+
public:
6+
int maxIceCream(vector<int>& a, int coins) {
7+
sort(ALL(a));
8+
int s = 0;
9+
for (int x: a)
10+
if (coins >= x)
11+
coins -= x, s++;
12+
return s;
13+
}
14+
};
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Minimum Number of Operations to Make String Sorted
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+
#define ROF(i, a, b) for (remove_cv<remove_reference<decltype(b)>::type>::type i = (b); --i >= (a); )
5+
6+
class Solution {
7+
static const int MOD = 1000000007;
8+
int inv(int x) {
9+
long s = 1;
10+
for (; x > 1; x = MOD%x)
11+
s = s*(MOD-MOD/x)%MOD;
12+
return s;
13+
}
14+
public:
15+
int makeStringSorted(string s) {
16+
int n = s.size(), cnt[26] = {};
17+
vector<int> fac(n+1), invfac(n+1);
18+
fac[0] = invfac[0] = 1;
19+
FOR(i, 1, n+1) {
20+
fac[i] = long(fac[i-1])*i%MOD;
21+
invfac[i] = inv(fac[i]);
22+
}
23+
long ans = 0;
24+
ROF(i, 0, n) {
25+
int ch = s[i]-'a';
26+
long c = accumulate(cnt, cnt+ch, 0L)*fac[n-1-i]%MOD;
27+
cnt[ch]++;
28+
REP(j, 26)
29+
c = c*invfac[cnt[j]]%MOD;
30+
ans = (ans+c)%MOD;
31+
}
32+
return ans;
33+
}
34+
};
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Minimum Number of Operations to Make String Sorted
2+
const MOD: i64 = 1000000007;
3+
4+
impl Solution {
5+
fn inv(mut x: i32) -> i32 {
6+
let mut s = 1;
7+
while x > 1 {
8+
s = s * (MOD-MOD/x as i64) % MOD;
9+
x = MOD as i32 % x;
10+
}
11+
return s as i32;
12+
}
13+
pub fn make_string_sorted(s: String) -> i32 {
14+
let n = s.len();
15+
let mut fac = vec![1; n+1];
16+
let mut invfac = vec![1; n+1];
17+
for i in 1..=n {
18+
fac[i] = (fac[i-1] as i64 * i as i64 % MOD) as i32;
19+
invfac[i] = Self::inv(fac[i]);
20+
}
21+
let mut cnt: Vec<usize> = vec![0; 26];
22+
let mut ans = 0;
23+
for i in (0..n).rev() {
24+
let ch = (s.as_bytes()[i]-b'a') as usize;
25+
let mut c = cnt[0..ch].iter().sum::<usize>() as i64 * fac[n-1-i] as i64 % MOD;
26+
cnt[ch] += 1;
27+
for j in 0..26 {
28+
c = c * invfac[cnt[j]] as i64 % MOD;
29+
}
30+
ans = (ans+c)%MOD;
31+
}
32+
ans as i32
33+
}
34+
}

next-permutation.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
class Solution {
55
public:
66
void nextPermutation(vector<int> &num) {
7-
if (num.size() <= 1) return;
87
ROF(i, 0, num.size()-1)
98
if (num[i] < num[i+1]) {
109
int j = num.size();

permutation-sequence.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
impl Solution {
2+
pub fn get_permutation(n: i32, mut k: i32) -> String {
3+
let f = [1,1,2,6,24,120,720,5040,40320];
4+
let mut a = 0x987654321u64;
5+
k -= 1;
6+
let mut s = String::new();
7+
for i in (0..n as usize).rev() {
8+
let t = k/f[i];
9+
k %= f[i];
10+
s.push(std::char::from_digit((a >> 4*t & 15) as u32, 10).unwrap());
11+
a = a&(1u64<<4*t)-1 | a>>4*(t+1)<<4*t;
12+
}
13+
s
14+
}
15+
}

single-threaded-cpu.cc

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Single-Threaded CPU
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+
struct Node {
7+
int bgn, dur, idx;
8+
bool operator>(const Node &r) const {
9+
return tie(dur, idx) > tie(r.dur, r.idx);
10+
}
11+
};
12+
13+
class Solution {
14+
public:
15+
vector<int> getOrder(vector<vector<int>>& tasks) {
16+
vector<Node> a;
17+
REP(i, tasks.size())
18+
a.push_back(Node{tasks[i][0], tasks[i][1], (int)i});
19+
sort(ALL(a), [](const Node &l, const Node &r) {
20+
return l.bgn != r.bgn ? l.bgn < r.bgn : l.idx < r.idx;
21+
});
22+
priority_queue<Node, vector<Node>, greater<Node>> b;
23+
int n = a.size(), i = 0;
24+
long tick = 0;
25+
vector<int> ans;
26+
while (i < n || b.size()) {
27+
if (i < n && a[i].bgn <= tick)
28+
b.push(a[i++]);
29+
else if (!b.empty()) {
30+
ans.push_back(b.top().idx);
31+
tick += b.top().dur;
32+
b.pop();
33+
} else
34+
tick = a[i].bgn;
35+
}
36+
return ans;
37+
}
38+
};

0 commit comments

Comments
 (0)