用最少的弓箭射穿所有氣球 Min of Arrow to Burst Balloon_Leetcode #452精選75

2024/02/23閱讀時間約 6 分鐘

題目敘述

題目會給定一個輸入陣列points,陣列元素都是一組pair,

points[i] = [starti, endi],分別代表每顆氣球的左邊界,和右邊界。

假設弓箭射出去後,動能不會減弱,沿路上的氣球都會被射穿。

請問最少需要幾隻弓箭,才可以射穿所有氣球?


題目的原文敘述


測試範例

Example 1:

Input: points = [[10,16],[2,8],[1,6],[7,12]]
Output: 2
Explanation: The balloons can be burst by 2 arrows:
- Shoot an arrow at x = 6, bursting the balloons [2,8] and [1,6].
- Shoot an arrow at x = 11, bursting the balloons [10,16] and [7,12].

Example 2:

Input: points = [[1,2],[3,4],[5,6],[7,8]]
Output: 4
Explanation: One arrow needs to be shot for each balloon for a total of 4 arrows.

Example 3:

Input: points = [[1,2],[2,3],[3,4],[4,5]]
Output: 2
Explanation: The balloons can be burst by 2 arrows:
- Shoot an arrow at x = 2, bursting the balloons [1,2] and [2,3].
- Shoot an arrow at x = 4, bursting the balloons [3,4] and [4,5].

約束條件

Constraints:

  • 1 <= points.length <= 10^5

輸入陣列長度介於1~十萬之間。

  • points[i].length == 2

每個陣列元素都是一組Pair

  • -2^31 <= xstart < xend <= 2^31 - 1

每顆氣球的左邊界、右邊界介於 32bit整數範圍之內。


演算法 Greedy + 排序

和前一題 排序應用題: 不重複的區間 有異曲同工之妙。


想要讓弓箭的使用數量最精簡,就是用同一支箭去射穿區間有重複的氣球

以行動支持創作者!付費即可解鎖
本篇內容共 2452 字、0 則留言,僅發佈於Leetcode 精選75題 上機考面試題 詳解你目前無法檢視以下內容,可能因為尚未登入,或沒有該房間的查看權限。
43會員
286內容數
由有業界實戰經驗的演算法工程師, 手把手教你建立解題的框架, 一步步寫出高效、清晰易懂的解題答案。 著重在讓讀者啟發思考、理解演算法,熟悉常見的演算法模板。 深入淺出地介紹題目背後所使用的演算法意義,融會貫通演算法與資料結構的應用。 在幾個經典的題目融入一道題目的多種解法,或者同一招解不同的題目,擴展廣度,並加深印象。
留言0
查看全部
發表第一個留言支持創作者!