forked from Sunchit/Coding-Decoded
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLargestTimeForGivenDigits.java
55 lines (44 loc) · 1.37 KB
/
LargestTimeForGivenDigits.java
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class Solution {
// 0 1 2 3
// max val (i +j + k) +l = 6
// l = 6 -i - j - k
public String largestTimeFromDigits(int[] A) {
int maxTime = -1;
int maxHours = -1;
int maxMin = -1;
for(int i=0;i<A.length;i++){
for(int j=0;j<A.length;j++){
for(int k=0;k<A.length;k++){
if(i==j || j==k || k==i){
continue;
}
int hoursTens = A[i];
int hoursUnits = A[j];
int minsTens = A[k];
int minsUnit = A[6-i-j-k];
int hour = hoursTens*10 + hoursUnits;
int min = minsTens * 10+ minsUnit;
int totalTime = hour*60 + min;
if(hour<24 && min< 60 && (totalTime)> maxTime){
maxTime = totalTime;
maxHours = hour;
maxMin = min;
}
}
}
}
if(maxHours == -1 || maxMin == -1){
return "";
} else {
// 9:45 => 09:45 , 8:08
return padding(maxHours) + ":" + padding(maxMin);
}
}
private String padding(int no){
String str = ""+no;
if(str.length()==1){
str = "0" + str;
}
return str;
}
}