Skip to content

Commit bb576c8

Browse files
authored
Create find-the-number-of-ways-to-place-people-i.py
1 parent c4fc1f7 commit bb576c8

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Time: O(n^2)
2+
# Space: O(1)
3+
4+
# sort, array
5+
class Solution(object):
6+
def numberOfPairs(self, points):
7+
"""
8+
:type points: List[List[int]]
9+
:rtype: int
10+
"""
11+
points.sort(key=lambda x: (x[0], -x[1]))
12+
result = 0
13+
for i in xrange(len(points)):
14+
y = float("-inf")
15+
for j in xrange(i+1, len(points)):
16+
if points[i][1] < points[j][1]:
17+
continue
18+
if points[j][1] > y:
19+
y = points[j][1]
20+
result += 1
21+
return result
22+
23+
24+
# Time: O(n^3)
25+
# Space: O(1)
26+
# sort, array
27+
class Solution2(object):
28+
def numberOfPairs(self, points):
29+
"""
30+
:type points: List[List[int]]
31+
:rtype: int
32+
"""
33+
points.sort(key=lambda x: (x[0], -x[1]))
34+
return sum(all(not points[i][1] >= points[k][1] >= points[j][1] for k in xrange(i+1, j))
35+
for i in xrange(len(points))
36+
for j in xrange(i+1, len(points)) if points[i][1] >= points[j][1])
37+

0 commit comments

Comments
 (0)