From 96fd57157aafe44560c828d6f3c4be3ee4555143 Mon Sep 17 00:00:00 2001 From: Manuel Pineda Date: Sat, 23 Jan 2021 18:00:18 +0100 Subject: [PATCH] Add problems from Biweekly Contest 44. --- .../solution.kt | 11 +++++ .../solution.cc | 43 +++++++++++++++++++ .../solution.cc | 15 +++++++ 3 files changed, 69 insertions(+) create mode 100644 solved/LeetCode/1732. Find the Highest Altitude/solution.kt create mode 100644 solved/LeetCode/1733. Minimum Number of People to Teach/solution.cc create mode 100644 solved/LeetCode/1734. Decode XORed Permutation/solution.cc diff --git a/solved/LeetCode/1732. Find the Highest Altitude/solution.kt b/solved/LeetCode/1732. Find the Highest Altitude/solution.kt new file mode 100644 index 00000000..e45f70f3 --- /dev/null +++ b/solved/LeetCode/1732. Find the Highest Altitude/solution.kt @@ -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 + } +} \ No newline at end of file diff --git a/solved/LeetCode/1733. Minimum Number of People to Teach/solution.cc b/solved/LeetCode/1733. Minimum Number of People to Teach/solution.cc new file mode 100644 index 00000000..8e492db6 --- /dev/null +++ b/solved/LeetCode/1733. Minimum Number of People to Teach/solution.cc @@ -0,0 +1,43 @@ +class Solution { + public: + int minimumTeachings(int n, vector>& languages, vector>& friendships) { + memo = vector>(501, vector(501, -1)); + + vector> 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 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(toTeach.size(), best); + } + return best; + } + + private: + bool CanTalk(int a, int b, const vector>& lang) { + if (memo[a][b] != -1) return memo[a][b]; + + set 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> memo; +}; diff --git a/solved/LeetCode/1734. Decode XORed Permutation/solution.cc b/solved/LeetCode/1734. Decode XORed Permutation/solution.cc new file mode 100644 index 00000000..93e328d4 --- /dev/null +++ b/solved/LeetCode/1734. Decode XORed Permutation/solution.cc @@ -0,0 +1,15 @@ +class Solution { + public: + vector decode(vector& encoded) { + vector 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; + } +};