aboutsummaryrefslogtreecommitdiff
path: root/binary_watch.c
blob: 67ac9d05dad0940aaeba90f684e84514e1501ed8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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);
}