diff options
author | Jack Sangdahl <[email protected]> | 2025-03-10 00:36:13 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-03-10 00:36:13 -0600 |
commit | 7c60dee83b436e8e8a667bf9f191beced8de4fc3 (patch) | |
tree | eb7a24ee2067f92245c57bb118e85a1baf15df50 /other/largest_multiple_three.cpp | |
parent | 5d7cdc215e4ba1ec4cbb24c084cccb5e5e641468 (diff) | |
download | leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.tar.gz leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.zip |
chore: move cpp and rust code to directory
Diffstat (limited to 'other/largest_multiple_three.cpp')
-rw-r--r-- | other/largest_multiple_three.cpp | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/other/largest_multiple_three.cpp b/other/largest_multiple_three.cpp new file mode 100644 index 0000000..1c9e3e1 --- /dev/null +++ b/other/largest_multiple_three.cpp @@ -0,0 +1,37 @@ +#include "leetcode.h" + +class Solution { +public: + string largestMultipleOfThree(vector<int> &digits) { + int n = digits.size(), d[10] = {}, sum = 0; + int r1[] = {1, 4, 7, 2, 5, 8}, r2[] = {2, 5, 8, 1, 4, 7}; + string ans = ""; + for (int i = 0; i < n; i++) { + ++d[digits[i]]; + sum += digits[i]; + } + while (sum % 3 != 0) { + for (int i = 0; i < 6; i++) { + if (sum % 3 == 1 && d[r1[i]]) { + sum -= r1[i]; + d[r1[i]]--; + break; + } else if (sum % 3 == 2 && d[r2[i]]) { + sum -= r2[i]; + d[r2[i]]--; + break; + } + } + } + for (int i = 9; i >= 0; i--) + for (int j = 1; j <= d[i]; j++) + ans += to_string(i); + return ans[0] == '0' ? "0" : ans; + } +}; + +int main() { + Solution obj; + vector<int> digits = {8, 1, 9}; + cout << obj.largestMultipleOfThree(digits); +} |