diff --git a/cpp/2305/LC230504.cpp b/cpp/2305/LC230504.cpp new file mode 100644 index 0000000..84eaf9a --- /dev/null +++ b/cpp/2305/LC230504.cpp @@ -0,0 +1,52 @@ +#include + +/** + * 649. Dota2 Senate + * + * In the world of Dota2, there are two parties: the Radiant and the Dire. + * + * The Dota2 senate consists of senators coming from two parties. Now the Senate wants to decide on a change in the Dota2 game. The voting for this change is a round-based procedure. In each round, each senator can exercise one of the two rights: + * + * Ban one senator's right: A senator can make another senator lose all his rights in this and all the following rounds. + * Announce the victory: If this senator found the senators who still have rights to vote are all from the same party, he can announce the victory and decide on the change in the game. + * Given a string senate representing each senator's party belonging. The character 'R' and 'D' represent the Radiant party and the Dire party. Then if there are n senators, the size of the given string will be n. + * + * The round-based procedure starts from the first senator to the last senator in the given order. This procedure will last until the end of voting. All the senators who have lost their rights will be skipped during the procedure. + * + * Suppose every senator is smart enough and will play the best strategy for his own party. Predict which party will finally announce the victory and change the Dota2 game. The output should be "Radiant" or "Dire". + */ + +class LC230504 { +public: + static std::string predictPartyVictory(std::string) noexcept; +}; + +std::string LC230504::predictPartyVictory(std::string senate) noexcept { + int rem[2]{}; + for (int i = senate.size() - 1; i >= 0; --i) + ++rem[senate[i] == 'D']; + while (rem[0] && rem[1]) { + for (int i = 0; i < senate.size(); ++i) { + if (senate[i] == 'R') { + auto pos = senate.find_first_of('D', i); + if (pos == std::string::npos) + pos = senate.find_first_of('D'); + if (pos != std::string::npos) { + senate[pos] = '_'; + --rem[1]; + } + } else if (senate[i] == 'D') { + auto pos = senate.find_first_of('R', i); + if (pos == std::string::npos) + pos = senate.find_first_of('R'); + if (pos != std::string::npos) { + senate[pos] = '_'; + --rem[0]; + } + } + if (!rem[0] || !rem[1]) + break; + } + } + return rem[1] ? "Dire" : "Radiant"; +} diff --git a/cpp/2305/LC230504CN.cpp b/cpp/2305/LC230504CN.cpp new file mode 100644 index 0000000..17570e5 --- /dev/null +++ b/cpp/2305/LC230504CN.cpp @@ -0,0 +1,35 @@ +#include +#include + +/** + * 2106. Maximum Fruits Harvested After at Most K Steps + * + * Fruits are available at some positions on an infinite x-axis. You are given a 2D integer array fruits where fruits[i] = [positioni, amounti] depicts amounti fruits at the position positioni. fruits is already sorted by positioni in ascending order, and each positioni is unique. + * You are also given an integer startPos and an integer k. Initially, you are at the position startPos. From any position, you can either walk to the left or right. It takes one step to move one unit on the x-axis, and you can walk at most k steps in total. For every position you reach, you harvest all the fruits at that position, and the fruits will disappear from that position. + * Return the maximum total number of fruits you can harvest. + */ + +class LC230504CN { +public: + static int maxTotalFruits(const std::vector>&, int, int) noexcept; +}; + +constexpr inline int calc_step(int L, int R, int sp) noexcept { + // Assume that L <= R + if (R < sp) + return sp - L; + if (sp < L) + return R - sp; + return std::min(sp - L + (R - sp) * 2, R - sp + (sp - L) * 2); +} + +int LC230504CN::maxTotalFruits(const std::vector>& f, int sp, int k) noexcept { + int ret = 0, n = f.size(); + for (int L = 0, R = 0, sumL = 0, sumR = 0; R < n; ++R) { + for (sumR += f[R][1]; L < R && calc_step(f[L][0], f[R][0], sp) > k; ++L) + sumL += f[L][1]; + if (calc_step(f[L][0], f[R][0], sp) <= k) + ret = std::max(ret, sumR - sumL); + } + return ret; +} diff --git a/cpp/2305/LC230505.cpp b/cpp/2305/LC230505.cpp new file mode 100644 index 0000000..44c5223 --- /dev/null +++ b/cpp/2305/LC230505.cpp @@ -0,0 +1,22 @@ +#include +#include + +class LC230505 { +public: + static int maxVowels(const std::string&, int) noexcept; +}; + +constexpr inline int verdict(char ch) noexcept { + return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'; +} + +int LC230505::maxVowels(const std::string& s, int k) noexcept { + const int n = s.length(); + int qualified = 0; + for (int i = 0; i < k; ++i) + qualified += verdict(s[i]); + int ret = qualified; + for (int i = k; i < n; ++i) + ret = std::max(ret, qualified += verdict(s[i]) - verdict(s[i - k])); + return ret; +} diff --git a/cpp/2305/LC230505CN.cpp b/cpp/2305/LC230505CN.cpp new file mode 100644 index 0000000..f9e4a6f --- /dev/null +++ b/cpp/2305/LC230505CN.cpp @@ -0,0 +1,17 @@ +#include + +class LC230505CN { +public: + static int hardestWorker(int, const std::vector>&) noexcept; +}; + +int LC230505CN::hardestWorker(int, const std::vector>& logs) noexcept { + int id = logs[0][0], time = logs[0][1]; + for (int i = 1; i < logs.size(); ++i) { + if (int delta = logs[i][1] - logs[i - 1][1]; delta > time || (delta == time && logs[i][0] < id)) { + time = delta; + id = logs[i][0]; + } + } + return id; +} diff --git a/cpp/2305/defs.h b/cpp/2305/defs.h index 1b24ff2..bb54d22 100644 --- a/cpp/2305/defs.h +++ b/cpp/2305/defs.h @@ -40,4 +40,24 @@ public: static VVI findDifference(CVIR, CVIR) noexcept; }; +class LC230504CN { +public: + static int maxTotalFruits(const std::vector>&, int, int) noexcept; +}; + +class LC230504 { +public: + static std::string predictPartyVictory(std::string) noexcept; +}; + +class LC230505CN { +public: + static int hardestWorker(int, const std::vector>&) noexcept; +}; + +class LC230505 { +public: + static int maxVowels(const std::string&, int) noexcept; +}; + #endif //LEETCODE_CPP_DEFS_H diff --git a/cpp/2305/main.cpp b/cpp/2305/main.cpp index 5cdba9c..f210098 100644 --- a/cpp/2305/main.cpp +++ b/cpp/2305/main.cpp @@ -12,6 +12,8 @@ std::ostream& operator<<(std::ostream& os, const std::vector& x) noexcept { } int main() { - std::cout << LC230503CN::isValid("aabbcc"); + std::cout << LC230504CN::maxTotalFruits({ + {0,3},{6,4},{8,5} + }, 3,2); return 0; }