-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
322 additions
and
0 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
solved/LeetCode/Contests/Biweekly Contest 42/.vscode/settings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"files.associations": { | ||
"array": "cpp", | ||
"bitset": "cpp", | ||
"string_view": "cpp", | ||
"initializer_list": "cpp", | ||
"regex": "cpp", | ||
"utility": "cpp", | ||
"valarray": "cpp" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#include <bits/stdc++.h> | ||
#include "./template.cc" | ||
using namespace std; | ||
|
||
class Solution { | ||
public: | ||
int countStudents(vector<int>& students, vector<int>& sandwiches) { | ||
deque<pair<int, int>> q; | ||
for (int i = 0; i < students.size(); i++) { | ||
q.emplace_back(students[i], i); | ||
} | ||
map<int, set<int>> 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<int> st; | ||
vector<int> sand; | ||
}; | ||
|
||
int main() { | ||
vector<Input> 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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include <bits/stdc++.h> | ||
#include "./template.cc" | ||
using namespace std; | ||
|
||
class Solution { | ||
public: | ||
double averageWaitingTime(vector<vector<int>>& 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<vector<int>> c; | ||
}; | ||
|
||
int main() { | ||
vector<Input> 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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#include <bits/stdc++.h> | ||
#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<Input> in = { | ||
{"000110"}, | ||
{"01"} | ||
}; | ||
|
||
for (auto &it : in) { | ||
Solution sol; | ||
print(cout, sol.maximumBinaryString(it.b)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#include <bits/stdc++.h> | ||
#include "./template.cc" | ||
using namespace std; | ||
|
||
using int64 = long long; | ||
class Solution { | ||
vector<int64> bestLeft(vector<int>& nums, int64 k) { | ||
int n = nums.size(); | ||
int64 best = 10000000000; | ||
vector<int64> ans(n, best); | ||
deque<int64> 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<int>& 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<int> n; | ||
int k; | ||
}; | ||
|
||
int main() { | ||
vector<Input> 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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
int n = nums.size(); | ||
vector<int64> 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<int64>::max(); | ||
for (int i = 0; i < n; i++) { | ||
best = min(best, prev + r[i]); | ||
size += (nums[i]); | ||
prev += size; | ||
} | ||
return best; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#include <bits/stdc++.h> | ||
#include "./template.cc" | ||
using namespace std; | ||
|
||
struct Solution { | ||
|
||
}; | ||
|
||
struct Input { | ||
|
||
}; | ||
|
||
int main() { | ||
vector<Input> in = { | ||
|
||
}; | ||
|
||
for (auto &it : in) { | ||
Solution sol; | ||
// TODO: Method. | ||
sol.method(in); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
void print(std::ostream& out) { out << std::endl; } | ||
|
||
template <typename T, typename T2> | ||
void printOne(std::ostream& out, const std::pair<T, T2>& a) { | ||
out << "{" << a.first << ", " << a.second << "}"; | ||
} | ||
|
||
template <typename T> | ||
void printOne(std::ostream& out, const T& a) { | ||
out << a; | ||
} | ||
|
||
template <typename T> | ||
void printOne(std::ostream& out, const std::vector<T>& a) { | ||
out << "{"; | ||
for (int i = 0; i < a.size(); i++) { | ||
if (i) out << ", "; | ||
printOne(out, a[i]); | ||
} | ||
out << "}"; | ||
} | ||
|
||
template <typename T> | ||
void printOne(std::ostream& out, const std::deque<T>& a) { | ||
out << "{"; | ||
for (int i = 0; i < a.size(); i++) { | ||
if (i) out << ", "; | ||
printOne(out, a[i]); | ||
} | ||
out << "}"; | ||
} | ||
|
||
|
||
template <typename T, typename V> | ||
void printOne(std::ostream& out, const std::map<T, V>& a) { | ||
out << "{"; | ||
for (auto [key, val] : a) { | ||
out << "["; printOne(out, key); out << " = "; printOne(out, val); out << "],"; | ||
} | ||
out << "}"; | ||
} | ||
|
||
|
||
template <typename T, typename... Tail> | ||
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 |