Skip to content

Commit ec536c0

Browse files
committed
no message
1 parent 158fa82 commit ec536c0

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

problems/binary-watch.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""
2+
Time: O(1)
3+
Space: O(1), no "extra" space are used.
4+
"""
5+
class Solution(object):
6+
def readBinaryWatch(self, turnedOn):
7+
ans = []
8+
if turnedOn>8: return ans #at most 8 LED are turned on for a valid time.
9+
10+
for h in xrange(12):
11+
for m in xrange(60):
12+
if (bin(h) + bin(m)).count('1')==turnedOn:
13+
ans.append('%d:%02d' % (h, m))
14+
return ans
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""
2+
Time: O(N^2)
3+
Space: O(N)
4+
5+
People need to be exactly insert to index k, so that there are "exact" k people equal or taller to k.
6+
Shorter person does not matter (invisible) to taller person, so insert taller person first.
7+
People does not care about the people on their right, so insert the person with smaller k first.
8+
"""
9+
class Solution(object):
10+
def reconstructQueue(self, people):
11+
people.sort(key=lambda x: (-x[0], x[1]))
12+
ans = []
13+
for h, k in people:
14+
ans.insert(k, (h, k))
15+
return ans

0 commit comments

Comments
 (0)