Skip to content

Latest commit

 

History

History
141 lines (120 loc) · 4.38 KB

README.md

File metadata and controls

141 lines (120 loc) · 4.38 KB

A binary watch has 4 LEDs on the top to represent the hours (0-11), and 6 LEDs on the bottom to represent the minutes (0-59). Each LED represents a zero or one, with the least significant bit on the right.

  • For example, the below binary watch reads "4:51".

Given an integer turnedOn which represents the number of LEDs that are currently on (ignoring the PM), return all possible times the watch could represent. You may return the answer in any order.

The hour must not contain a leading zero.

  • For example, "01:00" is not valid. It should be "1:00".

The minute must consist of two digits and may contain a leading zero.

  • For example, "10:2" is not valid. It should be "10:02".

 

Example 1:

Input: turnedOn = 1
Output: ["0:01","0:02","0:04","0:08","0:16","0:32","1:00","2:00","4:00","8:00"]

Example 2:

Input: turnedOn = 9
Output: []

 

Constraints:

  • 0 <= turnedOn <= 10

Companies: Google

Related Topics:
Backtracking, Bit Manipulation

Similar Questions:

Hints:

  • Simplify by seeking for solutions that involve comparing bit counts.
  • Consider calculating all possible times for comparison purposes.

Solution 1.

// OJ: https://leetcode.com/problems/binary-watch
// Author: github.com/lzl124631x
// Time: O(1)
// Space: O(1)
class Solution {
public:
    vector<string> readBinaryWatch(int turnedOn) {
        vector<string> ans;
        int h = 0, m = 0;
        function<void(int, int)> dfs = [&](int i, int len) {
            if (len == 0) {
                ans.push_back(to_string(h) + ":" + (m < 10 ? "0" : "") + to_string(m));
            }
            if (i > 10) return;
            for (int j = i; j < 10; ++j) {
                int dh = 0, dm = 0;
                if (j < 4) dh += (1 << j);
                else dm += (1 << (j - 4));
                h += dh;
                m += dm;
                if (h < 12 && m < 60) dfs(j + 1, len - 1);
                h -= dh;
                m -= dm;
            }
        };
        dfs(0, turnedOn);
        return ans;
    }
};

Solution 2.

// OJ: https://leetcode.com/problems/binary-watch
// Author: github.com/lzl124631x
// Time: O(1)
// Space: O(1)
class Solution {
    vector<string> ans;
    bool valid(int bits) { return (bits >> 6) < 12 && (bits & ((1 << 6) - 1)) < 60; }
    void read(int bits) {
        int h = bits >> 6, m = bits - (h << 6);
        ans.push_back(to_string(h) + ':' + string(m < 10 ? 1 : 0, '0') + to_string(m));
    }
    void dfs(int num, int start, int bits) {
        if (!num) {
            read(bits);
            return;
        }
        for (int i = start; i < 10; ++i) {
            bits |= 1 << i;
            if (valid(bits)) dfs(num - 1, i + 1, bits);
            bits ^= 1 << i;
        }
    }
public:
    vector<string> readBinaryWatch(int num) {
        dfs(num, 0, 0);
        return ans;
    }
};

Solution 3.

// OJ: https://leetcode.com/problems/binary-watch
// Author: github.com/lzl124631x
// Time: O(1)
// Space: O(1)
// Ref: https://discuss.leetcode.com/topic/59401/straight-forward-6-line-c-solution-no-need-to-explain
class Solution {
public:
    vector<string> readBinaryWatch(int num) {
        vector<string> ans;
        for (int h = 0; h < 12; ++h)
            for (int m = 0; m < 60; ++m)
                if (bitset<10>(h << 6 | m).count() == num)
                    ans.push_back(to_string(h) + (m < 10 ? ":0" : ":") + to_string(m));
        return ans;
    }
};