aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-03-12 13:37:35 -0600
committerJack Sangdahl <[email protected]>2025-03-12 13:37:35 -0600
commitc4e135086dbef6281031947b44cab3379afe404e (patch)
tree02402cbbb3454836f4d2c494b2becaa8070a170e
parentb477c5d538f1fef91dbf906a17411eef32b2ded2 (diff)
downloadleetcode-c4e135086dbef6281031947b44cab3379afe404e.tar.gz
leetcode-c4e135086dbef6281031947b44cab3379afe404e.zip
401. binary watch
-rw-r--r--binary_watch.c46
-rw-r--r--binary_watch.py30
2 files changed, 76 insertions, 0 deletions
diff --git a/binary_watch.c b/binary_watch.c
new file mode 100644
index 0000000..67ac9d0
--- /dev/null
+++ b/binary_watch.c
@@ -0,0 +1,46 @@
+// 401. Binary Watch
+#include "leetcode.h"
+
+/*
+ * 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 0
+ * or one, with the least siginificant bit on the right. given an integer
+ * 'turnedOn' which represents the number of leds that are currently on, return
+ * all possible times the watch could represent. you may return the answer in
+ * any order.
+ */
+
+char **readBinaryWatch(int turnedOn, int *returnSize) {
+ char **ans = (char **)malloc(190 * sizeof(char *));
+ *returnSize = 0;
+ for (int h = 0; h < 12; h++)
+ for (int m = 0; m < 60; m++) {
+ int total = __builtin_popcount(h) + __builtin_popcount(m);
+ if (total == turnedOn) {
+ ans[*returnSize] = (char *)malloc(8 * sizeof(char));
+ snprintf(ans[*returnSize], 8, "%d:%02d", h, m);
+ (*returnSize)++;
+ }
+ }
+ return ans;
+}
+
+int main() {
+ int rs1, rs2;
+ char **rbw1 = readBinaryWatch(1, &rs1);
+ char **rbw2 = readBinaryWatch(9, &rs2);
+ for (int i = 0; i < rs1; i++)
+ printf(
+ "%s ",
+ rbw1[i]); // expect: 0:01,0:02,0:04,0:08,0:16,0:32,1:00,2:00,4:00,8:00
+ printf("\n");
+ for (int i = 0; i < rs2; i++)
+ printf("%s ", rbw2[i]); // expect: null
+ printf("\n");
+ for (int i = 0; i < rs1; i++)
+ free(rbw1[i]);
+ free(rbw1);
+ for (int i = 0; i < rs2; i++)
+ free(rbw2[i]);
+ free(rbw2);
+}
diff --git a/binary_watch.py b/binary_watch.py
new file mode 100644
index 0000000..f7af950
--- /dev/null
+++ b/binary_watch.py
@@ -0,0 +1,30 @@
+# 401. Binary Watch
+
+"""
+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 0
+or one, with the least siginificant bit on the right. given an integer
+'turnedOn' which represents the number of leds that are currently on, return
+all possible times the watch could represent. you may return the answer in
+any order.
+"""
+
+
+class Solution(object):
+ def readBinaryWatch(self, turnedOn):
+ """
+ :type turnedOn: int
+ :rtype: List[str]
+ """
+ return [
+ "%d:%02d" % (h, m)
+ for h in range(12)
+ for m in range(60)
+ if (bin(h) + bin(m)).count("1") == turnedOn
+ ]
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.readBinaryWatch(turnedOn=1))
+ print(obj.readBinaryWatch(turnedOn=9))