diff --git a/daily/Sep13.cc b/daily/Sep13.cc new file mode 100644 index 0000000..9e2c8cd --- /dev/null +++ b/daily/Sep13.cc @@ -0,0 +1,33 @@ +#include +#include +using namespace std; + +class Solution { + public: + int maximumRobots(vector& chargeTimes, vector& runningCosts, + long long budget) { + int ans = 0, le = 0; + long long sum = 0; + deque dq; + for (int ri = 0; ri < chargeTimes.size(); ri++) { + // enque + while (!dq.empty() && chargeTimes[ri] >= chargeTimes[dq.back()]) { + dq.pop_back(); + } + dq.push_back(ri); + sum += runningCosts[ri]; + + // deque + while (!dq.empty() && + chargeTimes[dq.front()] + (ri - le + 1) * sum > budget) { + if (dq.front() == le) { + dq.pop_front(); + } + sum -= runningCosts[le++]; + } + + ans = max(ans, ri - le + 1); + } + return ans; + } +}; diff --git a/daily/Sep14.cc b/daily/Sep14.cc new file mode 100644 index 0000000..8ae4ae3 --- /dev/null +++ b/daily/Sep14.cc @@ -0,0 +1,63 @@ +#include +using namespace std; + +class Solution { + public: + string removeStars1(string s) { + string ans; + stack stk; + for (char c : s) { + if (c == '*') { + stk.pop(); + } else { + stk.push(c); + } + } + while (!stk.empty()) { + char c = stk.top(); + ans.push_back(c); + stk.pop(); + } + std::reverse(ans.begin(), ans.end()); + return ans; + } + + string removeStars2(string s) { + string ans; + deque dq; + for (char c : s) { + if (c == '*') { + dq.pop_back(); + } else { + dq.push_back(c); + } + } + + while (!dq.empty()) { + ans.push_back(dq.front()); + dq.pop_front(); + } + return ans; + } + + /** + * @brief remove stars in a string + * Use the stack to solve this problem, however, use stack or deque + * is a little bit complex + * Remember that `string` also has the `pop_back()` api, just use string. + * + * @param s + * @return string + */ + string removeStars3(string s) { + string ans; + for (char c : s) { + if (c == '*') { + ans.pop_back(); + } else { + ans += c; + } + } + return ans; + } +}; diff --git a/daily/Sep8.cc b/daily/Sep8.cc new file mode 100644 index 0000000..2f5b9df --- /dev/null +++ b/daily/Sep8.cc @@ -0,0 +1,26 @@ +#include + +class Solution { +public: + vector sortedSquares(vector nums) { + int len = nums.size(); + for (int i = 0; i < len ; i++) { + nums[i] = nums[i] * nums[i]; + } + + vector ans(len, 0); + int le = 0, ri = len - 1; + int pivot = len - 1; + while (le <= ri) { + if (nums[le] > nums[ri]) { + ans[pivot] = nums[le]; // get larger + le++; + } else { + ans[pivot] = nums[ri]; + ri--; + } + pivot--; + } + return ans; + } +}