From 8a535f42800e9f7aa703b9758cc512a51ad54b5d Mon Sep 17 00:00:00 2001 From: Manuel Pineda Date: Sun, 27 Dec 2020 15:10:59 +0100 Subject: [PATCH] Add LeetCode Biweekly Contest 42. --- .../Biweekly Contest 42/.vscode/settings.json | 11 +++ .../Contests/Biweekly Contest 42/A.cc | 51 +++++++++++++ .../Contests/Biweekly Contest 42/B.cc | 41 +++++++++++ .../Contests/Biweekly Contest 42/C.cc | 47 ++++++++++++ .../Contests/Biweekly Contest 42/D.cc | 72 +++++++++++++++++++ .../Contests/Biweekly Contest 42/E.cc | 19 +++++ .../Contests/Biweekly Contest 42/T.cc | 23 ++++++ .../Contests/Biweekly Contest 42/template.cc | 58 +++++++++++++++ 8 files changed, 322 insertions(+) create mode 100644 solved/LeetCode/Contests/Biweekly Contest 42/.vscode/settings.json create mode 100644 solved/LeetCode/Contests/Biweekly Contest 42/A.cc create mode 100644 solved/LeetCode/Contests/Biweekly Contest 42/B.cc create mode 100644 solved/LeetCode/Contests/Biweekly Contest 42/C.cc create mode 100644 solved/LeetCode/Contests/Biweekly Contest 42/D.cc create mode 100644 solved/LeetCode/Contests/Biweekly Contest 42/E.cc create mode 100644 solved/LeetCode/Contests/Biweekly Contest 42/T.cc create mode 100644 solved/LeetCode/Contests/Biweekly Contest 42/template.cc diff --git a/solved/LeetCode/Contests/Biweekly Contest 42/.vscode/settings.json b/solved/LeetCode/Contests/Biweekly Contest 42/.vscode/settings.json new file mode 100644 index 00000000..f6b93368 --- /dev/null +++ b/solved/LeetCode/Contests/Biweekly Contest 42/.vscode/settings.json @@ -0,0 +1,11 @@ +{ + "files.associations": { + "array": "cpp", + "bitset": "cpp", + "string_view": "cpp", + "initializer_list": "cpp", + "regex": "cpp", + "utility": "cpp", + "valarray": "cpp" + } +} \ No newline at end of file diff --git a/solved/LeetCode/Contests/Biweekly Contest 42/A.cc b/solved/LeetCode/Contests/Biweekly Contest 42/A.cc new file mode 100644 index 00000000..aebc60ed --- /dev/null +++ b/solved/LeetCode/Contests/Biweekly Contest 42/A.cc @@ -0,0 +1,51 @@ +#include +#include "./template.cc" +using namespace std; + +class Solution { +public: + int countStudents(vector& students, vector& sandwiches) { + deque> q; + for (int i = 0; i < students.size(); i++) { + q.emplace_back(students[i], i); + } + map> unable; + int i = 0; + while(i < sandwiches.size()) { + if (q.front().first == sandwiches[i]) { + q.pop_front(); + i++; + } else { + if (unable[i].count(q.front().second)) { + break; + } + q.push_back(q.front()); + unable[i].insert(q.front().second); + q.pop_front(); + } + } + return q.size(); + } +}; +struct Input { + vector st; + vector sand; +}; + +int main() { + vector in = { + { + {1,1,0,0}, + {0,1,0,1} + }, + { + {1,1,1,0,0,1}, + {1,0,0,0,1,1} + } + }; + + for (auto &it : in) { + Solution sol; + debug(sol.countStudents(it.st, it.sand)); + } +} \ No newline at end of file diff --git a/solved/LeetCode/Contests/Biweekly Contest 42/B.cc b/solved/LeetCode/Contests/Biweekly Contest 42/B.cc new file mode 100644 index 00000000..90b684a1 --- /dev/null +++ b/solved/LeetCode/Contests/Biweekly Contest 42/B.cc @@ -0,0 +1,41 @@ +#include +#include "./template.cc" +using namespace std; + +class Solution { +public: + double averageWaitingTime(vector>& c) { + double ans = 0; + int cur = 0; + for (auto it : c) { + int arr = it[0]; + int prep = it[1]; + ans += prep; + if (arr < cur) { + ans += (cur - arr); + } + cur = max(cur + prep, arr + prep); + } + return ans / c.size(); + } +}; + +struct Input { + vector> c; +}; + +int main() { + vector in = { + { + {{1,2},{2,5},{4,3}} + }, + { + {{5,2},{5,4},{10,3},{20,1}} + } + }; + + for (auto &it : in) { + Solution sol; + print(cout, sol.averageWaitingTime(it.c)); + } +} \ No newline at end of file diff --git a/solved/LeetCode/Contests/Biweekly Contest 42/C.cc b/solved/LeetCode/Contests/Biweekly Contest 42/C.cc new file mode 100644 index 00000000..98342f5d --- /dev/null +++ b/solved/LeetCode/Contests/Biweekly Contest 42/C.cc @@ -0,0 +1,47 @@ +#include +#include "./template.cc" +using namespace std; + +class Solution { +public: + string maximumBinaryString(string binary) { + string ans = binary; + int j = 0; + while (j < ans.size() && ans[j] == '1') j++; + for (int i = 0; i < ans.size(); i++) { + // debug(ans); + if (ans[i] == '1') continue; + if (i + 1 >= ans.size()) continue; + if (ans[i + 1] == '0') { + ans[i] = '1'; + j++; + continue; + } + // 0111...110 + j++; + while (j < ans.size() && ans[j] == '1') j++; + if (j >= ans.size()) break; + // debug(i+1, j); + swap(ans[i+1], ans[j]); + // 0011...111 + ans[i] = '1'; + } + return ans; + } +}; + +struct Input { + string b; +}; + +int main() { + vector in = { + {"000110"}, + {"01"} + }; + + for (auto &it : in) { + Solution sol; + print(cout, sol.maximumBinaryString(it.b)); + } +} \ No newline at end of file diff --git a/solved/LeetCode/Contests/Biweekly Contest 42/D.cc b/solved/LeetCode/Contests/Biweekly Contest 42/D.cc new file mode 100644 index 00000000..d01d452f --- /dev/null +++ b/solved/LeetCode/Contests/Biweekly Contest 42/D.cc @@ -0,0 +1,72 @@ +#include +#include "./template.cc" +using namespace std; + +using int64 = long long; +class Solution { + vector bestLeft(vector& nums, int64 k) { + int n = nums.size(); + int64 best = 10000000000; + vector ans(n, best); + deque pos; + int64 acc = 0; + int64 extra = ((k - 1) * (k) / 2); + for (int64 i = 0; i < n; i++) { + if (nums[i] == 1) { + pos.push_back(i); + acc += i; + } + if (pos.size() > k) { + acc -= pos.front(); + pos.pop_front(); + } + if (pos.size() == k) { + ans[i] = i * k - acc - extra; + } + + } + return ans; + } + +public: + int minMoves(vector& nums, int k) { + int sL = k / 2; + int sR = k - sL; + auto left = bestLeft(nums, sL); + reverse(nums.begin(), nums.end()); + auto right = bestLeft(nums, sR); + reverse(right.begin(), right.end()); + int64 best = 10000000000; + for (int i = 0; i + 1 < nums.size(); i++) { + best = min(best, left[i] + right[i + 1]); + } + return best; + } +}; + +struct Input { + vector n; + int k; +}; + +int main() { + vector in = { + { + {1,0,0,1,0,1}, 2 + }, + { + {1,0,0,0,0,0,1,1}, 3 + }, + { + {0,1,1,0,0,1,0,0,0}, 3 + }, + { + {1,0,0,1,0,1,1,1,0,1,1}, 7 + } + }; + + for (auto &it : in) { + Solution sol; + print(cout, sol.minMoves(it.n, it.k)); + } +} diff --git a/solved/LeetCode/Contests/Biweekly Contest 42/E.cc b/solved/LeetCode/Contests/Biweekly Contest 42/E.cc new file mode 100644 index 00000000..cf494214 --- /dev/null +++ b/solved/LeetCode/Contests/Biweekly Contest 42/E.cc @@ -0,0 +1,19 @@ + int n = nums.size(); + vector r(n); + int64 size = 0; + for (int i = n - 1; i >= 0; i--) { + size += nums[i]; + r[i] += size; + if (i + 1 < n) r[i] += r[i + 1]; + } + + + size = 0; + int64 prev = 0; + int64 best = numeric_limits::max(); + for (int i = 0; i < n; i++) { + best = min(best, prev + r[i]); + size += (nums[i]); + prev += size; + } + return best; \ No newline at end of file diff --git a/solved/LeetCode/Contests/Biweekly Contest 42/T.cc b/solved/LeetCode/Contests/Biweekly Contest 42/T.cc new file mode 100644 index 00000000..f1bb728b --- /dev/null +++ b/solved/LeetCode/Contests/Biweekly Contest 42/T.cc @@ -0,0 +1,23 @@ +#include +#include "./template.cc" +using namespace std; + +struct Solution { + +}; + +struct Input { + +}; + +int main() { + vector in = { + + }; + + for (auto &it : in) { + Solution sol; + // TODO: Method. + sol.method(in); + } +} \ No newline at end of file diff --git a/solved/LeetCode/Contests/Biweekly Contest 42/template.cc b/solved/LeetCode/Contests/Biweekly Contest 42/template.cc new file mode 100644 index 00000000..cc3ea288 --- /dev/null +++ b/solved/LeetCode/Contests/Biweekly Contest 42/template.cc @@ -0,0 +1,58 @@ +void print(std::ostream& out) { out << std::endl; } + +template +void printOne(std::ostream& out, const std::pair& a) { + out << "{" << a.first << ", " << a.second << "}"; +} + +template +void printOne(std::ostream& out, const T& a) { + out << a; +} + +template +void printOne(std::ostream& out, const std::vector& a) { + out << "{"; + for (int i = 0; i < a.size(); i++) { + if (i) out << ", "; + printOne(out, a[i]); + } + out << "}"; +} + +template +void printOne(std::ostream& out, const std::deque& a) { + out << "{"; + for (int i = 0; i < a.size(); i++) { + if (i) out << ", "; + printOne(out, a[i]); + } + out << "}"; +} + + +template +void printOne(std::ostream& out, const std::map& a) { + out << "{"; + for (auto [key, val] : a) { + out << "["; printOne(out, key); out << " = "; printOne(out, val); out << "],"; + } + out << "}"; +} + + +template +void print(std::ostream& out, const T& first, const Tail... tail) { + printOne(out, first); + if (sizeof...(tail) != 0) { + out << ", "; + } + print(out, tail...); +} + +#ifdef LOCAL_CP +#define debug(...) \ + std::cerr << "[DEBUG] [" << #__VA_ARGS__ << "] = ", print(std::cerr, __VA_ARGS__) +#else +#define debug(...) 0 +#endif