Skip to content

Prepare For Coder Interview – Denny

  • Basic
  • Medium
  • Hard
  • Architect
  • Life

LeetCode: Minimum Number of Arrows to Burst Balloons

Posted on August 5, 2019July 26, 2020 by braindenny

Minimum Number of Arrows to Burst Balloons



Similar Problems:

  • CheatSheet: Leetcode For Code Interview
  • CheatSheet: Common Code Problems & Follow-ups
  • Tag: #greedy, #interval, #meetingconflict

There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of the horizontal diameter. Since it’s horizontal, y-coordinates don’t matter and hence the x-coordinates of start and end of the diameter suffice. Start is always smaller than end. There will be at most 104 balloons.

An arrow can be shot up exactly vertically from different points along the x-axis. A balloon with xstart and xend bursts by an arrow shot at x if xstart <= x <= xend. There is no limit to the number of arrows that can be shot. An arrow once shot keeps travelling up infinitely. The problem is to find the minimum number of arrows that must be shot to burst all balloons.

Example:

Input:
[[10,16], [2,8], [1,6], [7,12]]

Output:
2

Explanation:
One way is to shoot one arrow for example at x = 6 (bursting the balloons [2,8] and [1,6]) and another arrow at x = 11 (bursting the other two balloons).

Github: code.dennyzhang.com

Credits To: leetcode.com

Leave me comments, if you have better ways to solve.


  • Solution:
// https://code.dennyzhang.com/minimum-number-of-arrows-to-burst-balloons
// Basic Ideas: greedy
//  All balloons need to burst
//  To hit one of [x, y], the arrow can only be within [x, y]. 
//  So we always choose y
//
//     2        8
//  1     6
//           7       12
//                10    16   
// Complexity: Time ? Space ?
import "sort"

func findMinArrowShots(points [][]int) int {
    if len(points) == 0 {
        return 0
    }
    sort.Slice(points, func(i, j int) bool {
        return points[i][1] < points[j][1]
    })
    res := 1
    y := points[0][1]
    for i:=1; i<len(points); i++ {
        // Can't re-use arrow
        if y<points[i][0] {
            res++
            y = points[i][1]
        }
    }
    return res
}
// https://code.dennyzhang.com/minimum-number-of-arrows-to-burst-balloons
// Basic Ideas: greedy
//  All balloons need to burst
//  To hit one of [x, y], the arrow can only be within [x, y]. 
//  So we always choose y
//
//     2        8
//  1     6
//           7       12
//                10    16   
// Complexity: Time O(n*log(n)), Space O(1)
import "sort"

func findMinArrowShots(points [][]int) int {
    if len(points) == 0 {
        return 0
    }
    sort.Slice(points, func(i, j int) bool {
        if points[i][0] != points[j][0] {
            return points[i][0] < points[j][0]
        } else {
            return points[i][1] < points[j][1]
        }
    })
    res := 1
    y := points[0][1]
    for i:=1; i<len(points); i++ {
        // Can't re-use arrow
        if y<points[i][0] {
            res++
            y = points[i][1]
        } else {
            // re-use arrow. Change the location of arrow to the left
            if points[i][1] < y {
                y = points[i][1]
            }
        }
    }
    return res
}
linkedin
github
slack

Post Views: 0
Posted in MediumTagged #greedy, #interval, meetingconflict

Post navigation

Series: #designstack – Design stack data structure for given requirements
LeetCode: Adding Two Negabinary Numbers

Leave a Reply Cancel reply

Your email address will not be published.

Tags

#array #backtracking #bfs #binarytree #bitmanipulation #blog #classic #codetemplate #combination #dfs #dynamicprogramming #game #graph #greedy #heap #inspiring #interval #linkedlist #manydetails #math #palindrome #recursive #slidingwindow #stack #string #subarray #trie #twopointer #twosum binarysearch editdistance hashmap intervaldp knapsack monotone oodesign presum rectangle redo review rotatelist series sql treetraversal unionfind

Recent Posts

  • a
  • a
  • a
  • a
  • a

Recent Comments

    Archives

    Categories

    • Amusing
    • Basic
    • Easy
    • Hard
    • Life
    • Medium
    • Resource
    • Review
    • Series
    • Uncategorized
    Proudly powered by WordPress | Theme: petals by Aurorum.