Skip to content

Commit ce03cb3

Browse files
committedMar 27, 2025
Add solution #949
1 parent bf297af commit ce03cb3

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,033 LeetCode solutions in JavaScript
1+
# 1,034 LeetCode solutions in JavaScript
22

33
[https://leetcode.com/](https://leetcode.com/)
44

@@ -758,6 +758,7 @@
758758
946|[Validate Stack Sequences](./solutions/0946-validate-stack-sequences.js)|Medium|
759759
947|[Most Stones Removed with Same Row or Column](./solutions/0947-most-stones-removed-with-same-row-or-column.js)|Medium|
760760
948|[Bag of Tokens](./solutions/0948-bag-of-tokens.js)|Medium|
761+
949|[Largest Time for Given Digits](./solutions/0949-largest-time-for-given-digits.js)|Medium|
761762
966|[Vowel Spellchecker](./solutions/0966-vowel-spellchecker.js)|Medium|
762763
970|[Powerful Integers](./solutions/0970-powerful-integers.js)|Easy|
763764
976|[Largest Perimeter Triangle](./solutions/0976-largest-perimeter-triangle.js)|Easy|
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* 949. Largest Time for Given Digits
3+
* https://leetcode.com/problems/largest-time-for-given-digits/
4+
* Difficulty: Medium
5+
*
6+
* Given an array arr of 4 digits, find the latest 24-hour time that can be made using
7+
* each digit exactly once.
8+
*
9+
* 24-hour times are formatted as "HH:MM", where HH is between 00 and 23, and MM is between
10+
* 00 and 59. The earliest 24-hour time is 00:00, and the latest is 23:59.
11+
*
12+
* Return the latest 24-hour time in "HH:MM" format. If no valid time can be made, return
13+
* an empty string.
14+
*/
15+
16+
/**
17+
* @param {number[]} arr
18+
* @return {string}
19+
*/
20+
var largestTimeFromDigits = function(arr) {
21+
let result = '';
22+
helper(arr, new Array(4).fill(false), []);
23+
return result;
24+
25+
function helper(digits, used, current) {
26+
if (current.length === 4) {
27+
const hours = parseInt(current.slice(0, 2).join(''));
28+
const minutes = parseInt(current.slice(2).join(''));
29+
if (hours <= 23 && minutes <= 59) {
30+
const time = `${current[0]}${current[1]}:${current[2]}${current[3]}`;
31+
if (time > result) result = time;
32+
}
33+
return;
34+
}
35+
36+
for (let i = 0; i < digits.length; i++) {
37+
if (!used[i]) {
38+
used[i] = true;
39+
current.push(digits[i]);
40+
helper(digits, used, current);
41+
current.pop();
42+
used[i] = false;
43+
}
44+
}
45+
}
46+
};

0 commit comments

Comments
 (0)
Please sign in to comment.