給定一堆的線段范圍,每個(gè)線段i從points[i][0]~points[i][1],每次射擊某一個(gè)值p,若points[i][0]<=p<=points[i][1]的線段可以被消除,問一共需要射擊幾次可以消除所有線段。貪心
先對(duì)這個(gè)二維point list按第二維的值從小到大排序,然后從第一根線段開始,射擊最右端的點(diǎn),之后的線段若可以cover這個(gè)點(diǎn),則一并被消去
1 #452
2 #Runtime: 2320 ms (Beats 46.75%)
3 #Memory: 62.9 MB (Beats 34.32%)
4
5 class Solution(object):
6 def findMinArrowShots(self, points):
7 """
8 :type points: List[List[int]]
9 :rtype: int
10 """
11 pt_sorted = sorted(points, key = (lambda x:[x[1], x[0]]))
12 ans = 0
13 i = 0
14 while i < len(pt_sorted):
15 p = pt_sorted[i][1]
16 while i < len(pt_sorted) and pt_sorted[i][0] <= p <= pt_sorted[i][1]:
17 i += 1
18 ans += 1
19 return ans