Minimum Number of Arrows to Burst Balloons
LeetCode 452 · நடுத்தரம் · தீர்வு, intuition & படிப்படியான வாக்த்ரூ
Sort by end; new arrow when start > current end.
நான் Minimum Number of Arrows to Burst Balloons (LeetCode 452) ஐ இப்படித் தீர்த்தேன் — Intervals பேட்டர்னின் ஒரு நடுத்தரம் பிராப்ளம். கீழே: intuition, படிப்படியான approach, என் accepted Python கோட், animated test cases மற்றும் line-by-line debugger.
கீழேயுள்ள விளக்கம் ஆங்கிலத்தில் உள்ளது.
Intuition
Sort balloons by their end. Shoot an arrow at the first balloon's end — it pops every balloon starting before that point. The first balloon starting after it needs a new arrow at its own end.
Approach
- 1
Sort by end.
- 2
arrows = 1 at points[0] end.
- 3
For each balloon, start > cur_end → new arrow at its end.
நான் எப்படித் தீர்த்தேன்
Greedy by endPythonO(n log n) time
class Solution:
def findMinArrowShots(self, points: List[List[int]]) -> int:
#minimum no of something to do something/minimum no of someting to meet some criteria - greedy
#sorty by end time so we can see 2nd baloon can't be burst with 0th baloon because it's start is greater than 0th baloon's end, so we need an extra arrow
#[1,6],[2,8],[7,12],[10,16]
points.sort(key=lambda x:x[1])
arrows = 1
cur_end = points[0][1]
i = 0
while i<len(points):
start = points[i][0]
if start>cur_end:
arrows+=1
cur_end=points[i][1]
i+=1
return arrows
Minimum Number of Arrows to Burst Balloons — அடிக்கடி கேட்கப்படும் கேள்விகள்
Minimum Number of Arrows to Burst Balloons பின்னாலுள்ள intuition என்ன?
Sort balloons by their end. Shoot an arrow at the first balloon's end — it pops every balloon starting before that point. The first balloon starting after it needs a new arrow at its own end.
Minimum Number of Arrows to Burst Balloons (LeetCode 452) ஐ எப்படித் தீர்ப்பது?
1. Sort by end. 2. arrows = 1 at points[0] end. 3. For each balloon, start > cur_end → new arrow at its end.
Minimum Number of Arrows to Burst Balloons இன் time மற்றும் space complexity என்ன?
Time: O(n log n). Space: O(1). (Accepted approach: Greedy by end.)
Minimum Number of Arrows to Burst Balloons இல் பொதுவான தவறுகள் என்ன?
Touching balloons ([1,2] and [2,3]) share one arrow — use strict >.