Skip to content

Commit 55088a3

Browse files
authored
Create largest-time-for-given-digits.cpp
1 parent 5141f22 commit 55088a3

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

C++/largest-time-for-given-digits.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Time: O(1)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
string largestTimeFromDigits(vector<int>& A) {
7+
string result;
8+
sort(A.begin(), A.end(), greater<int>());
9+
do {
10+
if (A[0] * 10 + A[1] < 24 && A[2] < 6) {
11+
for (const auto& i : A) {
12+
if (result.length() == 2) {
13+
result += ":";
14+
}
15+
result += to_string(i);
16+
}
17+
break;
18+
}
19+
} while (next_permutation(A.begin(), A.end(), greater<int>()));
20+
return result;
21+
}
22+
};

0 commit comments

Comments
 (0)