Skip to content

Commit f433690

Browse files
committed
Max Points on a Line/ Compare Version Numbers
1 parent 6ea68a3 commit f433690

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

149 Max Points on a Line.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'''
2+
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
3+
'''
4+
5+
# Definition for a point.
6+
class Point(object):
7+
def __init__(self, a=0, b=0):
8+
self.x = a
9+
self.y = b
10+
11+
12+
class Solution(object):
13+
def maxPoints(self, points):
14+
"""
15+
:type points: List[Point]
16+
:rtype: int
17+
"""
18+
n = len(points)
19+
slope_map = {}
20+
result = 0
21+
for i in range(n):
22+
slope_map.clear()
23+
same, vertical = 1, 0
24+
slope_max = 0
25+
for j in range(i + 1, n):
26+
dx, dy = points[i].x - points[j].x, points[i].y - points[j].y
27+
if dx == dy == 0:
28+
same += 1
29+
elif dx == 0:
30+
vertical += 1
31+
else:
32+
slope = float(dy) / float(dx)
33+
slope_map[slope] = slope_map.get(slope, 0) + 1
34+
slope_max = max(slope_max, slope_map[slope])
35+
result = max(result, max(slope_max, vertical) + same)
36+
return result
37+
38+
39+
if __name__ == "__main__":
40+
None

165 Compare Version Numbers.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'''
2+
Compare two version numbers version1 and version2.
3+
If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0.
4+
5+
You may assume that the version strings are non-empty and contain only digits and the . character.
6+
The . character does not represent a decimal point and is used to separate number sequences.
7+
For instance, 2.5 is not "two and a half" or "half way to version three", it is the fifth second-level revision of the second first-level revision.
8+
9+
Here is an example of version numbers ordering:
10+
11+
0.1 < 1.1 < 1.2 < 13.37
12+
'''
13+
14+
class Solution(object):
15+
def compareVersion(self, version1, version2):
16+
"""
17+
:type version1: str
18+
:type version2: str
19+
:rtype: int
20+
"""
21+
version1_list = version1.split(".")
22+
version2_list = version2.split(".")
23+
len1 = len(version1_list)
24+
len2 = len(version2_list)
25+
for i in range(max(len1, len2)):
26+
v1 = int(version1_list[i]) if i < len1 else 0
27+
v2 = int(version2_list[i]) if i < len2 else 0
28+
if v1 != v2:
29+
return 1 if v1 > v2 else -1
30+
return 0
31+
32+
33+
if __name__ == "__main__":
34+
assert Solution().compareVersion("0.1", "1.1") == -1
35+
assert Solution().compareVersion("01.2", "1.1") == 1
36+
assert Solution().compareVersion("2.3", "2.3") == 3

0 commit comments

Comments
 (0)