LeetCode 401 | Binary Watch | Java | Easy | Bit Manipulation & Clock Enumeration | CodeByTushu
Description: π Welcome Back to CodeByTushu!
In todayβs video, we solve LeetCode 401 β Binary Watch using Bit Manipulation and Backtracking in Java.
We are given a binary watch with 4 LEDs at the top representing hours from 0 to 11 and 6 LEDs at the bottom representing minutes from 0 to 59. Each LED represents a binary bit, where 1 means on and 0 means off. Given an integer named turnedOn, which indicates the number of LEDs that are lit, we need to return all valid clock times the watch can represent.
Instead of generating combinations using recursive backtracking across all 10 LEDs, we use a simpler approach. The total number of valid clock times is limited to just 720 combinations, with 12 possible hours and 60 possible minutes.
We iterate through all 720 hour and minute pairs and count the set bits in each hour and minute using Java's Integer.bitCount method. If the combined bit count equals turnedOn, we format the time with two-digit minute padding and add it to our answer list.
The maximum number of lit LEDs for a valid time is 8, because hour 11 uses at most 3 LEDs and minute 59 uses at most 5 LEDs. Therefore, any input greater than 8 immediately returns an empty list. This solution uses O(1) time and O(1) auxiliary space because the search space is fixed.
π₯ Problem Highlights
β Optimized finite search space with 12 hours multiplied by 60 minutes, giving 720 states
β Counted set bits using Java's Integer.bitCount method
β Applied early exit when turnedOn is greater than 8
β Formatted time strings with two-digit minute padding
β Avoided recursive stack overhead by using simple iteration
β Achieved O(1) time complexity and O(1) auxiliary space
β±οΈ TIMESTAMPS
00:00 Intro and Problem Overview
01:29 Binary Watch Layout and Formatting Rules
03:10 720-State Bounded Space and Popcount Insight
05:04 Nested Loop Enumeration and Popcount Matching
06:20 Java Solution Walkthrough Line by Line
07:38 Test Cases Walkthrough and Simulation
09:04 Time Complexity Analysis O(1)
10:03 Space Complexity Analysis O(1)
10:47 Outro and Wrap-up
π Problem Folder and Detailed Documentation:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/tree/main/solutions/February/17-February/401-Binary-Watch
β Java Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/February/17-February/401-Binary-Watch/Solution.java
π Python Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/February/17-February/401-Binary-Watch/solution.py
β‘ C++ Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/February/17-February/401-Binary-Watch/solution.cpp
π JavaScript Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/February/17-February/401-Binary-Watch/solution.js
π LeetCode Source Code
π Add the link here
π All LeetCode Solutions
π https://codebytushu.com/Leetcode/
π± Join CodeByTushu on WhatsApp
Get coding updates, LeetCode solutions, daily questions and more.
https://whatsapp.com/channel/0029Vb8k5mCDeON2Xwlkxe40
β€οΈ Donate
π https://codebytushu.com/Leetcode/donate.php
π₯ Perfect For
LeetCode Easy Problems
Java DSA Preparation
Bit Manipulation and Population Count
Clock Simulation and Formatting
Finite State Enumeration
Coding Interview Preparation
Problem Solving
π Follow CodeByTushu
βΆοΈ YouTube: https://www.youtube.com/@codebytushu
πΈ Instagram: https://instagram.com/codebytushu/
πΌ LinkedIn: https://www.linkedin.com/company/codebytushu/
π Website: https://codebytushu.com
π GitHub: https://github.com/CodeByTushu
β€οΈ Donβt Forget To
π Like | π¬ Comment | π Subscribe
π¬ Question of the Day
What is the maximum number of LEDs that can be lit simultaneously for any valid time on this binary watch?
1οΈβ£ 10 LEDs
2οΈβ£ 8 LEDs, with 3 for hour 11 and 5 for minute 59
3οΈβ£ 7 LEDs
4οΈβ£ 6 LEDs
#LeetCode401 #LeetCode #BinaryWatch #Java #JavaDSA #BitManipulation #Backtracking #DSA #CodingInterview #ProblemSolving #LeetCodeEasy #DailyLeetCode #JavaProgramming #CodeByTushu
π Comment the answer below! π₯Β Close description
In todayβs video, we solve LeetCode 401 β Binary Watch using Bit Manipulation and Backtracking in Java.
We are given a binary watch with 4 LEDs at the top representing hours from 0 to 11 and 6 LEDs at the bottom representing minutes from 0 to 59. Each LED represents a binary bit, where 1 means on and 0 means off. Given an integer named turnedOn, which indicates the number of LEDs that are lit, we need to return all valid clock times the watch can represent.
Instead of generating combinations using recursive backtracking across all 10 LEDs, we use a simpler approach. The total number of valid clock times is limited to just 720 combinations, with 12 possible hours and 60 possible minutes.
We iterate through all 720 hour and minute pairs and count the set bits in each hour and minute using Java's Integer.bitCount method. If the combined bit count equals turnedOn, we format the time with two-digit minute padding and add it to our answer list.
The maximum number of lit LEDs for a valid time is 8, because hour 11 uses at most 3 LEDs and minute 59 uses at most 5 LEDs. Therefore, any input greater than 8 immediately returns an empty list. This solution uses O(1) time and O(1) auxiliary space because the search space is fixed.
π₯ Problem Highlights
β Optimized finite search space with 12 hours multiplied by 60 minutes, giving 720 states
β Counted set bits using Java's Integer.bitCount method
β Applied early exit when turnedOn is greater than 8
β Formatted time strings with two-digit minute padding
β Avoided recursive stack overhead by using simple iteration
β Achieved O(1) time complexity and O(1) auxiliary space
β±οΈ TIMESTAMPS
00:00 Intro and Problem Overview
01:29 Binary Watch Layout and Formatting Rules
03:10 720-State Bounded Space and Popcount Insight
05:04 Nested Loop Enumeration and Popcount Matching
06:20 Java Solution Walkthrough Line by Line
07:38 Test Cases Walkthrough and Simulation
09:04 Time Complexity Analysis O(1)
10:03 Space Complexity Analysis O(1)
10:47 Outro and Wrap-up
π Problem Folder and Detailed Documentation:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/tree/main/solutions/February/17-February/401-Binary-Watch
β Java Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/February/17-February/401-Binary-Watch/Solution.java
π Python Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/February/17-February/401-Binary-Watch/solution.py
β‘ C++ Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/February/17-February/401-Binary-Watch/solution.cpp
π JavaScript Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/February/17-February/401-Binary-Watch/solution.js
π LeetCode Source Code
π Add the link here
π All LeetCode Solutions
π https://codebytushu.com/Leetcode/
π± Join CodeByTushu on WhatsApp
Get coding updates, LeetCode solutions, daily questions and more.
https://whatsapp.com/channel/0029Vb8k5mCDeON2Xwlkxe40
β€οΈ Donate
π https://codebytushu.com/Leetcode/donate.php
π₯ Perfect For
LeetCode Easy Problems
Java DSA Preparation
Bit Manipulation and Population Count
Clock Simulation and Formatting
Finite State Enumeration
Coding Interview Preparation
Problem Solving
π Follow CodeByTushu
βΆοΈ YouTube: https://www.youtube.com/@codebytushu
πΈ Instagram: https://instagram.com/codebytushu/
πΌ LinkedIn: https://www.linkedin.com/company/codebytushu/
π Website: https://codebytushu.com
π GitHub: https://github.com/CodeByTushu
β€οΈ Donβt Forget To
π Like | π¬ Comment | π Subscribe
π¬ Question of the Day
What is the maximum number of LEDs that can be lit simultaneously for any valid time on this binary watch?
1οΈβ£ 10 LEDs
2οΈβ£ 8 LEDs, with 3 for hour 11 and 5 for minute 59
3οΈβ£ 7 LEDs
4οΈβ£ 6 LEDs
#LeetCode401 #LeetCode #BinaryWatch #Java #JavaDSA #BitManipulation #Backtracking #DSA #CodingInterview #ProblemSolving #LeetCodeEasy #DailyLeetCode #JavaProgramming #CodeByTushu
π Comment the answer below! π₯Β Close description
