|
| 1 | +/* |
| 2 | + * @lc app=leetcode id=1363 lang=cpp |
| 3 | + * |
| 4 | + * [1363] Largest Multiple of Three |
| 5 | + * |
| 6 | + * https://leetcode.com/problems/largest-multiple-of-three/description/ |
| 7 | + * |
| 8 | + * algorithms |
| 9 | + * Hard (32.33%) |
| 10 | + * Likes: 84 |
| 11 | + * Dislikes: 19 |
| 12 | + * Total Accepted: 4.9K |
| 13 | + * Total Submissions: 15.1K |
| 14 | + * Testcase Example: '[8,1,9]' |
| 15 | + * |
| 16 | + * Given an integer array of digits, return the largest multiple of three that |
| 17 | + * can be formed by concatenating some of the given digits in any order. |
| 18 | + * |
| 19 | + * Since the answer may not fit in an integer data type, return the answer as a |
| 20 | + * string. |
| 21 | + * |
| 22 | + * If there is no answer return an empty string. |
| 23 | + * |
| 24 | + * |
| 25 | + * Example 1: |
| 26 | + * |
| 27 | + * |
| 28 | + * Input: digits = [8,1,9] |
| 29 | + * Output: "981" |
| 30 | + * |
| 31 | + * |
| 32 | + * Example 2: |
| 33 | + * |
| 34 | + * |
| 35 | + * Input: digits = [8,6,7,1,0] |
| 36 | + * Output: "8760" |
| 37 | + * |
| 38 | + * |
| 39 | + * Example 3: |
| 40 | + * |
| 41 | + * |
| 42 | + * Input: digits = [1] |
| 43 | + * Output: "" |
| 44 | + * |
| 45 | + * |
| 46 | + * Example 4: |
| 47 | + * |
| 48 | + * |
| 49 | + * Input: digits = [0,0,0,0,0,0] |
| 50 | + * Output: "0" |
| 51 | + * |
| 52 | + * |
| 53 | + * |
| 54 | + * Constraints: |
| 55 | + * |
| 56 | + * |
| 57 | + * 1 <= digits.length <= 10^4 |
| 58 | + * 0 <= digits[i] <= 9 |
| 59 | + * The returning answer must not contain unnecessary leading zeros. |
| 60 | + * |
| 61 | + * |
| 62 | + */ |
| 63 | + |
| 64 | +// @lc code=start |
| 65 | +class Solution { |
| 66 | +public: |
| 67 | + string largestMultipleOfThree(vector<int>& digits) { |
| 68 | + vector<int> nums; |
| 69 | + vector<int> ones, twos; |
| 70 | + for(auto e: digits){ |
| 71 | + int r = e%3; |
| 72 | + if(r == 0) |
| 73 | + nums.push_back(e); |
| 74 | + else if(r == 1) |
| 75 | + ones.push_back(e); |
| 76 | + else |
| 77 | + twos.push_back(e); |
| 78 | + } |
| 79 | + sort(ones.begin(), ones.end(), greater<int>()); |
| 80 | + sort(twos.begin(), twos.end(), greater<int>()); |
| 81 | + |
| 82 | + int i = 0, j = 0; |
| 83 | + |
| 84 | + // 0, 2 |
| 85 | + if( (!ones.empty() && ones.size()%3 == 0 && twos.size()%3 == 2) |
| 86 | + || (!twos.empty() && twos.size()%3 == 0 && ones.size()%3 == 2) ){ |
| 87 | + nums.push_back(twos[j++]); |
| 88 | + nums.push_back(ones[i++]); |
| 89 | + nums.push_back(twos[j++]); |
| 90 | + nums.push_back(ones[i++]); |
| 91 | + } |
| 92 | + // 2, 0 |
| 93 | + |
| 94 | + while(i+2<ones.size()) |
| 95 | + for(int k=0; k<3; k++) |
| 96 | + nums.push_back(ones[i++]); |
| 97 | + while(j+2<twos.size()) |
| 98 | + for(int k=0; k<3; k++) |
| 99 | + nums.push_back(twos[j++]); |
| 100 | + |
| 101 | + while(j<twos.size() && i<ones.size()){ |
| 102 | + nums.push_back(twos[j++]); |
| 103 | + nums.push_back(ones[i++]); |
| 104 | + } |
| 105 | + |
| 106 | + sort(nums.begin(), nums.end(), greater<int>()); |
| 107 | + |
| 108 | + if(nums.size() == 0) |
| 109 | + return ""; |
| 110 | + if(nums[0] == 0) |
| 111 | + return "0"; |
| 112 | + |
| 113 | + string ret = ""; |
| 114 | + for(int e: nums){ |
| 115 | + ret += (char)('0'+e); |
| 116 | + } |
| 117 | + return ret; |
| 118 | + } |
| 119 | +}; |
| 120 | +// @lc code=end |
0 commit comments