Skip to content

Commit 772c16e

Browse files
committed
1363
1 parent 8796ed2 commit 772c16e

File tree

3 files changed

+157
-0
lines changed

3 files changed

+157
-0
lines changed

leetcode/1363/0.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[7,1,2,4,0,0,4,0,3,8]

leetcode/1363/main.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
3+
*/
4+
5+
#include <bits/stdc++.h>
6+
7+
#include "../../lib/LeetCodeInputTemplate.hpp"
8+
#include "solution.cpp"
9+
using namespace std;
10+
11+
typedef vector<int> vi;
12+
typedef vector<vector<int>> vvi;
13+
14+
int main() {
15+
LeetCodeInput li("0.in");
16+
17+
int i = 0;
18+
auto l0 = li.get<vi>(i++);
19+
20+
chrono::steady_clock::time_point begin = chrono::steady_clock::now();
21+
Solution solution;
22+
auto output = solution.largestMultipleOfThree(l0);
23+
chrono::steady_clock::time_point end = chrono::steady_clock::now();
24+
cout << "Time difference = "
25+
<< chrono::duration_cast<chrono::microseconds>(end - begin).count()
26+
<< "µs" << endl;
27+
28+
cout << output << endl;
29+
30+
// for (int i: output){
31+
// cout << i << ' ';
32+
// }
33+
34+
// for (auto i: output){
35+
// for (int j: i){
36+
// cout << j << ' ';
37+
// }
38+
// cout << '\n';
39+
// }
40+
41+
return 0;
42+
}

leetcode/1363/solution.cpp

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
3+
time: O(n)
4+
space: O(n)
5+
6+
Runtime
7+
18 ms
8+
Beats
9+
95.53%
10+
Memory
11+
19.3 MB
12+
Beats
13+
92.28%
14+
*/
15+
16+
#include <bits/stdc++.h>
17+
18+
using namespace std;
19+
20+
#define all(x) begin(x), end(x)
21+
typedef vector<int> vi;
22+
typedef vector<vector<int>> vvi;
23+
24+
class Solution {
25+
public:
26+
string largestMultipleOfThree(vector<int>& digits) {
27+
int n = size(digits);
28+
auto sum = accumulate(all(digits), 0);
29+
auto rem_sum = sum % 3;
30+
31+
int rtrim = 0;
32+
if (rem_sum != 0) {
33+
// find the two least num
34+
int m1_1 = 10;
35+
int m1_2 = 10;
36+
int m2_1 = 10;
37+
int m2_2 = 10;
38+
int m1_1_index;
39+
int m1_2_index;
40+
int m2_1_index;
41+
int m2_2_index;
42+
43+
for (int i = 0; i < n; ++i) {
44+
auto digit = digits[i];
45+
if (digit % 3 == 1) {
46+
if (digit < m1_1) {
47+
m1_2 = m1_1;
48+
m1_2_index = m1_1_index;
49+
m1_1 = digit;
50+
m1_1_index = i;
51+
} else if (digit == m1_1 && digit < m1_2) {
52+
m1_2 = digit;
53+
m1_2_index = i;
54+
}
55+
} else if (digit % 3 == 2) {
56+
if (digit < m2_1) {
57+
m2_2 = m2_1;
58+
m2_2_index = m2_1_index;
59+
m2_1 = digit;
60+
m2_1_index = i;
61+
} else if (digit == m2_1 && digit < m2_2) {
62+
m2_2 = digit;
63+
m2_2_index = i;
64+
}
65+
}
66+
}
67+
68+
/// replace the 1 or 2 least num with 0
69+
if (rem_sum == 1) {
70+
if (m1_1 != 10) {
71+
digits[m1_1_index] = 0;
72+
rtrim = 1;
73+
} else if (m2_1 != 10 && m2_2 != 10) {
74+
digits[m2_1_index] = 0;
75+
digits[m2_2_index] = 0;
76+
rtrim = 2;
77+
} else {
78+
return "";
79+
}
80+
} else if (rem_sum == 2) {
81+
if (m2_1 != 10) {
82+
digits[m2_1_index] = 0;
83+
rtrim = 1;
84+
} else if (m1_1 != 10 && m1_2 != 10) {
85+
digits[m1_1_index] = 0;
86+
digits[m1_2_index] = 0;
87+
rtrim = 2;
88+
} else {
89+
return "";
90+
}
91+
}
92+
}
93+
94+
if (n == rtrim) return "";
95+
96+
sort(all(digits), greater());
97+
if (digits[0] == 0) return "0";
98+
99+
/// trim the replaced 0, and cast
100+
string res;
101+
res.reserve(n - rtrim);
102+
for (int i = 0; i < n - rtrim; ++i) {
103+
res += digits[i] + 48;
104+
}
105+
return res;
106+
}
107+
};
108+
109+
const static auto initialize = [] {
110+
std::ios::sync_with_stdio(false);
111+
std::cin.tie(nullptr);
112+
std::cout.tie(nullptr);
113+
return nullptr;
114+
}();

0 commit comments

Comments
 (0)