Using greedy without proof
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.
Build confidence with an intuition-first walkthrough focused on greedy fundamentals.
You are given a string time in the form of hh:mm, where some of the digits in the string are hidden (represented by ?).
The valid times are those inclusively between 00:00 and 23:59.
Return the latest valid time you can get from time by replacing the hidden digits.
Example 1:
Input: time = "2?:?0" Output: "23:50" Explanation: The latest hour beginning with the digit '2' is 23 and the latest minute ending with the digit '0' is 50.
Example 2:
Input: time = "0?:3?" Output: "09:39"
Example 3:
Input: time = "1?:22" Output: "19:22"
Constraints:
time is in the format hh:mm.Problem summary: You are given a string time in the form of hh:mm, where some of the digits in the string are hidden (represented by ?). The valid times are those inclusively between 00:00 and 23:59. Return the latest valid time you can get from time by replacing the hidden digits.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Greedy
"2?:?0"
"0?:3?"
"1?:22"
number-of-valid-clock-times)latest-time-you-can-obtain-after-replacing-characters)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1736: Latest Time by Replacing Hidden Digits
class Solution {
public String maximumTime(String time) {
char[] t = time.toCharArray();
if (t[0] == '?') {
t[0] = t[1] >= '4' && t[1] <= '9' ? '1' : '2';
}
if (t[1] == '?') {
t[1] = t[0] == '2' ? '3' : '9';
}
if (t[3] == '?') {
t[3] = '5';
}
if (t[4] == '?') {
t[4] = '9';
}
return new String(t);
}
}
// Accepted solution for LeetCode #1736: Latest Time by Replacing Hidden Digits
func maximumTime(time string) string {
t := []byte(time)
if t[0] == '?' {
if t[1] >= '4' && t[1] <= '9' {
t[0] = '1'
} else {
t[0] = '2'
}
}
if t[1] == '?' {
if t[0] == '2' {
t[1] = '3'
} else {
t[1] = '9'
}
}
if t[3] == '?' {
t[3] = '5'
}
if t[4] == '?' {
t[4] = '9'
}
return string(t)
}
# Accepted solution for LeetCode #1736: Latest Time by Replacing Hidden Digits
class Solution:
def maximumTime(self, time: str) -> str:
t = list(time)
if t[0] == '?':
t[0] = '1' if '4' <= t[1] <= '9' else '2'
if t[1] == '?':
t[1] = '3' if t[0] == '2' else '9'
if t[3] == '?':
t[3] = '5'
if t[4] == '?':
t[4] = '9'
return ''.join(t)
// Accepted solution for LeetCode #1736: Latest Time by Replacing Hidden Digits
struct Solution;
impl Solution {
fn maximum_time(time: String) -> String {
let time: Vec<char> = time.chars().collect();
let mut max = 0;
for i in 0..24 {
'outer: for j in 0..60 {
let t = i * 60 + j;
let s = format!("{:02}:{:02}", i, j);
let s: Vec<char> = s.chars().collect();
for i in 0..5 {
if time[i] != '?' && time[i] != s[i] {
continue 'outer;
}
}
max = max.max(t);
}
}
format!("{:02}:{:02}", max / 60, max % 60)
}
}
#[test]
fn test() {
let time = "2?:?0".to_string();
let res = "23:50".to_string();
assert_eq!(Solution::maximum_time(time), res);
let time = "0?:3?".to_string();
let res = "09:39".to_string();
assert_eq!(Solution::maximum_time(time), res);
let time = "1?:22".to_string();
let res = "19:22".to_string();
assert_eq!(Solution::maximum_time(time), res);
}
// Accepted solution for LeetCode #1736: Latest Time by Replacing Hidden Digits
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #1736: Latest Time by Replacing Hidden Digits
// class Solution {
// public String maximumTime(String time) {
// char[] t = time.toCharArray();
// if (t[0] == '?') {
// t[0] = t[1] >= '4' && t[1] <= '9' ? '1' : '2';
// }
// if (t[1] == '?') {
// t[1] = t[0] == '2' ? '3' : '9';
// }
// if (t[3] == '?') {
// t[3] = '5';
// }
// if (t[4] == '?') {
// t[4] = '9';
// }
// return new String(t);
// }
// }
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.