🤖המדריך להייטיקיסט המתחיל
קלLeetCode #1736

Latest Time by Replacing Hidden Digits

עשויה להישאל ב:Checkpoint

תיאור השאלה

You are given a string time in the form hh:mm, where some digits are hidden (represented by ?). Return the latest valid time you can get by replacing the hidden digits.

איך פותרים?

Fill each unknown digit greedily from left to right with the largest value that keeps the time valid.

פתרון קוד

JavaScript
Python
function maximumTime(time) {
  const chars = time.split("");
  if (chars[0] === "?") {
    chars[0] = chars[1] === "?" || chars[1] <= "3" ? "2" : "1";
  }
  if (chars[1] === "?") {
    chars[1] = chars[0] === "2" ? "3" : "9";
  }
  if (chars[3] === "?") {
    chars[3] = "5";
  }
  if (chars[4] === "?") {
    chars[4] = "9";
  }
  return chars.join("");
}

הסבר וידאו כיצד לפתור את השאלה

Book Cover

לעבור את ראיון העבודה הבא שלך בהצלחה

קורס דיגיטלי מקיף עם +25 שיעורים מעשיים, כשעתיים של וידאו, וליווי של מראיין בכיר.

התחל עכשיו ב-149 ₪ בלבד! 🚀