-
Notifications
You must be signed in to change notification settings - Fork 0
/
largest_time_for_given_digits.js
82 lines (77 loc) · 2.24 KB
/
largest_time_for_given_digits.js
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// https://leetcode.com/problems/largest-time-for-given-digits/description/
// 949. Largest Time for Given Digits
/**
* @param {number[]} arr
* @return {string}
*/
var largestTimeFromDigits = function(arr) {
function minute_first_digit(arr, res) {
return res+arr[0].toString()
};
function minute_second_digit(arr, res) {
let out = '';
for(let i=5; i>-1; i--){
let pos = arr.indexOf(i);
if(pos > -1){
arr.splice(pos, 1);
out = minute_first_digit(arr,res+i.toString());
break
}
}
return out
};
function hour_first_digit(arr, res) {
let out = '';
for(let i=(res === '2' ? 3 : 9); i>-1; i--){
let pos = arr.indexOf(i);
if(pos > -1){
arr.splice(pos, 1);
out = minute_second_digit(arr,res+i.toString()+':');
break
}
}
return out
};
let out = '';
for(let i=2; i>-1; i--){
let pos = arr.indexOf(i);
if(pos > -1){
arr.splice(pos, 1);
out = hour_first_digit(arr,i.toString());
break
}
}
return out
};
/**
* @param {number[]} arr
* @return {string}
*/
var largestTimeFromDigitsBF = function(arr) {
let comp = arr.sort().join('');
for(let i = 23; i >= 0; i--){
for(let j = 59; j >= 0; j--){
if(comp === [Math.floor(i / 10), i % 10, Math.floor(j / 10), j % 10].sort().join(''))
return '' + Math.floor(i / 10) + i % 10 + ':' + Math.floor(j / 10) + j % 10;
}
}
return '';
};
console.log('Largest Time for Given Digits');
console.log(largestTimeFromDigits([1,5,3,4]));
// Output: "15:43"
console.log(largestTimeFromDigits([1,2,3,4]));
// Output: "23:41"
console.log(largestTimeFromDigits([5,5,5,5]));
// Output: ""
console.log(largestTimeFromDigits([0,9,9,9]));
// Output: ""
console.log('Largest Time for Given Digits (Brute Force)');
console.log(largestTimeFromDigitsBF([1,5,3,4]));
// Output: "15:43"
console.log(largestTimeFromDigitsBF([1,2,3,4]));
// Output: "23:41"
console.log(largestTimeFromDigitsBF([5,5,5,5]));
// Output: ""
console.log(largestTimeFromDigitsBF([0,9,9,9]));
// Output: ""