-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday_01.cpp
More file actions
39 lines (32 loc) · 1.05 KB
/
day_01.cpp
File metadata and controls
39 lines (32 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*PROBLEM STATEMENT:
Given an array of 4 digits, return the largest 24 hour time that can be made.
The smallest 24 hour time is 00:00, and the largest is 23:59. Starting from 00:00, a time is larger if more time has elapsed since midnight.
Return the answer as a string of length 5. If no valid time can be made, return an empty string.
Example 1:
Input: [1,2,3,4]
Output: "23:41"
Example 2:
Input: [5,5,5,5]
Output: ""
*/
//Code
class Solution {
public:
string largestTimeFromDigits(vector<int>& A) {
string result;
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
for(int k=0;k<4;k++){
if(i==j||j==k||k==i) continue;
string hh=to_string(A[i])+to_string(A[j]);
string mm=to_string(A[k])+to_string(A[6-i-j-k]);
string _time = hh+":"+mm;
if(hh<"24" && mm<"60" && _time>result){
result = _time;
}
}
}
}
return result;
}
};