Skip to content

Commit

Permalink
Add problems from Biweekly Contest 44.
Browse files Browse the repository at this point in the history
  • Loading branch information
pin3da committed Jan 23, 2021
1 parent d043c09 commit 96fd571
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
11 changes: 11 additions & 0 deletions solved/LeetCode/1732. Find the Highest Altitude/solution.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution {
fun largestAltitude(gain: IntArray): Int {
var cur = 0
var best = 0
for (i in gain) {
cur += i
best = best.coerceAtLeast(cur)
}
return best
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class Solution {
public:
int minimumTeachings(int n, vector<vector<int>>& languages, vector<vector<int>>& friendships) {
memo = vector<vector<int>>(501, vector<int>(501, -1));

vector<set<int>> lang(languages.size());
for (int i = 0; i < languages.size(); i++) {
for (auto it : languages[i]) lang[i].insert(it);
}

int best = languages.size();
for (int cur = 0; cur < n; cur++) {
set<int> toTeach;
for (auto& fri : friendships) {
assert(fri.size() == 2);
if (!CanTalk(fri[0] - 1, fri[1] - 1, languages)) {
for (auto person : fri) {
if (lang[person - 1].count(cur) == 0) {
toTeach.insert(person);
}
}
}
}
best = min<int>(toTeach.size(), best);
}
return best;
}

private:
bool CanTalk(int a, int b, const vector<vector<int>>& lang) {
if (memo[a][b] != -1) return memo[a][b];

set<int> A(lang[a].begin(), lang[a].end());
for (auto it : lang[b]) {
if (A.count(it)) {
return memo[a][b] = 1;
}
}
return memo[a][b] = 0;
}

vector<vector<int>> memo;
};
15 changes: 15 additions & 0 deletions solved/LeetCode/1734. Decode XORed Permutation/solution.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public:
vector<int> decode(vector<int>& encoded) {
vector<int> ans(encoded.size() + 1);
int extra = ((ans.size() + 1) / 2) & 1;
for (int i = 1; i < encoded.size(); i+=2) {
extra ^= encoded[i];
}
ans[0] = extra;
for (int i = 1; i < ans.size(); i++) {
ans[i] = ans[i-1] ^ encoded[i-1];
}
return ans;
}
};

0 comments on commit 96fd571

Please sign in to comment.