Skip to content

Commit ba5003f

Browse files
按右侧边界升序排列
1 parent 21dc5c8 commit ba5003f

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
'''
2+
Descripttion: 用最少数量的箭引爆气球
3+
version: 1
4+
Author: Jason
5+
Date: 2020-11-24 14:40:10
6+
LastEditors: Jason
7+
LastEditTime: 2020-11-24 16:00:31
8+
'''
9+
10+
import random
11+
from typing import List
12+
13+
14+
def GenerateRandomList(left_bound, right_bound, numbers):
15+
temp = list()
16+
random_legth = random.randint(0, numbers)
17+
current_length = 0
18+
while current_length < random_legth:
19+
left_point = random.randint(0, left_bound)
20+
right_point = random.randint(left_point + 1, right_bound)
21+
temp.append([left_point, right_point])
22+
current_length += 1
23+
return temp
24+
25+
26+
class Solution:
27+
def findMinArrowShots(self, points: List[List[int]]) -> int:
28+
points.sort(key=lambda point: point[1])
29+
arrows = 0
30+
boundary = float("-inf")
31+
for point in points:
32+
if point[0] > boundary:
33+
arrows += 1
34+
boundary = point[1]
35+
return arrows
36+
37+
def findMinArrowShots2(self, points: List[List[int]]) -> int:
38+
'''
39+
func: 按开始位置排序,维护gap区域,如果当前点不在gap范围内,则arrows+1,且将gap更新为当前点的区间
40+
param {*}
41+
return {*}
42+
'''
43+
if len(points) < 1:
44+
return 0
45+
points.sort()
46+
arrows = 1
47+
gap = [float("-inf"), float("inf")]
48+
for each in points:
49+
if gap[0] <= each[0] <= gap[1] or gap[0] <= each[1] <= gap[1]:
50+
gap = [max(gap[0], each[0]), min(gap[1], each[1])]
51+
continue
52+
gap = each
53+
arrows += 1
54+
return arrows
55+
56+
57+
s = Solution()
58+
for i in range(100):
59+
points = GenerateRandomList(30, 60, 20)
60+
# print("**********************************************")
61+
# print(points)
62+
points2 = points[::]
63+
res = s.findMinArrowShots(points[::])
64+
res2 = s.findMinArrowShots2(points2)
65+
# print("res: %d res2: %d" % (res, res2))
66+
if res != res2:
67+
print("***************************************")
68+
print(points)
69+
print("Wrong")
70+
print("Done!")

0 commit comments

Comments
 (0)